Which do you prefer, and why:
Typical
if (this.sun.hidden === true &&
this.moon.visible =开发者_StackOverflow社区== false) {
print "its daytime"
}
Possible unnecessary abstraction
if (isSunHidden() === true &&
isMoonVisible() === false) {
print "its daytime"
}
Removing some syntax
if (isSunHidden() &&
isMoonVisible()) {
print "its daytime"
}
I like this one as its very readable, but also requires a hard-coded "daytime" string
if (timeOfDay() === "daytime") {
print "its daytime"
}
This is also easy to read
if (isItDaytime()) {
print "its daytime"
}
Mixes printing with time of day checking, not good
printDaytime();
Anyway, this is personal preference to a large degree, but there also good, logical reasons to prefer one over the other and I am interested in hearing those reasons - or possibly other variations of the same construct if it adds anything.
Thanks!
Of your options, I prefer:
if (isItDaytime()) {
print "its daytime"
}
This one is also not terrible:
if (timeOfDay() === "daytime") {
print "its daytime"
}
...though I would use an enumeration or similar construct instead of the literal string, like:
if (timeOfDay() === TimeOfDay.DAY_TIME) {
print "its daytime"
}
精彩评论