Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 1
    I can make it shorter (in C and C-like languages): if ((expand ? life_max < max_value : life_max > max_value)) life_max = max_value. Not more readable obviously, so leave it as it is. Commented Aug 20, 2021 at 13:19
  • 2
    @gnasher729 Sure, but “shorter” isn't necessarily “faster”, “simpler”, or “clearer”. I love code golfing as a sport but wouldn't tolerate it in production code (and I say this as someone who likes Perl). Commented Aug 20, 2021 at 13:22
  • Spelling it out as you did, did the trick for me. I just have issues reading code like in my OP and it always leaves me puzzled because I'm trying to optimize or figure out a shortcut. Commented Aug 20, 2021 at 13:42
  • 1
    @ChibuezeOpata focus that energy on readability. Measure readability by getting others to read your code. Commented Aug 20, 2021 at 15:09
  • 3
    @ChibuezeOpata - don't think of it in terms of optimizing or finding some clever way to shorten it, rather, think in terms of how can you rewrite the code so that the reader can immediately tell what it's trying to accomplish. In your original code, and even in amon's first example, you have to mentally parse what the conditions are, what they mean, and infer why the developer wrote the code snippet in the first place, while in amon's second example you can pretty much read the intent right away: "Oh, if expand is set, then expand if necessary, otherwise shrink if necessary". Commented Aug 20, 2021 at 21:18