Return to site

CODE SMELL: Long method

· coding,code-smell

Long functions are not bad per se. Functions and classes that do more than one thing (and are, as a consequence, long) are bad 👎. This smell is about jamming 📻 different responsibilities into the same entity; therefore the "multitasker" name rather than just "long function".

Why is it bad? 🤔

Multitaskers are harder 😩 to understand and change, since they require knowledge from different contexts and functionalities.

Pieces of knowledge from within multitaskers cannot be reused ♻️ if it's not extracted ⛏️.

Longer functions are naturally more complex 🤯 to work with.

How to fix it? 💡

Identify what is the core 💗 purpose of a function/ class. Everything that doesn't directly belong to that core purpose should be extracted ⛏️ into other entities 🧩.

Use good naming 🏷️ strategies for functions. A function with a good name often transmits 📨 the intent of the code without the developer having to go and check 🕵️ the function body.