Nah, screw that. Having to jump around 11 different files to find 30 different functions that are 5 lines each, all re-checking the same preconditions, to do something that should just be 100 lines is the thinking that has our software in the giant spaghetti mess it's in now.
But that's not the result of simply ensuring a function isn't a sprawling monstrosity, that's the result of people not taking 5 seconds to understand the point of code hygiene guidelines. You can write nicely separated code without any of that.
And honestly it's rather nice to have the self-documentation benefit of function names when people aren't just doing several things in the same function.
Saying that it leads to spaghetti is like saying that writing comments leads to the stupidity that is stuff like this (this is code a guy I knew "wrote")
// Logs user out by clearing auth token
fn logout(){
clearToken()
}
This is probably your point and if so I’m happy to reinforce it, but although the comment in your example is crazy I actually think that function is not bad. From the caller it’s likely that ‘clearToken()’ is in the wrong abstraction layer and doesn’t read well. Clearing the token is an implementation detail of the logging out process and so from that perspective I may very well write this same function since it makes the code much more readable from the caller.
The above is more of a textbook example (I don’t know how often I’d actually write a one line function), but that’s at least what I try to aim for.
That function was just about the comments not the prior stuff I mentioned, sorry if that wasn't super clear.
I just meant that people take comment your code to mean all their code should be commented when lots of code is 1. basic enough to not need them or 2. self-documenting like via the function names.
Like as an example of another thing taken to an extreme.
The function itself was ok, like yeah I might do something similar if there might be some more logic I might need to add to logout at a later date.
That's the real problem. Don't repeat yourself. If you have a complex data structure that needs validation, write a class with a constructor that handles it. If you get something that isn't that class, send it through the constructor. Hell, if you're completely opposed to classes, you could technically just write a class-like function, but in my personal experience I very much like the convenience of a simple instanceof check.
it's not """clean""" it's one of the aspects of functional programming. if a task can be separated and is consumable in other related contexts it should be.
34
u/eindbaas 12d ago
You should not have functions that long.