Avoid comments at the end of a code block.

Your block is probably too big.

If you feel the need to put comments at the end of a block of code then your block of code is probably too big. Some developers put this at the end of for and while loops to indicate which block is actually ending. If this isn’t obvious at a glance, then your block is probably too long.

Extract out the logic in the block to a method. This will make it a lot smaller, or possibly eliminate the need for a block altogether. It’s okay to have more methods, the code will be more readable this way. Plus, the new methods can be tested separately.

Also, never trust the comment you see at the end of a block. Errors in coding or later changes can turn such a comment into a lie.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: