Thu 2 Aug 2007
Spot-welding Code: Tips for Maintaining Consistency
Posted by Dennis B under Productivity, Programming
[5] Comments
Nearly all programmers are passionate about one thing — their coding style. This is kind of ironic considering this is a populace of people who, with few exceptions, could pretty much care less about style in any other aspect of their life — clothing, home decor, personal hygiene…
When I fix a bug in someone else’s code, sometimes I cringe at their coding style or lack thereof. On occasion, I find myself in some code that I can genuinely admire the clarity and attention to detail. I like to think of fixing bugs, in your own code, or in someone else’s, as a kind of spot-welding. The goal is to put in a clinically minimal change to fix the problem.
Here are a few tips for creating a clean, but strong spot-weld.
a) Adapt to the existing code style — whether good or bad
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13 int foobar(int *arr[], int n) {
int i,count=0;
for (i=0; i<n; i++) {
if ( arr[i] == 0 ) {
++count;
}
if(arr[i]<0)/*fix*/
{
break;
}
}
return count;
}
Different brace style, indentation, spacing around punctuation, and a nearly worthless comment, “fix”. The coder who put in this fix put his ego and love for his style ahead of consistency. One thing you will find about any programmer’s style: no matter what the brace style, no matter what the spacing preferences, in general, inconsistency is ugly.
b) Adapt to the existing naming conventions — whether good or bad
Granted, some people’s naming conventions are difficult, nay, impossible to recognize, but there are simple facets that can always be mimicked to improve consistency. Do they use mixed case? How do they use underscores? Do they use a naming prefix pattern? Do they use a naming suffix? What are some of the root words that represent the concepts being worked with?
Making your changes mesh with the existing conventions — whether good or bad — will make your changes fit in with the code around it and it shows a degree of professionalism. The code’s original author is less likely to doubt the quality of your change if it fits in.
c) Adapt to the existing structures and coding constructs
Example: You may use exception handling in all of your code. Programmer X may use return codes. When you do a fix in programmer X’s code, you should adapt to his constructs, even if you think they are archaic, because consistent, archaic techniques are better than inconsistent application of two very different techniques.
Another example: Don’t write a class to fix a bug in code that doesn’t use any classes, unless it is the only solution. Again, think of the concept of a spot-weld.
d) Beautify the code, but don’t check in the beautifications
If you just can not read a piece of code because of it’s style, go ahead and beautify it. But once you have a fix, revert back to the original style and merge your fix back in. This keeps the amount of change to a minimum which will help the next person who has to review the change history of that code.
e) Leave a note behind
When you fix a bug, leave your initials and the date with a short comment saying, “heh, I was here, I did this, and it was supposed to fix something.” That will save other people who see the code time when they are wondering who to blame (or who to give credit to). This is one area where I think it is ok to diverge from the existing code style. You should leave a note behind even if it is the only comment in the entire code base. You can think of it as good manners.
August 2nd, 2007 at 1:29 pm
I agree with most of this, but I disagree with your last suggestion. Don’t leave a note. If you do, the code will eventually be overshadowed by notes and it’ll be impossible to figure out which notes are still valid and which are historical.
Your version control system will be able to keep track of historical information. Use it. If you aren’t using one yet, start NOW.
August 2nd, 2007 at 4:37 pm
Good post.
I agree with the comment above about notes. We’ve got good version control practices now, and it allows us to be very aggressive with keeping code clean when it undergoes change. We don’t comment out code we’re getting rid of — we delete it. Someone can find it in VCS if he needs it. I put notes in for things that still need to be done (i.e. TODOs), not for things that have been done.
With regard to your comment about beautification: If I do make changes to “beautify” a file, or any similar non-functional change, I make sure I commit it in a separate change set from functional changes. For the reason you give — otherwise it’s too hard to see the functional changes amongst all the formatting changes.
Regards
John Hurst
August 3rd, 2007 at 9:29 am
I agree that version control systems have improved, and everyone should be using them. And yes, to an extent, they can supplant leaving “kilroy was here” notes behind. That is provided developers actually enter useful checkin comments.
However, when I am reading source code and I see a change note, I have no need to look any further. I find that very convenient. In my opinion, the locality of reference outweighs the inconvenience of cluttering the code.
The second issue is with branching and version control. To my knowledge, there is still only one VC system that tracks branch merge points (Clearcase). The most popular open source VC systems, CVS and Subversion, while they have good branching capabilities, do not track merge points.
Why is this a problem? Because when I use version control (the annotate command) to track down a line of code to a revision number, 75% of the time, it points to a checkin containing the comment “merge branch XXX to trunk”. Then I have to try again to find the *real* change on that branch (usually a bug-fixing branch) and see who is responsible.
Also, most annotate commands do not help you find deleted code at all. You have to do a binary search diffing previous versions of the code until you can figure out when those critical lines disappeared. And unless you are extremely familiar with the code, how do you know those lines ever were there in the first place? All you know is that it doesn’t do something that it used to do.
Now, if a development group wants the best of both worlds, they should use SlickEdit 2007’s code annotation tools. With it, you can leave sticky notes all over the place without cluttering the code. It would be great if version control systems integrated this concept, so you could annotate individual changes with checkin comments, maintaining locality of reference, and making the annotations persistent with your source base. Until then, keep looking for my little notes
// DJB – 08-02-2007
// broke something that worked before
August 6th, 2007 at 1:34 am
Code littered with comments about who changed what and where (often accompanied by commented out lines of code to show what was there before if things were removed or changed rather than added) make code harder to read.
Not so bad when it’s a few spot changes here and there but over time such changes tend to pile up to the point where every single line has been changed multiple times, leading to maybe a dozen lines of change comments for each line of code.
I’ve seen it happen, the situation had become unworkable.
We agreed to cut out ALL those change comments, but someone higher up decided that wasn’t good. We were instead to leave the change comments related to the last 2 major releases in place.
So now we had less comments but a lot more bookkeeping as we had to constantly check when encountering a change comment (did I tell you this had to be done for the entire file whenever we changed a file?) whether it should be deleted or not.
How old was it? What version did that correspond to? Was that more than 2 major versions ago or not?
Better to rely on version control for such things (everything was also in version control, with mandatory change history also in the header of each file as well as version control checkin comments).
You can always leave a little remark stating what something does if it appears weird but is a necessary construct to make something work.
June 2nd, 2008 at 11:10 pm
Is a welding procedure qualfication required as per any international codes, if yes then which code/standard is this..its an offshore job