I have a moderately long(for me at least) bash script file that that maintains a list of LAN users and the bandwidth each one uses. It is 199 lines with 11 functions and is already calling 3 different files. At most it calls 3 functions at least 1 every 5 minutes.

Should I separate each function into its own file, or is this still small enough to run in 1 file?


This is a very common question for any programming language, and the answer is surprisingly simple.

Do what makes sense and makes the program better.

Here are qualities that make a program/script better in no particular order:

  • Ease of understanding
  • Ease of maintenance
  • Ease of extension
  • Ability to be tested and debugged
  • Portability
  • Performance

If separating the code into multiple files increases any of these metrics without sacrificing the others, then you should do it. If it does not, then you should avoid it.

It is sometimes difficult to determine if these metrics are being positively affected. Furthermore many of these qualities can be subjective. To get a better perspective on your code I would encourage you to submit the code for review by your peers and senior developers in said area. Code review is one of the best tools programmers have to hone their ability to discern the above qualities.

| improve this answer | |
  • I was looking for if it was better for the memory and cpu usage, when loading functions that are not use at the time. But I did forget about these qualities, I had to learn some of them on my own. I'd give you a +1, but I don't have the reputation yet. Thank again. – Randell Feb 5 '15 at 4:03
  • A common mistake is in any programming language is premature/unnecessary optimization. Performance is just one of many metrics that make up a "good" program/script. For most code, the other metrics are typically a much higher priority. – Nathan Feb 10 '15 at 22:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.