When to break code into small functions?

I understand the important of breaking big complex code into smaller functions. But for small programs like for example: finding the player with the highest score where I need to get the players' scores, and compare them against each other. Pretty simple. Should I write a separate getScore and compareScore functions for each task or just write one big code? Is there a general rule when one should break the code into smaller functions?

Hi coutcin,

especially if you are "new" to programming with C++, I would always recommend breaking down your code! Because, as you've said yourself, being able to abstract your code and break it down really gets important when working on more complex projects.

Learning, how to break down code, really will help you in the future; even if you do not necessarily plan to work on huge projects. So, if you want a yes/no answer: YES!

Start implementing your own, broken down functions as early as possible!

Have a nice one :)
Is there a general rule when one should break the code into smaller functions?

I would say, when it makes things easier for a human to understand.

With modern compilers, you don't need to worry about the performance impact of calls to small functions as they will almost always be inlined (in the case of optimized builds.)

Andy
Last edited on
Topic archived. No new replies allowed.