+ 4

Many short methods or few long methods?

Which way is better? In every aspect from organization to code reliability, and for heritance and stuff.

8th Sep 2019, 7:42 PM
Luis Silva
Luis Silva - avatar
3 ответов
+ 6
Luis Silva When doing code reviews, methods that are longer than average will stand out as something we need to scrutinize. We check to see if there is too much going on in the method, could it be broken up into smaller, testable, discernable parts, is the code too complicated, so on and so forth. This rarely happens with developers with much experience writing unit tests. Also, those who follow SOLID principles and Clean Code will keep their methods tighter and focus on single responsibilities as much as possible. The reasons have much to do with maintaining code with extensibility and minimizing the need to change existing code. The more going on in a function, the more likely it will need to be revised for future enhancements, which increase the risk of more issues and breaking changes.
10th Sep 2019, 6:26 AM
David Carroll
David Carroll - avatar
+ 3
It doesn't matter whether you have short or long methods. Technically you can write every program with one method. A method is needed when you have a piece of code that is repeated multiple times. This helps with readability. If you have a math equation that needs to be solved multiple times throughout the program instead of writing the equation each time put a method instead. Then call the method each time you need it.
9th Sep 2019, 12:34 AM
Adam Kollgaard
Adam Kollgaard - avatar
+ 3
I prefer many short methods as long as they are not like one line of code.
10th Sep 2019, 3:34 AM
Sonic
Sonic - avatar