Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it naughty to have a large utility file?

In my C project I have quite a large utils.c file. It is really full of many utilities of different sorts. I feel a bit naughty just stuffing different miscellaneous functions in there. For example it has some utilities related to low level stuff such as a lowercase() function, and it also has some quite sophisticated utilities such as converting to/from different colour formats.

My question is, is it very naughty to have such a large utils.c with many different types of utilities in it? Should I break it up into many different kinds of utility files? Such as graphics_utils.c and so on What do you think?

like image 905
horseyguy Avatar asked Aug 18 '09 03:08

horseyguy


3 Answers

Breaking them up into separate files based on categories (ie graphics, strings, etc.) will lead to better organization, making it easier to locate certain pieces of code, having smaller files to go through, instead of just one large file.

like image 121
DeadHead Avatar answered Nov 09 '22 22:11

DeadHead


You want to break it up, not just for organizational reasons, but because you will have many other files that depend on this one. Because everything will depend on this file, it makes this one file difficult to change because it might cause widespread breakage.

http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/

like image 7
Ken Liu Avatar answered Nov 09 '22 23:11

Ken Liu


If it's just you that will EVER maintain the stuff, it's a matter of when the complexity gets to the point where you find yourself searching for things. That would be the time to refactor and reorganize (there's a cost to reorganize, just as there's a cost to not reorganize).

If it's POSSIBLE that anyone else will maintain a project that includes your utils, you have to consider THEIR pain point when deciding when to reorganize. Theirs is MUCH lower than yours.

like image 6
JR. Avatar answered Nov 09 '22 22:11

JR.