KBD

Keith Devens .com

Friday, July 4, 2008 Flag waving
"Fools! Don't they know that tears are a woman's most effective weapon?" – Catwoman (The Batman TV Series, episode 83)
← 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)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.14s.

(Used 8 db queries)

mobile phone