KBD

Keith Devens .com

Tuesday, March 9, 2010 Flag waving
Life moves pretty fast. If you don't stop and look around once in a while, you could miss it.... – Ferris Bueller

Archive: March 16, 2006

← March 14, 2006March 17, 2006 →

Daily link icon Thursday, March 16, 2006

  1. James Gosling's foolishness is a perfect candidate for the age old question, "Are you a fool or are you a liar?" In my experience, whenever you have to ask that question, the answer is typically: both.

       (0)
  2. Just came across this story at Scientific American on feather evolution and forwarded it to a friend because I thought it was interesting and funny. He forwarded back a link to this amusing Creation-Evolution Headlines commentary on the story: “This Is a Problem”: Dino-Feather Story Gets Scaly.

       (0) Tags: [Evolution]

Asinine accessors, or preposterous properties

Was just reading this post by Jeremy Miller (via Keith) and some of the code in it has forced me to write this post, completely unrelated to the topic of Jeremy's post.

So often I see people writing properties that look like this:

public DateTime InvoiceDate
{
    get { return _invoiceDate; }
    set { _invoiceDate = value; }
}

public string InvoiceNumber
{
    get { return _invoiceNumber; }
    set { _invoiceNumber = value; }
}

Damnit people, C#'s has its properties so that we don't have to go and write accessors for all public properties like people do in Java. Make it a public data member and be done with it until you actually need custom behavior. Otherwise you're just needlessly writing code around what C# already does.

Update: There are actually two cases where you would want to define "unnecessary" properties such as these. Adam explains the case of Winforms Data Binding:

Properties and member fields are treated differently by System.Reflection. You can databind to an object property, but not a plan old member.

...

The sad thing is that both MemberInfo and PropertyInfo, the classes that encapsulate field and property access, both have "GetValue" and "SetValue" members, but they're not defined in the base class nor with a common interface, so you can't use these interchangeably.

The other case is if you're writing code specifically to be distributed as a dll. Even though the syntax is the same, the generated code is of course different, and changing from a member to a property would require a recompile of the client code.

← March 14, 2006March 17, 2006 →
March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive

Generated in about 0.045s.

(Used 7 db queries)