Skip to main content

Posts

Showing posts from October, 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…

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
Array.Copy(source,destination....);
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 str…

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
21

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.

Advantages:
 As the functional units do not have side effects, so their orders could be reversed.
 They can be performed in parallel. (suited…