Refactoring

Applying logic to disparate items with anonymous methods

My previous post suggested that it is possible to apply the same logic to elements that are disparate, that is do not have a common base class or interface that is relevant to the operation. This post describes a method of doing this using anonymous methods. I will use a simple example of summing values, but the principal is applicable generally, and is not specific to looping.Take for instance the following code:List<int> integerList = new List<int>();for (int initialValue = 1; initialValue <= 5; initialValue++){ integerList.Add(initialValue);}int sum = 0;foreach (int value in integerList){ sum...

posted @ Friday, March 21, 2008 8:30 AM | Feedback (0)

Keeping your code maintainable: Manageable method lengths

Unless checked the length of methods tends to grow without bound. Changes to functionality result in a developer adding additional functionality to an existing method to acheive their current requirement. The method gets longer and more complex, and this has a number of negative consequences.Beyond a point the human mind simply can't deal with complexity efficiently. This leads to fragile, buggy code that can't be altered because the side effects cannot be adequately predicted.As logic is encapsulated in large blocks it becomes more difficult to reuse the existing code. This leads duplicate logic tends to proliferate in the codebase. Duplicate logic...

posted @ Wednesday, March 19, 2008 4:20 AM | Feedback (0)