<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>OOP - There It Is - Good Practices</title>
    <link>http://www.vpsw.com/blogbaby/</link>
    <description>A Very Practical Blog</description>
    <language>en-us</language>
    <copyright>Dean Fiala</copyright>
    <lastBuildDate>Fri, 19 May 2006 03:35:28 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>dfiala@vpsw.com</managingEditor>
    <webMaster>dfiala@vpsw.com</webMaster>
    <item>
      <trackback:ping>http://www.vpsw.com/blogbaby/Trackback.aspx?guid=bdc25d15-1d36-44ea-bdc1-f997a74cc740</trackback:ping>
      <pingback:server>http://www.vpsw.com/blogbaby/pingback.aspx</pingback:server>
      <pingback:target>http://www.vpsw.com/blogbaby/PermaLink,guid,bdc25d15-1d36-44ea-bdc1-f997a74cc740.aspx</pingback:target>
      <dc:creator>Dean</dc:creator>
      <wfw:comment>http://www.vpsw.com/blogbaby/CommentView,guid,bdc25d15-1d36-44ea-bdc1-f997a74cc740.aspx</wfw:comment>
      <wfw:commentRss>http://www.vpsw.com/blogbaby/SyndicationService.asmx/GetEntryCommentsRss?guid=bdc25d15-1d36-44ea-bdc1-f997a74cc740</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Just because an application is developed
