Suggestion for if else comments
See original GitHub issueIs 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:
- Created 8 years ago
- Reactions:2
- Comments:10
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To be honest, I kind of prefer something like this (only when there are comments):
This way, each
if
block is contained within{...}
, instead of something like:It seems odd to me that the
}
shows up under the comment of the otherif
.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.