xref: /third_party/lame/STYLEGUIDE (revision 159b3361)
1159b3361Sopenharmony_ci* The LAME API is frozen.  Poorly designed as it is, don't change it, 
2159b3361Sopenharmony_ci  and add to it sparingly.
3159b3361Sopenharmony_ci
4159b3361Sopenharmony_ci* Don't take it upon yourself to go through LAME with the sole purpose
5159b3361Sopenharmony_ci  of updating everything to match this guide.  Especially important:
6159b3361Sopenharmony_ci  don't change the spacing, indentation, curly braces, etc, 
7159b3361Sopenharmony_ci  in routines you did not write.
8159b3361Sopenharmony_ci
9159b3361Sopenharmony_ci* If you want to make a change which effects many many functions,
10159b3361Sopenharmony_ci  please check with the maintainer first.
11159b3361Sopenharmony_ci
12159b3361Sopenharmony_ci* Respect the indentation of the author of the original function.
13159b3361Sopenharmony_ci  If the indentation is not consistent, use 4.
14159b3361Sopenharmony_ci
15159b3361Sopenharmony_ci* Don't use tabulators (the character with the value '\t') in source code,
16159b3361Sopenharmony_ci  especially these with a width of unequal 8. Lame sources are using
17159b3361Sopenharmony_ci  different sizes for tabulators.
18159b3361Sopenharmony_ci
19159b3361Sopenharmony_ci* Don't set the macro NDEBUG in alpha versons.
20159b3361Sopenharmony_ci  NDEBUG should be set for beta versions.
21159b3361Sopenharmony_ci
22159b3361Sopenharmony_ci* check important assumptions with an assert()
23159b3361Sopenharmony_ci
24159b3361Sopenharmony_ci* use int for all integer quantities.  
25159b3361Sopenharmony_ci  LAME requires 32 bit ints, so you can assume int is at least 32 bits.  
26159b3361Sopenharmony_ci  Don't use 'long'.  Don't use 'unsigned' unless ABSOLUTELY necessary.
27159b3361Sopenharmony_ci  Don't use 'char' just to save bytes.  If 64 bits is required, use int64.
28159b3361Sopenharmony_ci
29159b3361Sopenharmony_ci  Annnotation:
30159b3361Sopenharmony_ci  ISO C calls the 64 bit int type not int64 but int64_t.
31159b3361Sopenharmony_ci
32159b3361Sopenharmony_ci* Avoid using float or double, and instead use: (defined in machine.h).  
33159b3361Sopenharmony_ci
34159b3361Sopenharmony_ci  FLOAT    for variables which require at least 32bits
35159b3361Sopenharmony_ci  FLOAT8   for variables which require at least 64bits
36159b3361Sopenharmony_ci
37159b3361Sopenharmony_ci  On some machines, 64bit will be faster than 32bit.  Also, some math
38159b3361Sopenharmony_ci  routines require 64bit float, so setting FLOAT=float will result in a
39159b3361Sopenharmony_ci  lot of conversions.
40159b3361Sopenharmony_ci
41159b3361Sopenharmony_ci  Annotation (pfk):
42159b3361Sopenharmony_ci  The new ISO C standard passed in autumn 1999 has defined new types for
43159b3361Sopenharmony_ci  exactly this reason. There are called float_least32_t and float_least64_t
44159b3361Sopenharmony_ci  and have at least the advantage that you not need to explain their
45159b3361Sopenharmony_ci  meaning. 
46159b3361Sopenharmony_ci
47159b3361Sopenharmony_ci  Annotation (mt):  
48159b3361Sopenharmony_ci  we will adopt this convention in Jan 1, 2003.
49159b3361Sopenharmony_ci
50159b3361Sopenharmony_ci
51159b3361Sopenharmony_ci* The internal representation of PCM samples in type 'sample_t',
52159b3361Sopenharmony_ci  currently this is a FLOAT.
53159b3361Sopenharmony_ci
54159b3361Sopenharmony_ci* Use SI base units. Lame mixes Hz, kHz, kbps, bps. This is mess.
55159b3361Sopenharmony_ci
56159b3361Sopenharmony_ci  Example:
57159b3361Sopenharmony_ci        float     wavelength_green = 555.e-9;
58159b3361Sopenharmony_ci        unsigned  data_rate        = 128000;
59159b3361Sopenharmony_ci        float     lowpass_freq     = 12500.;
60159b3361Sopenharmony_ci  
61159b3361Sopenharmony_ci  Converting between user input and internal representation should be done
62159b3361Sopenharmony_ci  near the user interface, not in the most inner loop of an utility
63159b3361Sopenharmony_ci  function.
64159b3361Sopenharmony_ci
65159b3361Sopenharmony_ci----------------------------------------------------------------------------------
66159b3361Sopenharmony_ciEdited version of the Linux Kernel Style Guide:
67159b3361Sopenharmony_ci----------------------------------------------------------------------------------
68159b3361Sopenharmony_ci
69159b3361Sopenharmony_ci                Chapter 1: Indentation
70159b3361Sopenharmony_ci
71159b3361Sopenharmony_ciRespect the indentation of the author of the original function.
72159b3361Sopenharmony_ciIf the indentation is not consistent, don't change it.  If
73159b3361Sopenharmony_ciyou are so anal-retentive about these things and you can't
74159b3361Sopenharmony_cibare to even look at code with poor indentation, change it to 4.
75159b3361Sopenharmony_ci
76159b3361Sopenharmony_ci
77159b3361Sopenharmony_ci                Chapter 2: Placing Braces
78159b3361Sopenharmony_ci
79159b3361Sopenharmony_ciThe other issue that always comes up in C styling is the placement of
80159b3361Sopenharmony_cibraces.  Unlike the indent size, there are few technical reasons to
81159b3361Sopenharmony_cichoose one placement strategy over the other, but the preferred way, as
82159b3361Sopenharmony_cishown to us by the prophets Kernighan and Ritchie, is to put the opening
83159b3361Sopenharmony_cibrace last on the line, and put the closing brace first, thusly:
84159b3361Sopenharmony_ci
85159b3361Sopenharmony_ci        if (x is true) {
86159b3361Sopenharmony_ci                we do y
87159b3361Sopenharmony_ci        }
88159b3361Sopenharmony_ci
89159b3361Sopenharmony_ciHowever, there is one special case, namely functions: they have the
90159b3361Sopenharmony_ciopening brace at the beginning of the next line, thus:
91159b3361Sopenharmony_ci
92159b3361Sopenharmony_ci        int function(int x)
93159b3361Sopenharmony_ci        {
94159b3361Sopenharmony_ci                body of function
95159b3361Sopenharmony_ci        }
96159b3361Sopenharmony_ci
97159b3361Sopenharmony_ciHeretic people all over the world have claimed that this inconsistency
98159b3361Sopenharmony_ciis ...  well ...  inconsistent, but all right-thinking people know that
99159b3361Sopenharmony_ci(a) K&R are _right_ and (b) K&R are right.  Besides, functions are
100159b3361Sopenharmony_cispecial anyway (you can't nest them in C). 
101159b3361Sopenharmony_ci
102159b3361Sopenharmony_ciNote that the closing brace is empty on a line of its own, _except_ in
103159b3361Sopenharmony_cithe cases where it is followed by a continuation of the same statement,
104159b3361Sopenharmony_ciie a "while" in a do-statement or an "else" in an if-statement, like
105159b3361Sopenharmony_cithis:
106159b3361Sopenharmony_ci
107159b3361Sopenharmony_ci        do {
108159b3361Sopenharmony_ci                body of do-loop
109159b3361Sopenharmony_ci        } while (condition);
110159b3361Sopenharmony_ci
111159b3361Sopenharmony_ciand
112159b3361Sopenharmony_ci
113159b3361Sopenharmony_ci        if (x == y) {
114159b3361Sopenharmony_ci                ..
115159b3361Sopenharmony_ci        } else if (x > y) {
116159b3361Sopenharmony_ci                ...
117159b3361Sopenharmony_ci        } else {
118159b3361Sopenharmony_ci                ....
119159b3361Sopenharmony_ci        }
120159b3361Sopenharmony_ci                        
121159b3361Sopenharmony_ciRationale: K&R. 
122159b3361Sopenharmony_ci
123159b3361Sopenharmony_ciAlso, note that this brace-placement also minimizes the number of empty
124159b3361Sopenharmony_ci(or almost empty) lines, without any loss of readability.  Thus, as the
125159b3361Sopenharmony_cisupply of new-lines on your screen is not a renewable resource (think
126159b3361Sopenharmony_ci25-line terminal screens here), you have more empty lines to put
127159b3361Sopenharmony_cicomments on. 
128159b3361Sopenharmony_ci
129159b3361Sopenharmony_ci
130159b3361Sopenharmony_ci                Chapter 3: Naming
131159b3361Sopenharmony_ci
132159b3361Sopenharmony_ciC is a Spartan language, and so should your naming be.  Unlike Modula-2
133159b3361Sopenharmony_ciand Pascal programmers, C programmers do not use cute names like
134159b3361Sopenharmony_ciThisVariableIsATemporaryCounter.  A C programmer would call that
135159b3361Sopenharmony_civariable "tmp", which is much easier to write, and not the least more
136159b3361Sopenharmony_cidifficult to understand. 
137159b3361Sopenharmony_ci
138159b3361Sopenharmony_ciHOWEVER, while mixed-case names are frowned upon, descriptive names for
139159b3361Sopenharmony_ciglobal variables are a must.  To call a global function "foo" is a
140159b3361Sopenharmony_cishooting offense. 
141159b3361Sopenharmony_ci
142159b3361Sopenharmony_ciGLOBAL variables (to be used only if you _really_ need them) need to
143159b3361Sopenharmony_cihave descriptive names, as do global functions.  If you have a function
144159b3361Sopenharmony_cithat counts the number of active users, you should call that
145159b3361Sopenharmony_ci"count_active_users()" or similar, you should _not_ call it "cntusr()". 
146159b3361Sopenharmony_ci
147159b3361Sopenharmony_ciEncoding the type of a function into the name (so-called Hungarian
148159b3361Sopenharmony_cinotation) is brain damaged - the compiler knows the types anyway and can
149159b3361Sopenharmony_cicheck those, and it only confuses the programmer.  No wonder MicroSoft
150159b3361Sopenharmony_cimakes buggy programs. 
151159b3361Sopenharmony_ci
152159b3361Sopenharmony_ciLOCAL variable names should be short, and to the point.  If you have
153159b3361Sopenharmony_cisome random integer loop counter, it should probably be called "i". 
154159b3361Sopenharmony_ciCalling it "loop_counter" is non-productive, if there is no chance of it
155159b3361Sopenharmony_cibeing mis-understood.  Similarly, "tmp" can be just about any type of
156159b3361Sopenharmony_civariable that is used to hold a temporary value. 
157159b3361Sopenharmony_ci
158159b3361Sopenharmony_ci
159159b3361Sopenharmony_ci                
160159b3361Sopenharmony_ci                Chapter 4: Functions
161159b3361Sopenharmony_ci
162159b3361Sopenharmony_ciDocument functions.  
163159b3361Sopenharmony_ci
164159b3361Sopenharmony_ciKeep functions as modular as possible.  But don't adhere to artificial
165159b3361Sopenharmony_ciline number limitations.  For example, lame_encode_frame() encodes a
166159b3361Sopenharmony_cisingle MP3 frame and is a long sequence of function calls.  It makes
167159b3361Sopenharmony_cino sense to break this into two or more routines.
168159b3361Sopenharmony_ci
169159b3361Sopenharmony_ci
170159b3361Sopenharmony_ci
171159b3361Sopenharmony_ci                Chapter 5: Commenting
172159b3361Sopenharmony_ci
173159b3361Sopenharmony_ciComments are good, but there is also a danger of over-commenting.  NEVER
174159b3361Sopenharmony_citry to explain HOW your code works in a comment: it's much better to
175159b3361Sopenharmony_ciwrite the code so that the _working_ is obvious, and it's a waste of
176159b3361Sopenharmony_citime to explain badly written code. 
177159b3361Sopenharmony_ci
178159b3361Sopenharmony_ciGenerally, you want your comments to tell WHAT your code does, not HOW. 
179159b3361Sopenharmony_ciAlso, try to avoid putting comments inside a function body: if the
180159b3361Sopenharmony_cifunction is so complex that you need to separately comment parts of it,
181159b3361Sopenharmony_ciyou should probably go back to chapter 4 for a while.  You can make
182159b3361Sopenharmony_cismall comments to note or warn about something particularly clever (or
183159b3361Sopenharmony_ciugly), but try to avoid excess.  Instead, put the comments at the head
184159b3361Sopenharmony_ciof the function, telling people what it does, and possibly WHY it does
185159b3361Sopenharmony_ciit. 
186159b3361Sopenharmony_ci
187159b3361Sopenharmony_ci
188