Code Elegance

Are we sure that goto is dead?

Last week, during a code review I found this code (I cut some lines to keep the post short)

 

// [Cut]
case USER_AGENT:
     // [Cut]...various code....
     goto default; //we want it to be added to serverVariables
case CONTENT_TYPE:
     // [Cut]...more code (with some "if")
     goto default; //we want it to be added to serverVariables
 case USER_LANGUAGE:
     ParseUserLanguage(variableValue);
     break;
 default:
     ServerVariables.Add(variableName, variableValue);
     break;
// [Cut]...continue...

No words.

 

Technorati Tags: ,,
3 comments

The smell of Copy&Paste

I often read phrases like these “adding a new module to our application is very simple, just copy&paste some code” or “to create a new class you have to copy&paste from an old class”. In my opinion this there is something wrong about this. Copy&Paste is a smell. If you need the Copy&Paste to add some functionality to your application means that you are duplicating your code and then you will be in trouble when you need to  maintain it.

What could you do? When the second Copy&Paste happens, stop, read your code, extract a class and “encapsulate what varies“.

No comments