Sunday, November 15, 2009

The Criminal Coder


Mr. Reader, in case you have read my previous posts (in case, is the key phrase here), you might be by now thinking me to have lost my psyche. How can I, the ubiquitous attorney of coders on this planet (and Mars) call them evil? Well, to clear out doubts, they are criminals when compared to their superiors (read developers).

How can anyone deglamorise the developer by comparing him with a coder? To me coders are beginners, not absolute beginners, but people who know stuffs, but simply skip to do them, the way it is meant to be done, for laziness or for reasons I fail to understand. It’s not the lack of intellectual capacity that causes them to commit mistakes (read crime), its rather their sluggishness, that makes them do what they want to do, even if that brings about compromise in terms of the code they inject to dilute (read pollute) the system.

The following are some snippets from production code(basic changes made to trim the demo) that I have encountered, in this short span of life (read professional life), and when debugging performance problems in applications, stumbled upon them, and literally felt like giving digital life imprisonment to those who had the audacity to commit these bits!

  • Object declarations inside loops: For simple objects I sometimes feel it’s unavoidable to declare objects of types inside a loop, but for complex types, it’s simply a crime. The following code was intended to improve the performance of the application by spawning new threads. Don’t find the need to say more, the code is self explanatory (and yes, gives a boost to performance):


    public  void UploadXML()
    {
    DataTable dt = GetDataTableFromXML();
    for(int i=0; i<dt.Rows.Count; i++)
    {
    Thread th = new Thread(…);
    th.Start();
    }
    }
    Guess what will be the first problem that your app may run into? An OutOfMemoryException.


  • Unacceptable usage of If construct: The code is overly expressive of my intent.


    for(int i=0; i<1000; i++)
    {
    if(IsPostBack==false)
    {
    //Do something here
    }
    }

  • Overly expressive logic: I didn’t have the courage to congratulate this guy and kept shut even after seeing this. There were 10 checkboxes in the page and all of them had this. All of them!


    //As if Checked return a float
    bool b = (checkBox.Checked==true? true: false);
    And I have seen even experienced guys do a


    .ToString()
    on a string. What more do you expect from visual studio? Auto-detect these overly expressive conversions? Surely, it would do so, may be in VS 2014!


  • Global declarations in methods: Why not lazy declare and initialize at the point of usage? We are not in the C age any more (or are we?).


    public void Foo()
    {
    int a, b, c, d;
    //50 LOC before using c and d
    c=c+1;
    }

  • Avoiding short circuits: Short circuits were provided in the language for some reasons, why not use them to decrease the number of characters typed and let the compiler expand them?


    //Enjoy...
    bool a = false;
    bool b = true;

    //do something with b here
    if(a ==false)
    {
    //this is left blank (and yes, intentionally)
    }
    else
    {
    if(b == true)
    {
    //Some code to execute
    }
    else
    {
    //Again left blank (intentionally ofcourse)
    }
    }
    Why is the following so uncommon to write amongst few geniuses (strict pun intended)?


    if(a == true && b==true)
    {
    //Some code to execute
    }

    I know you might be thinking who on earth will follow the above pattern and dilute the code, but I have seen it far too many places to avoid mentioning it. Fact is, some coders simply forget the fact that an if can stay without an else. There are innumerable examples and I will certainly update this post to show some really valid places for short circuits, which were completely skipped (and did i mention, knowingly?).

The above list can’t ever be exhaustive, since each coder has his own pattern of seeing the code. At times I tend to give them (the coders) some leniency by considering their experience with a particular language, but most times, it’s just unacceptable and unforgiveable.

The following is a beautiful extract about the types of competence (or incompetence) that we encounter:
  • Unconscious incompetence: You don’t know what you don’t know.
  • Conscious incompetence: You know what you don’t know.
  • Conscious competence: You know how to do it, but you have to think your way through it.
  • Unconscious competence: You can do it without thinking. You just know what to do.


By the way, the reason I termed the above as crime, was because all those coders had live code at their disposal while adding these bits. They simply ignored the style of existing code used in the application and were arrogant enough to not tilt from their style of coding.

To me, that is simply unacceptable and unforgivable crime!

Hail Visual Studio! Save us from the criminal coders!

No comments:

Post a Comment