June 3, 2015

Code refactoring is important

One great mind have said it well that “Change is the only constant in the life”

What is the difference between hardware and software? One aspect suggests that “Hardware is likely to have mechanical parts that are subjected to wear and tear, and will need maintenance and replacement to extend continues service. While software do not have any mechanical parts and it is not subject to wear and tear.” 

If we live in static world, this would be true, but unfortunately or rather fortunately we don’t live in static world. Like many other things, software or the environment within with it gets used changes. Change in the way business operates that uses the software i.e. requirement change, enhancements to support extended business needs, change in environment within which software was assumed to run, or any other assumption within which software was built proves to be wrong, these are some of the example why software needs regular maintenance.

In science entropy is well used word for explaining gradual decline in disorder. It is bound to happen, over the period of time till equilibrium is achieved. If I draw the analogy, well engineered code at one point is order state. In changing world demand towards software changes, and  it will be inevitable to have the situation in life cycle of the software that it would go towards disordered state. I have accepted this to be fact until proven wrong, and based of I would suggest business that depends on the software that put effort and money to bring the software back to ordered state, otherwise one is bound to pay higher cost down the line. Do talk to engineering team to understand accumulated disorder, and prioritize it in the backlog. Earlier it is done better it is.

November 21, 2008

Navigate and injecting C# code using visual studio macro

Visual studio macros are great, probably I will write this in all posts that I write for macros :). Sometime back, I need to change a fairly big CF.NET application such a way that it will log a line on each function entry and on function exit. We were tracing for cause of intermittently occurring GWES.EXE error, and we were tracing it from multiple directions. The error code of GWES was 0xC0000005, which means someone was trying to access something, which was never allocated or is being accessed after releasing it. Application was multi-threaded, and hence it was difficult to understand which thread will do what at what time.

Moving back to original point, we wanted to log entry and exit of each functions. There can be more then one way to do that, but we have selected to change each functions. And to change each functions I have selected to use developer named “VS.NET macro”.

From macro it is possible to navigate through solution and projects. So macro first needs to find out all class types from a project, and then for each class, iterate through its member and find out functions that has body. If function has body, inject the code that logs line on function entry and exit.

It was easy to find function entry, but there can be many way function ends, like exception is being thrown, or based of some condition it may execute “return”, or body of the function is ended and hence return. To accommodate such cases, we decide to use “using statement” of C#.

All right, enough of the background, have look at Macro code here

Powered by WordPress