Friday, March 30, 2012

Comment: Yet another code smell

Today we are going to take a look, and discuss on yet another code smell named “Comment”. Sounds like some thing is been told wrong, yes you guys heard right, comment is one kind of smell. The days of well documented code with comment are gone, now time has changed and new agile wind is flowing, as a result comments are now considered as code smell.

How Comment is smell?

Who would have thought that this day will ever come, I have worked with organizations where the developers is been given extra time to put right comment on code, but its history. How comment is smell? its simple the code is not self explanatory, so the code is smelly.

“The idea is to make each and every piece of code self explanatory.”

If your code need comment, you should take careful consideration to refactor your code so that it need no comment at all.

Some Useful Example

In bellow code what ever comment you are seeing is a comment smell.

/// <summary>
/// this method gets a tag or a country
/// </summary>
/// <param name="countryName"></param>
/// <returns></returns>
public static string GetTag(string countryName)
{
//loop through the dictionary key
foreach (string tag in _dictionary.Keys)
{
//search the key
List<string> patternList = _dictionary[tag];
bool found = false;

//if the tag is found return it
found = IsFound(countryName, null, patternList);

if (found)
{
return tag;
}
}
return string.Empty;
}
/// <summary>
///
/// </summary>
/// <param name="title"></param>
/// <param name="description"></param>
/// <param name="patternList"></param>
/// <returns></returns>
private static bool IsFound(string title, string description,
IEnumerable<string> patternList)
{
//traverse in pattern list
foreach (string pattern in patternList)
{
//get rid of . and /.
string searchPattern = pattern.Replace(".", "/.");
string sourceContent = title;
//use regex to find the tag match
bool found = Regex.IsMatch(sourceContent, searchPattern, RegexOptions.IgnoreCase);

if (!found && !string.IsNullOrWhiteSpace(description))
{
sourceContent = description;
found = Regex.IsMatch(sourceContent, searchPattern);
}
if (found)
{
return true;
}
}

return false;
}



Which comment is Allowed?



  • In a function a algorithm is been implemented, for instance a tax calculation procedure, and the procedure is been implemented following a particular rule in a tax book. If you provide the name and reference of the book its not a comment.
  • Class responsibility declaration, its okay to put few lines of comment above a class, and describe its purpose.

Its all about judgment


In the end a programmers judgment matters, we would know when to comment and where not to comment, with little refactor effort we can avoid comment.

Thursday, March 8, 2012

Duplicated Code: The evil twin sister of you favorite function.

Duplicate code is one of the most common code smell we have in our codebase. Its simply we don’t care that much about reusing, we know how to reuse objects, functions, even solutions or component but we are some what careless about removing duplicate code. You would be surprised to know that what amount of duplicated code exists and what are those? Off course there are some obvious techniques to eliminate those.

Here is what Wikipedia has to say about it",

“Duplicate code is a computer programming term for a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity. Duplicate code is generally considered undesirable for a number of reasons.[1] A minimum requirement is usually applied to the quantity of code that must appear in a sequence for it to be considered duplicate rather than coincidentally similar. Sequences of duplicate code are sometimes known as code clones or just clones.”

There are two type’s of duplicate code
  1. Blatant –same code appears in more than one places (Example: two methods, two related classes, two unrelated classes, etc.)
  2. Subtle-looks like different code but serves the same purpose
The following are some examples of duplicate code.
  • character-for-character identical
  • character-for-character identical with white space characters and comments being ignored
  • token-for-token identical
  • token-for-token identical with occasional variation (i.e., insertion/deletion/modification of tokens)
  • functionally identical
How duplicate code find the way into our code base?

The process is more than simple. you knowingly introduce duplicate code, and the possible candidate is

  • copy and pest code and use.
  • write similar code (subtle duplication).
  • rather than using library re-inventing the wheel.

There could be thousands of example that can be put down here, but would not be appropriate, but we have the idea, lets go and find duplicate code and eliminate them, it’s a game and we have to play it with huge interest.

Summery

The bottom line is that we must eliminate duplicate codes, while doing coding just give a breather to your mind and try to find out if there is already a code there or not that do the same purpose, and could it be refactored in such a way so that it could be used by your case also? Most importantly while working with legacy code we need to be more careful and keep our mind open to refactor a lot to eliminate duplicate codes.

References

http://en.wikipedia.org/wiki/Duplicate_code

Wednesday, March 7, 2012

Technical Debt: Avoid it at any cost, other wise it would take all your properties.

Here is a fact that I learned from a real life problem, on of my relative took a huge loan from a bank, the risk was totally calculated but its like he is been paying the interest and principle forever, and its a never ending story, Same thing happens to software if you do technical dept. Here is what wikipedia has to say about technical dept.

“Technical debt (also known as design debt or code debt) is a neologistic metaphor referring to the eventual consequences of poor software architecture and software development within a codebase. The debt can be thought of as work that needs to be done before a particular job can be considered complete. As a change is started on a codebase, there is often the need to make other coordinated changes at the same time in other parts of the codebase or documentation. The other required, but uncompleted changes, are considered debt that must be paid at some point in the future.”

In short, while doing coding we faced a problem and we do a quick and dirty way to solved it and give a on time delivery. Later you are coming back to the same place again and again to write more dirty code to over come the scenarios its failing.

What a stupid thing to do, if only your higher stakeholders known, that, the two hour job eventually will take huge amount of development time, they would have hired people who would be monitoring so that no technical debt is been introduced in the code base.

if you want to know more about this topic please go to http://en.wikipedia.org/wiki/Technical_debt. There is a significant amount of information is there to help you out.

Suggestions

What to do?

Never ever introduce technical debt.

If a problem takes good amount to time and a significant amount of architectural change talk to your higher authorities, make them understand that this would take time and a quick and dirty solution will lead them more development effort in future.

Take calculated risk

Always follow 80/20 principle if the feature is been used by only 20 % of the users and its unlikely that this would cause any immediate breakdown.  But don’t forget to get back to it quickly and then refactor it, other wise you would soon forget about it.

General Suggestion

  • Refactor as you go deep in to the code.
  • Try to use design pattern
  • Be communicative with the team
  • Always follow good process of development (For Example : Agile: Scrum, Kanban)
  • Most important try to have some test coverage.

In an ideal development world there is no technical debt, but still there is in real world. so we got handle it correctly, until next time happy coding.

Friday, March 2, 2012

JustDecompile, how to get .net decompiled in a flash.

Alright, today we have a new point of focus, the target topic is JustDecompile, it’s a free decompile tool provided by none other then Telerik, thanks to them we have yet another awesome decompile tool in the community, most importantly, its completely free.

Anyone can download it from Telerik site http://www.telerik.com/products/decompiler.aspx. When you download the installer, it only downloads a web installer, kind of web platform installer thing.

image

After download, when you lunch the application, you would be given a choices to install wide range of tools, all that have telerik in there product line. Our focus is only “JustDecompile”. So choosing the right checkbox. You can also try some other productivity tool if you want to give it a try.

image

This is the nagging part, it ask for registration or username and password if you already have one. Since the product is free, it Should be totally free of any kind of registration. Unlike us telerik has different idea about the information collection policy.

image]

After login you the real installation will begin. Its going to download about 25 Megabyte of data. And going to take about a 5 minute or less depending on your internet bandwidth.

image

After installation lunch the application, the User interface is really good. And the tool is super simple. Just open any dll it will be added to the left side tree view, you can then open any class of your choice, one very interesting feature that catch my eye is that you can change the language, i.e. you can choose in which language you want to view the code.

image

This can be a good conversion product also.