Page 1 of 1

How to organize files

Posted: Fri Feb 02, 2007 3:31 pm
by frank
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?

Posted: Fri Feb 02, 2007 4:09 pm
by Brynet-Inc
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.

Posted: Fri Feb 02, 2007 5:52 pm
by pcmattman
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.

Posted: Sat Feb 03, 2007 8:59 am
by Combuster
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.

Posted: Sat Feb 03, 2007 1:43 pm
by frank
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.
Thats what I have been doing so far and I think that it is going well. I was just wondering what everyone else does.

Posted: Sun Feb 04, 2007 5:11 am
by Candy
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.

Posted: Sun Feb 04, 2007 5:50 am
by Solar
For C library work, one file per function, one directory per header.

When you're working on executables, stronger grouping is OK, because all the stuff ends up in a single binary anyway. ;)