exceptionz

Thoughts on Technology, Methodology and Programming.

Archive for May, 2005

Code Level Encapsulation Guidelines for Business Service Objects

Posted by Marcus Wyatt on 25 May 2005

1I’ve found this extremely informative article on Designing Efficient Business Service Objects and in the article the author noted the following Guidelines for Code Encapsulation that he follows when developing his Service Objects.

Code Level Encapsulation Guidelines

  1. Object encapsulation is at the forefront of consideration.
  2. Use Interfaces to create object contracts
  3. Follow descriptive naming conventions for all classes and methods.
  4. If an object contains more than 3 public methods (other than accessor methods) then it is a sign that the object is not sufficiently encapsulated, particularly controller classes which I many times limit to just 1 public method.
  5. Refrain from passing raw data into methods. Rather favor objects as parameters.
  6. Limit method parameters to 3 or less unless there is compelling reason for more. Most times a method that needs more parameters is not encapsulated and needs to be broken up into other methods.
  7. Limit method body text to what is easily seen in one screen.
  8. Limit each method to one specific discrete operation regardless of how small, even if the operation is not called from multiple other places in the class.
  9. Provide verbose and detailed JavaDoc comments no matter how trivial the method.
  10. JavaDoc all method parameters, returns, and exceptions.
  11. Do not nest conditional logic more than 2 levels; more indicates the need to break out into another method call.
  12. Consolidate all class functionality (i.e. business purpose and process) in one main public method which calls out to other methods in the class to do the work.
  13. Do not nest method calls more than 2 layers deeper then from the main public method (i.e. main public calls method1 which in turn calls method2 – but method2 is not allowed to cascade further).
  14. Try not to go more than 3 levels of inheritance before getting to your concrete class.
  15. Rather than using inheritance hierarchies to facilitate business solution responsibility, modern Object Oriented Design technology suggests combining aggregation (has a) with composition (is a type of) within inheritance hierarchies. For example, pure inheritance might have an ancestor class Person with an Employee descendant which might in turn subclass an HourlyWorker. But HourlyWorker “is a type of” Employee, not an “is a” Employee. Thus, Person will extend Employee but Employee will have an aggregate abstract EmployeeType which will then extend HourlyWorker, SeasonalWorker, etc. as descendants. Deep inheritance hierarchies tend to be quite bloated and clog up the pipe with excess object overhead and referencing through the stack. They can also be quite difficult to understand from a maintenance perspective with later developers coming into the system.

After reading the whole article I think these rules does apply in a big way. What do you think?

tags: , ,

Currently listening to: ** 79. Arksun – Astrodancer – Winamp ***

Posted in Development, OO | Leave a Comment »

Refactoring: Improving the Design of Existing Code by Martin Fowler

Posted by Marcus Wyatt on 22 May 2005

I love this book and it has a proud place on my desk at arms length.

A chapter outline:

  • Chapter 1: Refactoring, a First Example (C#)
  • Chapter 2: Principles of Refactoring
  • Chapter 3: Bad Smells in Code
  • Chapter 4: Building Tests
  • Chapter 5: Toward a Catalog of Refactorings
  • Chapter 6: Composing Methods
  • Chapter 7: Moving Features Between Objects
  • Chapter 8: Organizing Data
  • Chapter 9: Simplifying Conditional Expressions
  • Chapter 10: Making Method Calls Simpler
  • Chapter 11: Dealing with Generalization
  • Chapter 12: Big Refactorings
  • Chapter 13: Refactoring, Reuse, and Reality
  • Chapter 14: Refactoring Tools
  • Chapter 15: Putting It All Together

Every OO developer should have a copy of this great book.

tags: , ,

Currently listening to: – Winamp *** 349. u2 – lemon

Posted in Refactoring | Leave a Comment »

How do you bind a Combobox to an Enum Property?

Posted by Marcus Wyatt on 14 May 2005

Edit: I’ve updated the code and post here.

Probably many developers have worked with win forms data binding and wanted to do the following:

Data bind a Combobox to an enum in your domain.

Here is my solution to the problem:

We create a ComboBoxEnumItem Class. This class is a pure fabrication class that has two properties we’ll use when databinding to the combo. We have Value which is used by the ValueMember and DisplayItem that is used by the DisplayMember of the combo. Note that when we construct a new ComboBoxEnumItem instance that we call the AsciiEscape method when populating the displayItem field.

using System.Text.RegularExpressions;

public class ComboBoxEnumItem
{
   private object value;
   private string toString;
   private string displayItem;

   public object Value
   {
      get
      {
         return value;
      }
   }

   public override string ToString()
   {
      return toString;
   }

   public string DisplayItem
   {
      get
      {
         return displayItem;
      }
   }

   public ComboBoxEnumItem( object value )
   {
      this.value = value;
      displayItem = AsciiEscape( value.ToString() );
      toString = displayItem;
   }

   private string AsciiEscape( string value )
   {
      if( value == null )
         return null;

      string returnValue = "";
      Regex r = new Regex("0x[0-9A-Z]{4}");
      MatchCollection m = r.Matches( value );
      Match mtchPrevious = null;

      for( int i = 0; i < m.count; i++ )
      {
         int istartindex = (mtchprevious == null ? 0: mtchprevious.index + mtchprevious.length );
         returnvalue += value.substring( istartindex, m[i].index - istartindex );
         string strgroup = m[ i ].value;

         if( strgroup.startswith("0x") )
         {
            string strcode = strgroup.substring(2);
            byte bval = system.convert.tobyte( strcode, 16 );
            string s = new string((char)bval,1);
            returnvalue += s;
         }

         mtchprevious = m[i];
      }

      if ( mtchprevious == null )
      {
         returnvalue += value;
      }
      else
      {
         returnvalue += value.substring( mtchprevious.index + mtchprevious.length );
      }

      return returnvalue;
   }
}

The AsciiEscape method is used to Replace the Ascii characters in our Enum with the character representation. We do the Ascii characters because we want our combo to display “Bi- Annual” instead of “BiAnnual”. Look at the following enum as an example:

public enum PaymentFrequencies
{
   Annual = 1,
   Bi0x00A00x002D0x00A0Annual = 2,
   Quarterly = 4, Monthly = 12,
   Fortnightly = 26,
   Weekly = 52
}

Now, all that we need is the following code behind the form to bind our Combobox and we are away:

private void InitDataBindings()
{
   cboEntity.DataSource = GetEnumComboDataSource( PaymentFrequencies );
   cboEntity.DisplayMember = "DisplayItem";
   cboEntity.ValueMember = "Value";
   cboEntity.DataBindings.Add( "SelectedValue", LoanLogic, "PaymentFrequency" );
}

private ArrayList GetEnumComboDataSource( Type type )
{
   ArrayList comboData = new ArrayList();
   Array ary = Enum.GetValues( type );

   for( int j = 0; j < ary.length; j++ )
   {
      comboData.Add( new ComboBoxEnumItem( ary.GetValue( j ) ) );
   }

   return comboData;
}

The GetEnumComboDataSource method creates an ArrayList for us to use as a data source for the combobox.

And there you have it, a Combobox bound to an Enum!

tags: , ,

Currently listening to: 432. delerium – underwater – Winamp ***

Posted in Development | Leave a Comment »