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

FULL DISCUSSION
 

I create functions whenever I can. My main concern is readability. Testability is entirely secondary, since a lot of my functions will remain private, and not directly tested.

Some key indicators you need a new function:

  • You have a one-line comment, it's usually better as a function with good name
  • You have blocks of code separate by whitespace in a function. Chances are, those blocks do something logical, thus can be separated.
  • You need to scroll up to the function arguments. Long functions are hard to reason with.

There are several exceptions. If adding a function would make the code more complex, then I tend not to do it. I also use a lot of lambda functions lately, grouping behaviour locally and making use of the same scope.

 

You have a one-line comment, it's usually better as a function with good name

Are comments a rarity in your projects? Also, do you work on a team or alone?

code of conduct - report abuse