Somehow this dodo bird seems to be appropriate here...

Are the Best Coders Bad Software Engineers?

Bad software engineers aren’t always obvious. Some of them write bad code but make it look “good” or “brilliant.” Randy McDonald has shared one story that I think is a familiar one to anyone who has been around software awhile:

This contractor was quite brilliant, a coding rock star, everyone was in awe of his skill with the keyboard, and often went and sought his advice for the more esoteric coding problems.

That was the problem, no one on the project felt confident enough to challenge him on his work enough for him to back down…

Now he has left and no one likes going into his code because it is so difficult to understand. I don’t actually know the full story of why he left but I do know his attitude played a part in it.

What I’ve often experienced is that those considered to be the “best coders” are often regarded as such for dubious reasons. Common reasons include that they write more code than others, they write code faster, they can solve any problem, and they use code constructs no one else can understand.

Unfortunately all of those characteristics tend to produce complicated code more often than simple code. And complicated code resists change, lowers readability, and obscures the locations of bugs. Complicated code will cost a software organization umpteen times more what dumb code will cost.

Via XKCD
Via XKCD

So what does overcomplicated code look like?

Compare the following two blocks of code. You may not be surprised to know that these satisfy the same test cases. Yet the difference in readability is incredible.

Version 1

    List<String> getTAList(String tAs) {
        List<String> listOfTAs = new ArrayList<String>();
        //there are no separators so just add the whole string as one TA
        if(!tAs.contains(",") && !tAs.contains(";")) {
            listOfTAs.add(tAs);
        }
        else if(tAs.contains(";")) {
            if(tAs.contains(",")) {
                for(String s : tAs.split(";")) {
                    String reversedName  = s;
                    if(s.contains(",")) {
                        String[] nameParts = s.split(",");
                        reversedName = nameParts[1].trim() + " " + nameParts[0].trim();
                    }
                    listOfTAs.add(reversedName.trim());
                }
            }
            else {
                //If it doesn't contain commas just split it on semicolons and add it to the list
                for(String s : tAs.split(";")) {
                    listOfTAs.add(s.trim());
                }
            }
        }
        else if(tAs.contains(",")) {
            //if it contains commas, but after splitting on commas the names aren't at least two words long, 
            //containing spaces, then it must have been in the format Last, First and it should be rendered
            //accordingly
            if(!(tAs.split(",").length > 2)) {
                String[] nameParts = tAs.split(",");
                String reversedName = nameParts[1].trim() + " " + nameParts[0].trim();
                listOfTAs.add(reversedName);
            }
            //otherwise it must be several full names that were separated by commas
            else {
                for(String s : tAs.split(",")) {
                    listOfTAs.add(s.trim());
                }
            }
        }
        return listOfTAs;
    }

Version 2

List<String> getTAList(String tAs) {
    List<String> listOfTAs = new ArrayList<String>();
    for(String s : tAs.split(";")) {        
        if(isLastFirstFormat(s)) {
            listOfTAs.add(convertToFirstLastFormat(s));
        }
        else {
            for(String t : s.split(",")) {
                listOfTAs.add(t.trim());
            }
        }
    }
    return listOfTAs;
}

boolean isLastFirstFormat(String s) {
    return (s.contains(",") && s.split(",").length == 2);
}

String convertToFirstLastFormat(String s) {
    String[] nameParts = s.split(",");
    return nameParts[1].trim() + " " + nameParts[0].trim();
}

code_quality

What’s so bad?
The problem with the Version 1 code above isn’t functional. It actually satisfies all of the test cases provided. It can parse all the data correctly. It parses strings like nobody’s business!

But code is read ten times more often than it is written. Code is maintained, reused, and enhanced. It gets copy-pasted and then modified for other use cases elsewhere (the horror). So once sloppy code like this gets into the code base, it slows everyone else down. There’s an amplification effect that bad code has on a code base.

My point is a bad engineer causes more problems than they solve. They drag the rest of the team down since the rest of the team has to read, change, and maintain their overcomplicated code. And, sadly, these bad engineers are often the “best” coders.

Leave a Reply

Your email address will not be published. Required fields are marked *