4 ms·
I prefer code over comments userHasPermission = (loggedIn() && hasRole(ROLE_ADMIN)) if (userHasPermission) { dataIsValid = (data != null && validate(da
by Someone 10y ago
I prefer code over comments
userHasPermission = (loggedIn() && hasRole(ROLE_ADMIN))
if (userHasPermission) {
dataIsValid = (data != null && validate(data))
if (dataIsValid) {
...
}
}
That's shorter, introduces terms in reading order (readers do not have to wonder what if (loggedIn() && hasRole(ROLE_ADMIN)) means before encountering userHasPermission. Yes, you can write the comment before the if statement, but then, it tends to become longer: "check whether the user has permission to do this") and less likely to become inconsistent when code evolves.
I see such comments as symptoms of time pressure or a somewhat sloppy, but caring, programmer.
- jacquesm 10y agoThat's an interesting trick, but it does create a whole bunch of throwaway booleans. Still, I can see that if the situation gets really complex breaking it down like this can be a benefit (but not so much in the current example where it actually increases cognitive load because you will have to read more lines to figure out what is really going on rather than what the boolean name indicates, for instance, userHasPermission does not fully cover the load and if validate would return false on being sent 'null' for that data element then you could get rid of the flag altogether).