The ONE thing that I remember most out of anything during my CS classes atschool...
All instance variables (fields in .Net lingo) should be private... (more on this in aminute)
Which brings me to a blog post that Isaw this morning from Avonelle
“The last thing I need to do is to step through a bunch of dumb propertystatements, especially on a routine with lots of arguments. It is no wonder some peoplegive up on object-oriented programming - it drives me nuts!â€
Not to pick on Avonelle as a person or a software developer.. but this statementmakes zero sense...
- Stepping through dump property statements can be rememdied by using F10 in VisualStudio (step over) ... while in the debugger... (which I'm sure she knows)
- Dumb property statements is not always the case, there could be logic within thestatements... you should know where your data is coming from...
- There is no connection between properties (C# language syntax) and object-orientedprogramming/design other than it being a means to an end...
- Sometimes giving up on an idea comes from not fully understanding the idea in thefirst place... (not to say she doesn't)
< cue stealthy, sneaky, spy music >
In object oriented programming, one must encapsulate the information in such a way sothat the user of the object need not to know the internals. One way to enforceencapsulation is to make all of your instance variables (internal data) private... thusallowing the object to decide who gets to see what and how they get to see it. In theJava world, you write accessors (methods that retrieve and set the data in the waythat you want to). This ensures encapsulation and allows the developer to more easilywrite validation logic, data processing logic, and other types of code. In C# theequivalent is properties, they make exposing your fields easy... and in fact, propertiesare just syntactic sugar for accessor methods GET and SET. This I would imagine, isdefinitely a MUST KNOW type of knowledge for any object oriented programmer.
In short, once again I'll state.. all instance variables should be private.. useaccessors or properties to get to the state of your object.