3 ms·
I don't like repeating function calls, if logic calls for complex conditions (which I don't think the example does) I find it better to express them in new bool
by phn 11y ago
I don't like repeating function calls, if logic calls for complex conditions (which I don't think the example does) I find it better to express them in new booleans before the if, keeping the logic contained there, and call them on a:
bool shouldDoSomethingElse = a && b;
if(a) doSomething();
if(shouldDoSomethingElse) doSomethingElse();
kind of thing.
- seanwilson 11y agoI like this technique for avoiding comments in code as the variable name describes what the condition is checking but I don't see it used often for some reason.
- RussianCow 11y agoI like it too, but I assume it's probably not used very often just because it forces you to write more boilerplate code, which some may see as "verbose".
- seanwilson 11y agoOh, I was only commenting on the use of the variable to store and label a condition. For example, I prefer: var insideEventTime = time > start && time < end; if (insideEventTime) ... To this: // When inside event time if (time > start && time < end) ... I find comments frequently become inaccurate compared to function and variable names.
- RussianCow 11y agoI understand what you're saying, but I can see certain devs having a problem with introducing extra variables only to use them in a single conditional. Not that I agree with that, but that's the only reason I can come up with.
- seanwilson 11y agoWhat's wrong with introducing an extra variable compared to introducing a comment?
- RussianCow 11y agoWell, many developers will eschew the comment as well. :)
- Chris_Newton 11y agoI think adding extra variables (functions, modules, …) with meaningful names for exposition purposes is qualitatively different to adding boilerplate. The extra variable can establish a meaningful new concept, making the code more self-documenting and thus improving readability and maintainability. The value added is similar to grouping related concepts in a namespace rather than making everything global, or to adding well-chosen comments at key points in the code for a tricky algorithm. Most experienced programmers follow the same principle routinely when it comes to naming constants instead of writing literal values all over their code. In contrast, I think “boilerplate” has connotations of something verbose that you must write every time you want to express a certain concept, even though it has no inherent value itself.