question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion for if else comments

See original GitHub issue

Is there a perference for adding comments in the else block of an if statement ?

For example:

Case 1:

// comment about the if block
if (condition) {
  // comment about doStuff()
  doStuff();
} else {  // comment about the else block, what if this is too long ?
  // comment about doOtherStuff();
  doOtherStuff();
}

Case 2:

// comment about the if block
if (condition) {
  // comment about doStuff()
  doStuff();
} else { 
  // comment about the else block

  // comment about doOtherStuff();
  doOtherStuff();
}

Case 3:

// comment about the if block
if (condition) {
  // comment about doStuff()
  doStuff();

  // comment about the else block
} else {
  // comment about doOtherStuff();
  doOtherStuff();
}

Not sure if any of these is preferred… is there another way?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

12reactions
VictorChencommented, May 1, 2015

To be honest, I kind of prefer something like this (only when there are comments):

// comment about the if block
if (condition) {
  doStuff();
}
// comment about the else if block
else if (condition2) {
  doOtherStuff();
}
// comment about the else block
else {
  done();
}

This way, each if block is contained within {...}, instead of something like:

// comment about the if block
if (condition) {
  doStuff();

// comment about the else if block
} else if (condition2) {
  doOtherStuff();

// comment about the else block
} else {
  done();
}

It seems odd to me that the } shows up under the comment of the other if.

5reactions
KevinGimbelcommented, Apr 27, 2015

Depending on the complexity, I’d say it’s okay to use numbers to indicate comments. I’ve found it useful in CSS/Sass to document magic numbers, hacks or other stuff that may needs a bit of explanation.

A comment block above the declaration/function/block as shown below is better than long sentences inside the functions or blocks body IMO.

/*
 * [1] Here doStuff() is described.
 * [2] Explanation about the else statement and when it is expected to be executed,
 *     this comment could get really long, maybe the execution depends on previous
 *     functions or the developer likes to write detailed comments - who knows?
 * [3] Explanation of doOtherStuff() and what happens when it is called or what will be
 *     returned, if anything is returned here at all. This comment may also be long.
 */
if(condition) {
  // [1]
  doStuff();
  // [2]
} else {
  // [3]
  doOtherStuff();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a good way to comment if-else-clauses? [closed]
Try explanatory variable names · Keep your conditional blocks really short. · Call a method with a nice descriptive name if it looks...
Read more >
How should one comment an if-else structure? [duplicate]
If it is needed to comment if else statements, I prefer to describe the case what made the code reach that point. Especially...
Read more >
Best practices for writing code comments - Stack Overflow Blog
If your comment causes confusion instead of dispelling it, remove it. Rule 5: Explain unidiomatic code in comments. It's a good idea to...
Read more >
Comments - The Modern JavaScript Tutorial
There's a great rule about that: “if the code is so unclear that it requires a comment, then maybe it should be rewritten...
Read more >
Conventional Comments
Examples. Alice. @alice. suggestion: Let's avoid using this specific function… If we reference much of a function marked ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found