in .NET doesn't make it object oriented.<br /><br />
It might have classes.<br /><br />
It might have interfaces.<br /><br />
It might have methods.<br /><br />
It still is a mass of spaghetti code, with some objects thrown in to act as meatballs. 
<br /><br />
It never ceases to amaze me what lengths developers will go to to turn .NET into a
overwrought scripting language, instead of harnessing some simple OOP principles to
make their code and their lives simpler.<br /><br />
So, today we are going to talk about <a href="http://atomicobject.com/training/training-material/oo-notes/modularity-and-encapsulation.page">Encapsulation</a>.
Yes, I have spent the last two weeks ripping through an app that had objects, but
frequently didn't use them as much more than fancy structures.  And, yes, I will
rant a little bit (more).<br /><br />
Encapsulation is probably the simplest OOP concept to grasp, and it should be the
easiest to implement.  A developer does not need to understand class factories,
inheritance or polymorphism to develop encapsulated classes.  A desire for clean
interfaces coupled with an abhorrence for writing the same code twice will lead naturally
to encapsulation.<br /><br />
Yet, this well-known tool of code reuse, is often left unloved and dusty, next to
the once-cracked <a href="http://www.amazon.com/gp/product/0201633612/sr=8-2/qid=1148011183/ref=pd_bbs_2/104-4589587-2444704?%5Fencoding=UTF8">Gang
of Four OOP bible</a>. 
<br /><br />
I like to think of Encapsulation as empowering an object.  It allows the object
to say, "This is what I do. This is what I expect.  Don't tell me how to do my
job, just give me what I need and let me do it."<br /><br />
Simple, no?  You'd think so, but it is clearly not a universal practice. 
<br /><br />
Here is an object.  What it does really doesn't matter for this discussion. 
It has two ways of being populated -- one when it is first created, another for when
it is  reconstituted from the database. It has two consctructors. This is a very
common situation.  
<br /><br /><blockquote><font color="#ff0000" face="Courier New">public class SomeEntity{</font><br /><br /><font color="#ff0000" face="Courier New">    //Constructor for new
instance</font><br /><font color="#ff0000" face="Courier New">     public SomeEntity(string
Name, string ANeededValue, int AnotherNeededValue){...}</font><br /><font color="#ff0000" face="Courier New">     </font><br /><font color="#ff0000" face="Courier New">    //Constructor for a retrieved
instance</font><br /><font color="#ff0000" face="Courier New">     public SomeEntity(int
ID, string Name, XmlDocument TheReasonForTheObject){...} 
<br /><br />
    //Members<br />
    private string mName;<br />
    //...<br /></font><br /><font color="#ff0000" face="Courier New">    //Properties, Methods,
Etc.<br /><br />
    public void DoSomethingImportant(){...}<br /><br />
    public string Name{<br />
       { get{return mName;}<br />
    }<br />
    //...<br /></font><font color="#ff0000" face="Courier New">}</font><br /></blockquote><br />
Now, I don't have a problem with the first constructor -- at least there is one, this
actually demonstrates an important bit of encapsulation: controlling how the object
is instantiated.  The constructor gives a way to tell the world,  "This
is what I need to start properly!"<br /><br />
For the free-for-all that can result without encapsulated instantiation, here's the
same object sans a defined constructor...<br /><br /><blockquote><font color="#ff0000" face="Courier New">public class SomeEntity{</font><br /><br />
       <font color="#ff0000" face="Courier New">//default
constructor</font><br />
       <font color="#ff0000" face="Courier New">public
SomeEntity(){}</font><br /><font color="#ff0000" face="Courier New">   </font><br /><font color="#ff0000" face="Courier New">     //Members</font><br /><font color="#ff0000" face="Courier New">     public string Name;<br />
    public string ANeededValue;<br />
    public int AnotherNeededValue;<br />
    
<br />
    </font><font color="#ff0000" face="Courier New">//Properties, Methods,
Etc....<br /></font><font color="#ff0000" face="Courier New">    public void DoSomethingImportant(){...}</font><br /><font color="#ff0000" face="Courier New"></font><font color="#ff0000" face="Courier New"></font><br /><font color="#ff0000" face="Courier New"></font><font color="#ff0000" face="Courier New">}</font><br /></blockquote><br />
What's required to make sure an instance of this object works properly?  The
developer's dilligence, memory and typing skills.  Everywhere the object is created,
the developer must remember to type four lines of code -- just to get the object in
a state where it is minimally functional.<br /><br /><blockquote><font color="#ff0000" face="Courier New">SomeEntity AnObjectINeed = new
SomeEntity();<br />
AnObjectINeed.Name = "Fred";<br /></font><font color="#ff0000" face="Courier New">AnObjectINeed.ANeededValue = "Tuesday";<br /></font><font color="#ff0000" face="Courier New">AnObjectINeed.AnotherNeededValue =
789;</font><br /><font color="#ff0000" face="Courier New"></font></blockquote><br />
Forget to supply a value -- oops, error. What's missing?  Hope the exception
message is clear and go hunting. It is bad enough that this code will be cut-and-pasted
willy-nilly every time the object is needed, but what is worse is the maintenance
implication.  Things change, the object needs an additonal value to work properly....<br /><br /><blockquote><font color="#ff0000" face="Courier New">public class SomeEntity{</font><br /><br /><font color="#ff0000" face="Courier New">   </font><br /><font color="#ff0000" face="Courier New">     //Members</font><br /><font color="#ff0000" face="Courier New">     public string Name;</font><br /><font color="#ff0000" face="Courier New">     public string ANeededValue;</font><br /><font color="#ff0000" face="Courier New">     public int AnotherNeededValue;<br />
    public DateTime OoopsForgotThis;<br /></font><font color="#ff0000" face="Courier New">     </font><br /><font color="#ff0000" face="Courier New">     </font><font color="#ff0000" face="Courier New">//Properties,
Methods, Etc....</font><br /><font color="#ff0000" face="Courier New"></font><font color="#ff0000" face="Courier New">   
public void DoSomethingImportant(){...}</font><br /><font color="#ff0000" face="Courier New"></font><br /><font color="#ff0000" face="Courier New"></font><font color="#ff0000" face="Courier New">}</font><br /></blockquote><br />
Hmmm, what happens now? The object won't work without the new value, but the application
still compiles.  The developer must hunt for EVERY creation of the object and
add another line of code, and then clean up the inevitable bugs when he misses a few.<br /><br />
Using a defined constructor avoids this problem.<br /><br /><font color="#ff0000" face="Courier New">    //Constructor for new
instance</font><br /><font color="#ff0000" face="Courier New">     public SomeEntity(string
Name, string ANeededValue, int AnotherNeededValue, </font><font color="#ff0000" face="Courier New">DateTime
OoopsForgotThis</font><font color="#ff0000" face="Courier New">){...}</font><br /><br />
The application won't compile now. All the places the code needs to be changed will
be listed, they don't have to be hunted for.  Once the changes have been made
and the app compiles, the object will always have what it needs to do its job.<br /><br />
Pardon the long aside, and let me finally return to what bothered me about the second
constructor in the example above...<br /><br /><blockquote><font color="#ff0000" face="Courier New">//Constructor for a retrieved
instance</font><br /><font color="#ff0000" face="Courier New">  public SomeEntity(int ID, string Name,
XmlDocument TheReasonForTheObject){...} </font><br /></blockquote><br />
What bothers me here is a subtle, but more troubling, lack of encapsulation related
to this constructor.  
<br /><br />
In order to call this constructor, three parameters must be supplied, an ID, a name
and an XMLDocument. Now, where are these values stored? In a database record. 
They are retrieved via a stored procedure call using the entity's ID, which is a unique
integer value.  
<br /><br /><b>A UNIQUE value.</b><br /><br />
The ID is unique and possesses all the object needs to know to reconstitute itself. 
Why then, are the other two values needed in the constructor?<br /><br />
They're NOT!<br /><br />
Why are they there?  
<br /><br />
I have no friggin' clue.  But this is part of the foo I've been fighting for
the last few weeks. There are 5 or 6 lines of database-related code that precede every
use of this constructor, along with the creation of an XmlDocument object from its
string respresentation.  That's 8 lines of code repeated each time the object
is recreated from the database, all of which SHOULD have been placed in the object,
so it could have populated itself.<br /><br />
But that's not all?  What is controlling whether or not:<br />
1) The name and XML document supplied are actually related to the supplied ID?<br />
2) The XMLDocument has the proper schema the object expects?<br /><br />
The memory, diligence and typing skills of the developer.  In other words, NO
ONE!  
<br /><br />
By writing the object so it controls its own data retrieval, these problems are avoided.
The object saved (or should have saved -- don't get me started) its own data and can
be fairly confident it is getting the same information back -- in proper form.  
<br /><br /><blockquote><font color="#ff0000" face="Courier New">//Constructor for a retrieved
instance</font><br /><font color="#ff0000" face="Courier New">public SomeEntity(int ID){...} </font><br /></blockquote>The object is empowered, it controls its data, it works properly. 
It can be used in code without requiring 8 supporting lines of code to be written
each time.  
<br /><br />
Why?  
<br /><br />
It's encapsulated!<br /><br />
Want to make the app work when its disconnected and need to store the object in a
local Access database?  No problem, change the storage and retrieval code <b>in
the object</b>.  The rest of the app doesn't know and doesn't care how or where
the object data is stored.  All it knows and cares about is that if it saved
a SomeEntity object with a unique ID = 1776, that when this code is called...<br /><br />
    <font color="#ff0000" face="Courier New">SomeEntity AnObjectINeed
= new SomeEntity(1776);</font><br /><br />
It will get back a SomeEntity object with an ID = 1776 and the proper XMLDocument. 
<br /><br />
An object born ready to do what it is supposed to do. 
<br /><br />
What's hard about that?<br /><br />
Really?<br /><p></p><img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=bdc25d15-1d36-44ea-bdc1-f997a74cc740" /></body>
      <title>Encapsulate For Fun and Profit</title>
      <guid isPermaLink="false">http://www.vpsw.com/blogbaby/PermaLink,guid,bdc25d15-1d36-44ea-bdc1-f997a74cc740.aspx</guid>
      <link>http://www.vpsw.com/blogbaby/PermaLink,guid,bdc25d15-1d36-44ea-bdc1-f997a74cc740.aspx</link>
      <pubDate>Fri, 19 May 2006 03:35:28 GMT</pubDate>
      <description>Just because an application is developed in .NET doesn't make it object oriented.&lt;br&gt;
&lt;br&gt;
It might have classes.&lt;br&gt;
&lt;br&gt;
It might have interfaces.&lt;br&gt;
&lt;br&gt;
It might have methods.&lt;br&gt;
&lt;br&gt;
It still is a mass of spaghetti code, with some objects thrown in to act as meatballs. 
&lt;br&gt;
&lt;br&gt;
It never ceases to amaze me what lengths developers will go to to turn .NET into a
overwrought scripting language, instead of harnessing some simple OOP principles to
make their code and their lives simpler.&lt;br&gt;
&lt;br&gt;
So, today we are going to talk about &lt;a href="http://atomicobject.com/training/training-material/oo-notes/modularity-and-encapsulation.page"&gt;Encapsulation&lt;/a&gt;.
Yes, I have spent the last two weeks ripping through an app that had objects, but
frequently didn't use them as much more than fancy structures.&amp;nbsp; And, yes, I will
rant a little bit (more).&lt;br&gt;
&lt;br&gt;
Encapsulation is probably the simplest OOP concept to grasp, and it should be the
easiest to implement.&amp;nbsp; A developer does not need to understand class factories,
inheritance or polymorphism to develop encapsulated classes.&amp;nbsp; A desire for clean
interfaces coupled with an abhorrence for writing the same code twice will lead naturally
to encapsulation.&lt;br&gt;
&lt;br&gt;
Yet, this well-known tool of code reuse, is often left unloved and dusty, next to
the once-cracked &lt;a href="http://www.amazon.com/gp/product/0201633612/sr=8-2/qid=1148011183/ref=pd_bbs_2/104-4589587-2444704?%5Fencoding=UTF8"&gt;Gang
of Four OOP bible&lt;/a&gt;. 
&lt;br&gt;
&lt;br&gt;
I like to think of Encapsulation as empowering an object.&amp;nbsp; It allows the object
to say, "This is what I do. This is what I expect.&amp;nbsp; Don't tell me how to do my
job, just give me what I need and let me do it."&lt;br&gt;
&lt;br&gt;
Simple, no?&amp;nbsp; You'd think so, but it is clearly not a universal practice. 
&lt;br&gt;
&lt;br&gt;
Here is an object.&amp;nbsp; What it does really doesn't matter for this discussion.&amp;nbsp;
It has two ways of being populated -- one when it is first created, another for when
it is&amp;nbsp; reconstituted from the database. It has two consctructors. This is a very
common situation.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier New"&gt;public class SomeEntity{&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Constructor for new
instance&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public SomeEntity(string
Name, string ANeededValue, int AnotherNeededValue){...}&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Constructor for a retrieved
instance&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public SomeEntity(int
ID, string Name, XmlDocument TheReasonForTheObject){...} 
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; //Members&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private string mName;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; //...&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Properties, Methods,
Etc.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void DoSomethingImportant(){...}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; { get{return mName;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; //...&lt;br&gt;
&lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;}&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
Now, I don't have a problem with the first constructor -- at least there is one, this
actually demonstrates an important bit of encapsulation: controlling how the object
is instantiated.&amp;nbsp; The constructor gives a way to tell the world,&amp;nbsp; "This
is what I need to start properly!"&lt;br&gt;
&lt;br&gt;
For the free-for-all that can result without encapsulated instantiation, here's the
same object sans a defined constructor...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier New"&gt;public class SomeEntity{&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#ff0000" face="Courier New"&gt;//default
constructor&lt;/font&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#ff0000" face="Courier New"&gt;public
SomeEntity(){}&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Members&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public string ANeededValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public int AnotherNeededValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;//Properties, Methods,
Etc....&lt;br&gt;
&lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void DoSomethingImportant(){...}&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;}&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt; 
&lt;br&gt;
What's required to make sure an instance of this object works properly?&amp;nbsp; The
developer's dilligence, memory and typing skills.&amp;nbsp; Everywhere the object is created,
the developer must remember to type four lines of code -- just to get the object in
a state where it is minimally functional.&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier New"&gt;SomeEntity AnObjectINeed = new
SomeEntity();&lt;br&gt;
AnObjectINeed.Name = "Fred";&lt;br&gt;
&lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;AnObjectINeed.ANeededValue = "Tuesday";&lt;br&gt;
&lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;AnObjectINeed.AnotherNeededValue =
789;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&lt;/font&gt; &lt;/blockquote&gt; 
&lt;br&gt;
Forget to supply a value -- oops, error. What's missing?&amp;nbsp; Hope the exception
message is clear and go hunting. It is bad enough that this code will be cut-and-pasted
willy-nilly every time the object is needed, but what is worse is the maintenance
implication.&amp;nbsp; Things change, the object needs an additonal value to work properly....&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier New"&gt;public class SomeEntity{&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Members&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; public string ANeededValue;&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; public int AnotherNeededValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public DateTime OoopsForgotThis;&lt;br&gt;
&lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;//Properties,
Methods, Etc....&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
public void DoSomethingImportant(){...}&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt; &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;}&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt; 
&lt;br&gt;
Hmmm, what happens now? The object won't work without the new value, but the application
still compiles.&amp;nbsp; The developer must hunt for EVERY creation of the object and
add another line of code, and then clean up the inevitable bugs when he misses a few.&lt;br&gt;
&lt;br&gt;
Using a defined constructor avoids this problem.&lt;br&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Constructor for new
instance&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public SomeEntity(string
Name, string ANeededValue, int AnotherNeededValue, &lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;DateTime
OoopsForgotThis&lt;/font&gt;&lt;font color="#ff0000" face="Courier New"&gt;){...}&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
The application won't compile now. All the places the code needs to be changed will
be listed, they don't have to be hunted for.&amp;nbsp; Once the changes have been made
and the app compiles, the object will always have what it needs to do its job.&lt;br&gt;
&lt;br&gt;
Pardon the long aside, and let me finally return to what bothered me about the second
constructor in the example above...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier New"&gt;//Constructor for a retrieved
instance&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;&amp;nbsp; public SomeEntity(int ID, string Name,
XmlDocument TheReasonForTheObject){...} &lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
What bothers me here is a subtle, but more troubling, lack of encapsulation related
to this constructor.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
In order to call this constructor, three parameters must be supplied, an ID, a name
and an XMLDocument. Now, where are these values stored? In a database record.&amp;nbsp;
They are retrieved via a stored procedure call using the entity's ID, which is a unique
integer value.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;A UNIQUE value.&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
The ID is unique and possesses all the object needs to know to reconstitute itself.&amp;nbsp;
Why then, are the other two values needed in the constructor?&lt;br&gt;
&lt;br&gt;
They're NOT!&lt;br&gt;
&lt;br&gt;
Why are they there?&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
I have no friggin' clue.&amp;nbsp; But this is part of the foo I've been fighting for
the last few weeks. There are 5 or 6 lines of database-related code that precede every
use of this constructor, along with the creation of an XmlDocument object from its
string respresentation.&amp;nbsp; That's 8 lines of code repeated each time the object
is recreated from the database, all of which SHOULD have been placed in the object,
so it could have populated itself.&lt;br&gt;
&lt;br&gt;
But that's not all?&amp;nbsp; What is controlling whether or not:&lt;br&gt;
1) The name and XML document supplied are actually related to the supplied ID?&lt;br&gt;
2) The XMLDocument has the proper schema the object expects?&lt;br&gt;
&lt;br&gt;
The memory, diligence and typing skills of the developer.&amp;nbsp; In other words, NO
ONE!&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
By writing the object so it controls its own data retrieval, these problems are avoided.
The object saved (or should have saved -- don't get me started) its own data and can
be fairly confident it is getting the same information back -- in proper form.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier New"&gt;//Constructor for a retrieved
instance&lt;/font&gt;
&lt;br&gt;
&lt;font color="#ff0000" face="Courier New"&gt;public SomeEntity(int ID){...} &lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;The object is empowered, it controls its data, it works properly.&amp;nbsp;
It can be used in code without requiring 8 supporting lines of code to be written
each time.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
Why?&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
It's encapsulated!&lt;br&gt;
&lt;br&gt;
Want to make the app work when its disconnected and need to store the object in a
local Access database?&amp;nbsp; No problem, change the storage and retrieval code &lt;b&gt;in
the object&lt;/b&gt;.&amp;nbsp; The rest of the app doesn't know and doesn't care how or where
the object data is stored.&amp;nbsp; All it knows and cares about is that if it saved
a SomeEntity object with a unique ID = 1776, that when this code is called...&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#ff0000" face="Courier New"&gt;SomeEntity AnObjectINeed
= new SomeEntity(1776);&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
It will get back a SomeEntity object with an ID = 1776 and the proper XMLDocument. 
&lt;br&gt;
&lt;br&gt;
An object born ready to do what it is supposed to do. 
&lt;br&gt;
&lt;br&gt;
What's hard about that?&lt;br&gt;
&lt;br&gt;
Really?&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=bdc25d15-1d36-44ea-bdc1-f997a74cc740" /&gt;</description>
      <comments>http://www.vpsw.com/blogbaby/CommentView,guid,bdc25d15-1d36-44ea-bdc1-f997a74cc740.aspx</comments>
      <category>.NET</category>
      <category>Good Practices</category>
      <category>OOP</category>
    </item>
    <item>
      <trackback:ping>http://www.vpsw.com/blogbaby/Trackback.aspx?guid=4f1a8021-37cb-4e3c-8d64-4147e1656d7e</trackback:ping>
      <pingback:server>http://www.vpsw.com/blogbaby/pingback.aspx</pingback:server>
      <pingback:target>http://www.vpsw.com/blogbaby/PermaLink,guid,4f1a8021-37cb-4e3c-8d64-4147e1656d7e.aspx</pingback:target>
      <dc:creator>Dean</dc:creator>
      <wfw:comment>http://www.vpsw.com/blogbaby/CommentView,guid,4f1a8021-37cb-4e3c-8d64-4147e1656d7e.aspx</wfw:comment>
      <wfw:commentRss>http://www.vpsw.com/blogbaby/SyndicationService.asmx/GetEntryCommentsRss?guid=4f1a8021-37cb-4e3c-8d64-4147e1656d7e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Among the many improvements of .NET 2.0 is a subtle but welcome one in GridView, the
replacement to the DataGrid control. 
</p>
        <p>
The Items have finally left the building.  Any old kinda grid control should
have columns and rows.
</p>
        <p>
The DataGrid had Columns and <em>Items</em>?
</p>
        <p>
What in the world is an <em>Item</em>?  It's a row, but it's not called that
-- for a very good reason I am sure.
</p>
        <p>
The GridView has Columns and <strong>Rows</strong>!
</p>
        <p>
Hooray! 
</p>
        <p>
Instead of ItemCreated, or ItemDataBound event handlers, there are RowCreated and
RowDataBound.  Instead of ItemType there is RowType.
</p>
        <p>
I cannot tell you how confusing this was for developers when first working with the
DataGrid.  "How can I access a row?" must have been screamed at monitors
from Mountain View to Mumbay.
</p>
        <p>
What things are named can greatly affect how easy/hard it is use someone else's interface
or maintain code.  How many times have you written code for something, only to
find the same functionality in an obscurely/badly named function or class?
</p>
        <p>
Consistent, meaningful naming makes it much easier for someone else, or the future
you (go read some code you haven't touched in a year) to get what you are trying to
do.
</p>
        <p>
I must confess that I have not always been the best practioner.  My original
programming platform was the Apple II, and variable names were limited to 8 characters
(only the first three of which were meaningful -- found that out the HARD way), and
I've never fully overcome the combined effects of that and my Modified Hunt &amp;
Peck Typing skills. 
</p>
        <p>
This is something I have worked to be better at, but this <a href="http://aspadvice.com/lists/message.aspx?MessageID=167068">post</a> from
Ryan Olshan really made it hit home.  I will paraphrase his thesis as "Variable
names should be like characters in novel, they should be easily identifiable and make the
reader care what happens to them".  
</p>
        <p>
Especially with Intellisense and the built-in refactoring tools in Visual Studio (this <a href="http://msdn2.microsoft.com/en-us/library/ckfya594(VS.80).aspx">one </a>especially),
there is no reason to skimp on meaningful naming and it's easy to fix past mistakes.
</p>
        <img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=4f1a8021-37cb-4e3c-8d64-4147e1656d7e" />
      </body>
      <title>There Is Something In a Name</title>
      <guid isPermaLink="false">http://www.vpsw.com/blogbaby/PermaLink,guid,4f1a8021-37cb-4e3c-8d64-4147e1656d7e.aspx</guid>
      <link>http://www.vpsw.com/blogbaby/PermaLink,guid,4f1a8021-37cb-4e3c-8d64-4147e1656d7e.aspx</link>
      <pubDate>Wed, 15 Mar 2006 17:41:54 GMT</pubDate>
      <description>&lt;p&gt;
Among the many improvements of .NET 2.0 is a subtle but welcome one in GridView, the
replacement to the DataGrid control. 
&lt;/p&gt;
&lt;p&gt;
The Items have finally left the building.&amp;nbsp; Any old kinda grid control should
have columns and rows.
&lt;/p&gt;
&lt;p&gt;
The DataGrid had Columns and &lt;em&gt;Items&lt;/em&gt;?
&lt;/p&gt;
&lt;p&gt;
What in the world is an &lt;em&gt;Item&lt;/em&gt;?&amp;nbsp; It's a row, but it's not called that
-- for a very good reason I am sure.
&lt;/p&gt;
&lt;p&gt;
The GridView has Columns and &lt;strong&gt;Rows&lt;/strong&gt;!
&lt;/p&gt;
&lt;p&gt;
Hooray! 
&lt;/p&gt;
&lt;p&gt;
Instead of ItemCreated, or ItemDataBound event handlers, there are RowCreated and
RowDataBound.&amp;nbsp; Instead of ItemType there is RowType.
&lt;/p&gt;
&lt;p&gt;
I cannot tell you how confusing this was for developers when first working with the
DataGrid.&amp;nbsp; "How can I access a row?"&amp;nbsp;must have been screamed&amp;nbsp;at monitors
from Mountain View to Mumbay.
&lt;/p&gt;
&lt;p&gt;
What things are named can greatly affect how easy/hard it is use someone else's interface
or maintain code.&amp;nbsp; How many times have you written code for something, only to
find the same functionality in an obscurely/badly named function or class?
&lt;/p&gt;
&lt;p&gt;
Consistent, meaningful naming makes it much easier for someone else, or the future
you (go read some code you haven't touched in a year) to get what you are trying to
do.
&lt;/p&gt;
&lt;p&gt;
I must confess that I have not always been the best practioner.&amp;nbsp; My original
programming platform was the Apple II, and variable names were limited to 8 characters
(only the first three of which were meaningful -- found that out the HARD way), and
I've never fully overcome the combined effects of that and my Modified Hunt &amp;amp;
Peck Typing skills.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
This is something I have&amp;nbsp;worked to be better at, but&amp;nbsp;this &lt;a href="http://aspadvice.com/lists/message.aspx?MessageID=167068"&gt;post&lt;/a&gt; from
Ryan Olshan really made it hit home.&amp;nbsp; I will paraphrase his thesis as "Variable
names should be like characters in novel, they should be easily identifiable and make&amp;nbsp;the
reader&amp;nbsp;care what happens to them".&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Especially with Intellisense and the built-in refactoring tools in Visual Studio (this &lt;a href="http://msdn2.microsoft.com/en-us/library/ckfya594(VS.80).aspx"&gt;one &lt;/a&gt;especially),
there is no reason to skimp on meaningful naming and it's easy to fix past mistakes.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=4f1a8021-37cb-4e3c-8d64-4147e1656d7e" /&gt;</description>
      <comments>http://www.vpsw.com/blogbaby/CommentView,guid,4f1a8021-37cb-4e3c-8d64-4147e1656d7e.aspx</comments>
      <category>.NET</category>
      <category>Good Practices</category>
    </item>
  </channel>
</rss>