Oct 31, 2008

Code Metrics for a developer

Here are some code metrics each developer should be tracking all along.

Cyclomatic complexity:
Cyclomatic complexity of a program is the count of the number of linearly independent paths of execution. If the source code contained no decision points ( such as 'if' 'switch' 'while' ), the complexity would be 1, FLAT CODE, since there is only a single path through the code. If the code has a single ‘if’ statement there would be two paths through the code, one path where the ‘if’ statement is evaluated as true and one path where the ‘if’ statement is evaluated as false. So the complexity increasing with number of decision points in your code. If you have not written your decision statements properly, it will lead you into unnecessary conditions and hence complexity of the program increases.

Measuring CC tells you 2 important things.
How many ways your execution may end up. If you have higher CC, then maybe you can re-write your logic to make fewer conditions.
It tells you how many test cases will be required to achieve full code coverage.

Depth of Inheritance:
It is the measure of the level of inheritance a given solution. The deeper a class is in the hierarchy, the greater the number of methods it is likely to inherit, making it more complex to predict its behavior. As we go down inheritance graph, refined classes constitute greater design complexity, since more methods and classes are involved. Deep Inherited types are by-products of an over engineered hierarchy and one should avoid this. It causes major performance hit.

Class Coupling:
Coupling is the measurement of code independence and re-usability. A module is considered highly coupled when resident classes relies on each one of the other classes. In highly coupled cases, change in one class forces a cascading changes in other classes, classes are hard to reuse or test because of their dependence on other modules.Usage of proper Creational pattern reduces the coupling between the classes. A Class should interact with another class through a stable interface and does not need to be concerned with its internal implementation.

I think every development environment has the feature of analyzing code metrics these days. Visual Studio team suite/ system has this feature of calculating code metrics from a solution.
If you track and address these issues at the earliest, it will save you from a lot of code re-factoring.

Oct 20, 2008

Extension Methods

It's been a while working on Visual Studio 2008 but there are new features I still didn't use. Extension method is one of them.Extension methods, are a way to call static method by an instance using instance method syntax. There are occasions when you call static methods where you pass an instance as first parameter.
for e.g:
We often copy arrays as
would not it be more readable if we can invoke it like
source.Copy (Destination);
Extension methods make it possible.

But example above has hardly anything to do with the word 'Extension'. So, the main idea of a extension method is to enable developers to write a method outside its class definition (of course on requirement basis) and use it just like any instance method.

Let's say I want to extend functionality of an existing type string.
I want to add a new method IsValidPinCode, just like IsNullOrEmpty or any other pre-defined methods.I can do it easily by "EXTENDING" the type string.
For this I have to write a static method outside definition of the type.

public static bool IsValidPinCode(this string s)
bool isValid = false;
// your logic to check whether an address is valid
// and assign ‘isValid’ accordingly

return isValid;
This method must be a defined inside a static class. Note that keyword this is being used in a static method.This would be signature of an extension method. Type for which it has been extended should be preceded in parameter list with this keyword. Once I define it, I can use it all across my code. (of course within the visibility of class where this method was defined)

string str = "500082";
bool isValid = str.IsValidPinCode();

I think it could be handy sometimes but it is not something I would go about as a language feature.

Oct 18, 2008

Functional Programming

You must have seen the amount of interest that is being generated for LINQ, Lamba Expressions with the release of C# 3.0.The entire Data Access Architecture has been focused on to LINQ to SQL, LINQ to XML and LINQ to Objects and more. So what exactly is Functional programming ?

FP is a programming model that treates computation as the evaluation of mathematical function.

For e.g, if we define two functions like given below
f(x) = x^2 + x + 1
g(x,y) = x * y

A problem f (g (2, 2) ) will be evaluated by compiler as
g(2*2)^2 + g(2*2) + 1
(4)^2 + (4) + 1
16+ 4 + 1

It is a declarative way of programming, where we leave the compiler to do the evaluation as late as possible. The user is only bothered about the result and not on how it is being evaluated.The focus is never on in the state transition of the variables, so one need not bother about the side effects.

 As the functional units do not have side effects, so their orders could be reversed.
 They can be performed in parallel. (suited for parallel computing)
 They are thread safe as one does not interfere other. (Thread safety)
 Function can be evaluated as late as possible, providing composability. (late evalutation)
This gives the compiler as edge to reorder or combine the evaluation of expressions in a program.

I believe it is a very strong feature for a programming language to possess.

Top Ad 728x90