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.

Required fields*

4
  • 2
    I'm already aware of the benefits of keeping a single edit point - that's why the question says "...called once". Nonetheless it's great to know about those compiler optimizations, I always thought they followed this kind of instructions literally. Commented Sep 12, 2011 at 17:16
  • Splitting out a method to test a simple condition tends to imply that a condition may be changed by changing the method. Such implication is useful when it's true, but can be dangerous when it's not. For example, suppose that code needs to divide things into lightweight objects, heavy green objects, and heavy non-green objects. Objects have a quick "seemsGreen" property which is reliable for heavy objects, but may return false positives with lightweight objects; they also have a "measureSpectralResponse" function which is slow, but will work reliably for all objects. Commented Sep 2, 2014 at 19:37
  • The fact that seemsGreen will be unreliable with lightweight objects will be irrelevant if code never cares whether lightweight objects are green. If the definition of "lightweight" changes, however, such that some non-green objects which return true for seemsGreen aren't reported as "lightweight", then such a change to the definition of "lightweight" could break the code that tests for objects being "green". In some cases, testing for green-ness and weight together in the code may make the relationship between the tests clearer than if they're separate methods. Commented Sep 2, 2014 at 19:41
  • This is also a nice example where it is absolutely clear what the code does (checks gender, number of children, age, employer) but that doesn't give us a clue about the meaning of the code. The function "getsChildCareVouchers()" might have very, very similar code. Or "nextToBeFired()" at the wrong company. Commented Oct 27, 2021 at 12:16