Let's say you have:
if(condition) { i = 1; } else { i = 2; }
and you need to put comments explaining if and else blocks. What is the most widely read way to do this so that someone can easily pick them up at first glance?
I usually do it like this:
//check for condition if(condition) { i = 1; } else { //condition isn't met i = 2; }
which I find not good enough, because the comments are at different levels, so take a quick look at the if comment and the else comment will look as if it belongs to some internal structure.
Put them like this:
if(condition) { //check for condition i = 1; } else { //condition isn't met i = 2; }
doesnβt look good to me, as it seems that the whole structure is not commented out (the condition can be large and take several lines).
Something like that:
//check for condition if(condition) { i = 1; //condition isn't met } else { i = 2; }
would probably be a better style in terms of comments, but confusing as a code structure.
How do you comment on such blocks?
PS. I am not asking about the reorganization of these two lines of code, only about the style of the code and the formatting of comments.
comments coding-style
serg Apr 13 '10 at 6:11 2010-04-13 06:11
source share