re: When do you create a function or method? VIEW POST


I don't know how to put it in words, it "feels natural" after a while, I guess there are some general guidelines:

  • 1 function should do 1 thing (when you add the 2nd one you extract it)
  • threshold of around 20LOC (it depends)
  • when you have multiple nested loops
  • when you have a switch case with more then 3-6LOC
  • when you want to reuse a small functionality (within the same module)
  • when you cannot easily understand what the function does (smelly code)
  • when you have a not-so-popular algorithm or math formula
  • when you suspect a part of the function and you want to test that

Note: These apply to procedural and OOP code.


Haven't thought of the math formula use case. One more thing to add to the list.

when you cannot easily understand what the function does (smelly code)

Do you look into the inner functions of the one that you are analyzing to see how hard it is to understand as whole? Or even better, do you look at the places where it has been used to see if it makes sense and it's not too difficult to understand just from the call?


I don't know really, I just follow some common sense rules, and usually I put myself in a junior dev shoes, would he understand this?

I was referring to inner functionalities like a sort before a search, or a normalization of a json before working with it. If that part is too ... messy I would extract it in a private function, or lambda or something to put a name on it. Like you rename a variable, I would encapsulate the piece of code.

Wouldn't it be better to just comment that code block and tell the reader what it does?

I don't know, it depends, we'll see when we get there :D

If that piece of code has also 3 temporary variables and some loops, a comment would not suffice, if is a one liner maybe.

Comments can be tricky because they usually remain outdated.

Totally agree with that last line, you are right, most of the time they do and they are really confusing.

I am mostly against comments in such cases but, creating another private function might complicate the problem even further... I think creating functions that have almost no reusability is a problem.

Although, the local lambda could be the best option there. The biggest problem is the way you read it. You have to over the lambda code until it is being used and then come back to it. Can be quite tricky.

Reusability is a rare reason for creating new functions, most of them are called for one place and used to separate the concerns and have small functions, so a lower cognitive load.

Reusability is the primary reason I create functions. Interesting to see a completely different view.

How do you deal with such complexity? I mean, from what you told me, you can have tons of functions that are hard to understand on their own. Isn't that a problem?

I think you are taking everything I say to an extreme, without a concrete example there are just guidelines.

You can read some open source code and see that re usability is not usually a concern for new functions, most would prefer a WET code than a 200LOC function.

Here are a few random examples:
cleanGlobPath function is a private function just to take the "load" off the Glob public function.
doubleCapacity function is a similar example, it can fit in the addFunction but is better to extract it.

Another reason I forgot about is when you want to protect a functionality to be overridden by an ancestor you can move it to a private function and let the function that can be overridden empty, just a proxy for the private one.

Alright. I understand now. Maybe I'm just too used to weakly typed languages. Thanks for the great examples!

Yes unfortunately in JS there is a preference to use lambda/ functions inside a public function, instead of declaring them outside, mainly because of the lack of language features (private functions ando/or modules support), but hopefully things will change in the next few years.

Anyway I don't normally bring JS in discussions about good/clean code because I haven't found yet a project or library that has one (in my oppinion). Even in large common libraries is custom to have 2-3 nested functions, 100LOC functions (random example Vue, createComponent function does at least 3-4 things, instead of 1), acute lack of defensive programming and other "bad practices".
PS: If you want to have nightmares go trough the NPM source.

I don't think large functions by themselves are really a problem. Large function that do way too many things without any structure or good flow could easily become a problem though.

Also, yeah, that createComponent function is difficult to understand... but maybe because I don't know much about VueJS

code of conduct - report abuse