Timeline for One-line functions that are called only once
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 10, 2017 at 0:56 | comment | added | bdsl | @dallin If you want to inline the code you still don't need a comment, you can put that information in a temp variable name, e.g. bool isTaxPayerEligibleForTaxRefund = (taxPayer.isFemale() ... | |
Aug 10, 2017 at 0:53 | comment | added | bdsl | @dallin this has several advantages over a comment. We spend a lot more time reading code than writing it. By using a function it's much easier for a reader to not read the implementation if they aren't interested in it, and concentrate on other stuff. The point at which the function ends is as clear as when it starts, which might not be true with a comment. Multiple languages in one file make things harder to read, it's better to make the code expressive instead of sprinkling in bits of English. | |
Jul 28, 2017 at 13:31 | history | edited | Deduplicator | CC BY-SA 3.0 |
added syntax-highlighting
|
Sep 6, 2013 at 16:09 | comment | added | Péter Török | @dallin, Bob Martin's Clean Code shows lots of semi-real life code examples. If you have too many functions in a single class, maybe your class is too big? | |
Sep 4, 2013 at 19:26 | comment | added | dallin | @PéterTörök I guess what I would really like to get my hands on is an example of a codebase that uses this design paradigm in a way that it really works. That way I think I could wrap my head around it and see its value. Every situation I've seen like this was more an example of frustration. It became a pain to follow the code and with large projects, it became an even bigger pain of organizing the code with that many functions. More often than not, I ended up thinking, "where did I put that?" It seemed to hurt my productivity just from having to keep track of and organize so many functions | |
Sep 4, 2013 at 9:24 | comment | added | Péter Török | @dallin, the above is a simplistic example. In real life, your method may contain multiple, or even lots of such chunks of code instead of a single one. Then you start to see the benefit in readability and maintainability (even if none of these chunks are needed anywhere else in your codebase - which is rarely the case in real life). Plus what you won't immediately see is that a series of short method calls is much easier to JIT optimize than one long method. | |
Sep 4, 2013 at 2:42 | comment | added | dallin | What is the benefit of doing this over putting a comment above the line that says: //Is tax payer eligible for tax refund? | |
Sep 13, 2011 at 10:27 | comment | added | spraff | Private method? NO! It's a predicate and should be nonmember gotw.ca/gotw/084.htm | |
Sep 13, 2011 at 7:04 | comment | added | user1249 | Additionally it allows you to document the function in a way that can be picked up by e.g. javadoc and be publicly visible. | |
Sep 12, 2011 at 22:14 | comment | added | Adam Robinson |
@Aditya: Nothing says that taxPayer is global in this scenario. Perhaps this class is TaxReturn , and taxPayer is an attribute.
|
|
Sep 12, 2011 at 20:22 | vote | accept | deprecated | ||
Sep 12, 2011 at 19:20 | comment | added | Winston Ewert | @Aditya, the example is deliberately contrived. | |
Sep 12, 2011 at 19:03 | comment | added | Aditya |
Isn't it better to make this a private method of the taxPayer class, rather than rely on a global taxPayer variable?
|
|
Sep 12, 2011 at 18:07 | comment | added | Anthony Pegram | A good example of what Uncle Bob would call "encapsulating conditionals." | |
Sep 12, 2011 at 15:17 | comment | added | Konamiman | +1. The magic word here is "Self-documenting code". | |
Sep 12, 2011 at 14:42 | history | edited | Péter Török | CC BY-SA 3.0 |
added 9 characters in body
|
Sep 12, 2011 at 14:36 | history | answered | Péter Török | CC BY-SA 3.0 |