Monday, January 11, 2010

How to increase Lines of Code counts by bad initialation

I've seen old style coding which often results in HIGH lines of code counts. For example this code section could have been done in just ONE statement....

_operationsPerSecond = new PerformanceCounter();
_operationsPerSecond.CategoryName = _categoryName;
_operationsPerSecond.CounterName = "# operations / sec";
_operationsPerSecond.MachineName = ".";
_operationsPerSecond.ReadOnly = false;
_operationsPerSecond.RawValue = 0;

Instead of:

_TotalOperations = new PerformanceCounter(){CategoryName = categoryName,CounterName = "# operations executed",MachineName = ".",ReadOnly = false,RawValue = 0}

While in reality (looking at the defaults for the class), only the following is needed

_TotalOperations = new PerformanceCounter(){CategoryName = categoryName,CounterName = "# operations executed",ReadOnly =false};

I noticed that the original author had this initialator was already in the code base, so:

_TotalOperations = new PerformanceCounter(categoryName, "# operations executed", false);

OR, not using it.

_TotalOperations = new PerformanceCounter(){CategoryName = categoryName,CounterName = "# operations executed",ReadOnly =false}

This causes me to reflect on multiple initiators. Prior to (){ } implementation, they made sense. They no longer make sense -- unless the parameters are internal only OR have private set.

In this case, they are all read and write, so the best practise would be:

_TotalOperations = new PerformanceCounter(){CategoryName = categoryName,CounterName = "# operations executed",ReadOnly =false}

I will deal with the redundant initialators initializers in my next post.


Another factor that concerns me is the use of strings instead of an enumeration for CounterName. The counter name is not coming from user input, so using an adhoc string is very questionable.

No comments:

Post a Comment