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


For me, it's about readability and DRYness (DRY = Don't Repeat Yourself).

It doesn't always have to be something that will be called in more than one place, but wherever it would help to give a piece of functionality a name, even if it's one short line of functionality, it might be time for a function.

On the other hand, too many small functions could inhibit readability too. It depends on the code I'm working with.


Great response!

Personally, I don't see repeating yourself that much of a problem. Sure, if there are 5-10 lines of code repeated across your code you should create a function and use that, but I also encountered the opposite, introducing problems by creating functions and using them everywhere.

The problem was the function itself. It was fairly short (less than 5 lines of code) and it was almost certain that it will be modified in the future, but not for all calls, only for some. That's where creating a function can bite you.

code of conduct - report abuse