KBD

Keith Devens .com

Saturday, March 20, 2010 Flag waving
Intertwingularity is not generally acknowledged – people keep pretending they can make things deeply hierarchical, categorizable and sequential when they can... – Ted Nelson
← New toolchain!Feather evolution →

Daily link icon Thursday, March 16, 2006

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.

← New toolchain!Feather evolution →

Comments XML gif

Erik wrote:

Preach on...

∴ Erik | 17-Mar-2006 7:48am est | #9320

Adam Vandenberg (http://adamv.com/) wrote:

∴ Adam Vandenberg | 17-Mar-2006 9:16am est | http://adamv.com/ | #9321

Hannibal wrote:

People who don't use accessors are either:

a) Stupid.
b) Lazy.
c) Think they are too clever to follow coding standards.

Any decent developer should be able to sit down and list out at least a dozen reasons why you should always use accessors.

∴ Hannibal | 19-Mar-2006 11:35am est | #9330

Keith (http://keithdevens.com/) wrote:

Ok, please do.

Keith | 19-Mar-2006 11:45am est | http://keithdevens.com/ | #9331

Hannibal wrote:

You know nothing of the true nature of evil.

∴ Hannibal | 20-Mar-2006 10:19pm est | #9335

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

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
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.102s.

(Used 8 db queries)