How to organize files
How to organize files
Right now I am grouping functions that are prototyped in the same header in the same source file. For example all of the functions in stdlib.h are in stdlib.c. Is this the best way to do it? How do you all do it?
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Coding styles usually are dependant on preference, I myself have never seen a single person who organizes their code exactly the same way as another person.
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc
OpenBSD as an example has a directory named stdlib, contains files with names corresponding to the functions found in stdlib.h.
Anyway, If it works for you, Go with it.. You can always take a peak at the methods used in other projects.
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc
OpenBSD as an example has a directory named stdlib, contains files with names corresponding to the functions found in stdlib.h.
Anyway, If it works for you, Go with it.. You can always take a peak at the methods used in other projects.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
I think it depends largely on what you are doing.
The advantage of small files are that:
- less time spent updating modifications
- smaller files when linking with a static library (as each unused function does not need to be linked in)
The disadvantages are:
- more time spent on building from scratch
- lots of files
And for each component of my OS I evaluate the appropriate choice. Drivers are grouped into a limited amount of files, (as all the functions will probably be used), while the C and kernel libraries goes one-file-a-function as a standard application doesnt use the larger part of those.
Apart from that, the one-file-a-function method seems to be standard for runtime libraries.
The advantage of small files are that:
- less time spent updating modifications
- smaller files when linking with a static library (as each unused function does not need to be linked in)
The disadvantages are:
- more time spent on building from scratch
- lots of files
And for each component of my OS I evaluate the appropriate choice. Drivers are grouped into a limited amount of files, (as all the functions will probably be used), while the C and kernel libraries goes one-file-a-function as a standard application doesnt use the larger part of those.
Apart from that, the one-file-a-function method seems to be standard for runtime libraries.
Thats what I have been doing so far and I think that it is going well. I was just wondering what everyone else does.pcmattman wrote:Grouping related functions helps when you need to modify, say, the file handling routines. Instead of traversing 8 different files, you just open the group file and everything is there.
I tend to group code that seems to belong together as much as possible, while keeping file sizes down enough to not confuse readers (<500 lines). If they do get above that the code must either be very repetitious (so you can skip over chunks) or it must be split up into more files.
I've seen Solar split up each function into a file, that includes testing code for that function. That seems like a good way to integrate testing code but I haven't been able to motivate myself to do that too.
I've seen Solar split up each function into a file, that includes testing code for that function. That seems like a good way to integrate testing code but I haven't been able to motivate myself to do that too.