4 ms·
I feel like this is only a problem if the small functions share a lot of global state. If each one acts upon its arguments and returns values without side effec
by jjnoakes 5y ago
I feel like this is only a problem if the small functions share a lot of global state. If each one acts upon its arguments and returns values without side effects, ordering is much less of an issue IMO.
- ajuc 5y agoWell, if they were one function before they probably share some state. Clean code recommends turning that function into a class and promoting the shared state from local variables into fields. After such a "refactoring" you get a nice puzzle trying to understand what exactly happens.
- virtue3 5y agoI've seen threads on this before but the "goto" (couldn' t stop myself) reaching of object oriented-ness to "solve" everything is really frustrating. I've found the single greatest contributor to more readable and maintainable code is to limit state as much as possible. Which was really hard for me to learn because it can be somewhat less efficient, and my game programmer upbringing hates it.
- JamesBarney 5y agoSometimes eliminating state can also mean increasing complexity and lines of code tremendously.
- TeMPOraL 5y agoA lot depends on what your language and its ecosystem can support. For instance, the kind of monadic stuff people do with Haskell and Scala can compress programs tremendously, but then I've worked in a codebase that tried the same things in C++ - and there, the line count expands, because the language just can't express some of the necessary concepts in a concise way.
- josephg 5y agoIn javascript I sometimes break up the behaviour of a large function by putting small internal functions inside it. Those internal functions often have side effects, mutating the state of the outer function which contains them. I find this approach a decent balance between having lots of small functions and having one big function. The result is self contained (like a function). It has the API of a function, and it can be read top to bottom. But you still get many of the readability benefits of small functions - like each of the internal methods can be named, and they’re simple and each one captures a specific thought / action.
- BigJono 5y agoIf you're calling those functions once each in a particular order then I can't possibly figure out what that does for you that whitespace and a few comments wouldn't. How does turning 100 lines of code into 120 and shuffling it out of execution order possibly make it easier to read?
- nojokes 5y agoA recommended reading https://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Signature/dp/0134757599 https://www.amazon.com/Refactoring-Improving-Existing-Addiso...
- throwawaythekey 5y agoI coded this way for a while and found it makes the code easier to read and easier to reason about. Instead of your function being func foo() { // do A.1 // do A.2 // do B.1 // do B.2 // etc... } It becomes func foo() { // do A // do B // etc... // func A()... // func B()... } When the func is doing something fairly complicated the savings can really add up. It also makes expressing some concurrency patterns easier (parallel, series etc...), I used to do this a lot back in the async.js days. The main downside seems to be less elegant automated testing from all the internal state.
- josephg 5y agoNo; I wouldn't do it if I was just calling them once each in a particular order. And I don't often use this trick for simple functions. But sometimes long functions have repeated behaviour. For example, in this case I needed to do two recursive tree walks inside this function, so each walk was expressed as an inner function which recursively called itself, and each is called once from the top level method: https://github.com/ottypes/json1/blob/05ef789cc697888802e7867f0e06d64b2b0471ba/lib/json1.ts#L422-L579 https://github.com/ottypes/json1/blob/05ef789cc697888802e786... I don't do it like this often though. The code in this file is easily the most complex code I've written in nearly 3 decades of programming. Here my ability to read and edit the code is easily the most important factor. I think this form makes the core algorithm more clear than any other way I could factor this code. I considered smearing this internal logic out over several top level methods, but I doubt that would be any easier to read.
- chii 5y ago> if they were one function before they probably share some state and this is exactly why you refactor to pull out the shared state into parameters, so that each of the "subfunctions" have zero side effects.
- nojokes 5y agoNow this is usually in my opinion not a good advice (it is like reintroduction of global variables) as unnecessary state certainly makes things more difficult to reason about. I have read the book (not very recently) and I do not recall this but perhaps I am just immune to such advice. I like his book about refactoring more than Clean Code but it introduced me to some good principles like SOLID (a good mnemonic), so I found it somewhat useful.
- jollybean 5y agoYes and no. What I find is that function boundaries have a bunch of hidden assumptions we don't think about. Especially things like exceptions. For all these utility functions are you going to check input variables, which means doing it over, over and over again. Catching exceptions everywhere etc? A function can be used for a 'narrow use case' - but - when it's actually made available to other parts of the system, it needs to be kind of more generalized. This is the problem. Is it possible that 'nested functions' could provide a solution? As in, you only call the function once, in the context of some other function, so why not physically put it there? I can have it's own stack, be tested separately if needed, but it remains exclusive to the context that it is in from a readability perspective - and you don't risk having it used for 'other things'. You could even have an editor 'collapse' the function into a single line of code, to make the longer algorithm more readable.
- JamesBarney 5y agoThe problem is abstraction isn't free. Sometimes it frees up your brain from unnecessary details and sometimes the implementation matters or the abstraction leaks. Even something as simple as Substring which is a method we use all the time and is far more clear than most helper functions I've seen in code bases. Is it Substring(string, index, length) or Substring(string, indexStart, indexEnd) What happens when you pass in "abc".Substring(0,4) do you get an exception or "abc"? What does Substring(0,-1) do? or Substring (-2,-3). What happens when you call it on null? Sometimes this matters, sometimes it doesn't.
- TeMPOraL 5y agoAlso: - Does it destructively modify the argument, or return a substring? Or both? - If it returns a substring, is it a view over the original string, or a fresh substring that doesn't share memory with the original? - If it returns a fresh substring, how does it do it? Is it smart or dumb about allocations? This almost never matters, except when it does. - How does it handle multibyte characters? Do locales impact it in any way? With the languages we have today, a big part of the function contract cannot be explicitly expressed in function signatures. And it only gets worse with more complicated tools of abstraction.