How to organize files

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply

How should I organize my source files?

Group related functions in 1 source file
17
85%
1 Source file per function and group class functions
2
10%
1 Source file per function and class functions
1
5%
 
Total votes: 20

frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

How to organize files

Post 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?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
User avatar
Combuster
Member
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:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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. ;)
Every good solution is obvious once you've found it.
Post Reply