每当我用任何一种语言编写典型的if-else-construct时,我都想知道(在可读性和概述方面)向其添加注释的最佳方法是什么。特别是在评论else子句时,这些评论总是让我觉得不合时宜。假设我们有一个这样的结构(示例用PHP编写):
if ($big == true) {
bigMagic();
} else {
smallMagic()
}
我可以这样评论:
// check, what kind of magic should happen
if ($big == true) {
// do some big magic stuff
bigMagic();
} else {
// small magic is enough
smallMagic()
}
要么
// check, what kind of magic should happen
// do some big magic stuff
if ($big == true) {
bigMagic();
}
// small magic is enough
else {
smallMagic()
}
要么
// check, what kind of magic should happen
// if: do some big magic stuff
// else: small magic is enough
if ($big == true) {
bigMagic();
} else {
smallMagic()
}
您对此发表评论的最佳做法是什么?
else { // for future reader: sorry, at the moment of writing this I did not have time and skills to come up with a better way to express my logic