Blame STYLEGUIDE

Packit 47f805
* The LAME API is frozen.  Poorly designed as it is, don't change it, 
Packit 47f805
  and add to it sparingly.
Packit 47f805
Packit 47f805
* Don't take it upon yourself to go through LAME with the sole purpose
Packit 47f805
  of updating everything to match this guide.  Especially important:
Packit 47f805
  don't change the spacing, indentation, curly braces, etc, 
Packit 47f805
  in routines you did not write.
Packit 47f805
Packit 47f805
* If you want to make a change which effects many many functions,
Packit 47f805
  please check with the maintainer first.
Packit 47f805
Packit 47f805
* Respect the indentation of the author of the original function.
Packit 47f805
  If the indentation is not consistent, use 4.
Packit 47f805
Packit 47f805
* Don't use tabulators (the character with the value '\t') in source code,
Packit 47f805
  especially these with a width of unequal 8. Lame sources are using
Packit 47f805
  different sizes for tabulators.
Packit 47f805
Packit 47f805
* Don't set the macro NDEBUG in alpha versons.
Packit 47f805
  NDEBUG should be set for beta versions.
Packit 47f805
Packit 47f805
* check important assumptions with an assert()
Packit 47f805
Packit 47f805
* use int for all integer quantities.  
Packit 47f805
  LAME requires 32 bit ints, so you can assume int is at least 32 bits.  
Packit 47f805
  Don't use 'long'.  Don't use 'unsigned' unless ABSOLUTELY necessary.
Packit 47f805
  Don't use 'char' just to save bytes.  If 64 bits is required, use int64.
Packit 47f805
Packit 47f805
  Annnotation:
Packit 47f805
  ISO C calls the 64 bit int type not int64 but int64_t.
Packit 47f805
Packit 47f805
* Avoid using float or double, and instead use: (defined in machine.h).  
Packit 47f805
Packit 47f805
  FLOAT    for variables which require at least 32bits
Packit 47f805
  FLOAT8   for variables which require at least 64bits
Packit 47f805
Packit 47f805
  On some machines, 64bit will be faster than 32bit.  Also, some math
Packit 47f805
  routines require 64bit float, so setting FLOAT=float will result in a
Packit 47f805
  lot of conversions.
Packit 47f805
Packit 47f805
  Annotation (pfk):
Packit 47f805
  The new ISO C standard passed in autumn 1999 has defined new types for
Packit 47f805
  exactly this reason. There are called float_least32_t and float_least64_t
Packit 47f805
  and have at least the advantage that you not need to explain their
Packit 47f805
  meaning. 
Packit 47f805
Packit 47f805
  Annotation (mt):  
Packit 47f805
  we will adopt this convention in Jan 1, 2003.
Packit 47f805
Packit 47f805
Packit 47f805
* The internal representation of PCM samples in type 'sample_t',
Packit 47f805
  currently this is a FLOAT.
Packit 47f805
Packit 47f805
* Use SI base units. Lame mixes Hz, kHz, kbps, bps. This is mess.
Packit 47f805
Packit 47f805
  Example:
Packit 47f805
        float     wavelength_green = 555.e-9;
Packit 47f805
        unsigned  data_rate        = 128000;
Packit 47f805
        float     lowpass_freq     = 12500.;
Packit 47f805
  
Packit 47f805
  Converting between user input and internal representation should be done
Packit 47f805
  near the user interface, not in the most inner loop of an utility
Packit 47f805
  function.
Packit 47f805
Packit 47f805
----------------------------------------------------------------------------------
Packit 47f805
Edited version of the Linux Kernel Style Guide:
Packit 47f805
----------------------------------------------------------------------------------
Packit 47f805
Packit 47f805
                Chapter 1: Indentation
Packit 47f805
Packit 47f805
Respect the indentation of the author of the original function.
Packit 47f805
If the indentation is not consistent, don't change it.  If
Packit 47f805
you are so anal-retentive about these things and you can't
Packit 47f805
bare to even look at code with poor indentation, change it to 4.
Packit 47f805
Packit 47f805
Packit 47f805
                Chapter 2: Placing Braces
Packit 47f805
Packit 47f805
The other issue that always comes up in C styling is the placement of
Packit 47f805
braces.  Unlike the indent size, there are few technical reasons to
Packit 47f805
choose one placement strategy over the other, but the preferred way, as
Packit 47f805
shown to us by the prophets Kernighan and Ritchie, is to put the opening
Packit 47f805
brace last on the line, and put the closing brace first, thusly:
Packit 47f805
Packit 47f805
        if (x is true) {
Packit 47f805
                we do y
Packit 47f805
        }
Packit 47f805
Packit 47f805
However, there is one special case, namely functions: they have the
Packit 47f805
opening brace at the beginning of the next line, thus:
Packit 47f805
Packit 47f805
        int function(int x)
Packit 47f805
        {
Packit 47f805
                body of function
Packit 47f805
        }
Packit 47f805
Packit 47f805
Heretic people all over the world have claimed that this inconsistency
Packit 47f805
is ...  well ...  inconsistent, but all right-thinking people know that
Packit 47f805
(a) K&R are _right_ and (b) K&R are right.  Besides, functions are
Packit 47f805
special anyway (you can't nest them in C). 
Packit 47f805
Packit 47f805
Note that the closing brace is empty on a line of its own, _except_ in
Packit 47f805
the cases where it is followed by a continuation of the same statement,
Packit 47f805
ie a "while" in a do-statement or an "else" in an if-statement, like
Packit 47f805
this:
Packit 47f805
Packit 47f805
        do {
Packit 47f805
                body of do-loop
Packit 47f805
        } while (condition);
Packit 47f805
Packit 47f805
and
Packit 47f805
Packit 47f805
        if (x == y) {
Packit 47f805
                ..
Packit 47f805
        } else if (x > y) {
Packit 47f805
                ...
Packit 47f805
        } else {
Packit 47f805
                ....
Packit 47f805
        }
Packit 47f805
                        
Packit 47f805
Rationale: K&R. 
Packit 47f805
Packit 47f805
Also, note that this brace-placement also minimizes the number of empty
Packit 47f805
(or almost empty) lines, without any loss of readability.  Thus, as the
Packit 47f805
supply of new-lines on your screen is not a renewable resource (think
Packit 47f805
25-line terminal screens here), you have more empty lines to put
Packit 47f805
comments on. 
Packit 47f805
Packit 47f805
Packit 47f805
                Chapter 3: Naming
Packit 47f805
Packit 47f805
C is a Spartan language, and so should your naming be.  Unlike Modula-2
Packit 47f805
and Pascal programmers, C programmers do not use cute names like
Packit 47f805
ThisVariableIsATemporaryCounter.  A C programmer would call that
Packit 47f805
variable "tmp", which is much easier to write, and not the least more
Packit 47f805
difficult to understand. 
Packit 47f805
Packit 47f805
HOWEVER, while mixed-case names are frowned upon, descriptive names for
Packit 47f805
global variables are a must.  To call a global function "foo" is a
Packit 47f805
shooting offense. 
Packit 47f805
Packit 47f805
GLOBAL variables (to be used only if you _really_ need them) need to
Packit 47f805
have descriptive names, as do global functions.  If you have a function
Packit 47f805
that counts the number of active users, you should call that
Packit 47f805
"count_active_users()" or similar, you should _not_ call it "cntusr()". 
Packit 47f805
Packit 47f805
Encoding the type of a function into the name (so-called Hungarian
Packit 47f805
notation) is brain damaged - the compiler knows the types anyway and can
Packit 47f805
check those, and it only confuses the programmer.  No wonder MicroSoft
Packit 47f805
makes buggy programs. 
Packit 47f805
Packit 47f805
LOCAL variable names should be short, and to the point.  If you have
Packit 47f805
some random integer loop counter, it should probably be called "i". 
Packit 47f805
Calling it "loop_counter" is non-productive, if there is no chance of it
Packit 47f805
being mis-understood.  Similarly, "tmp" can be just about any type of
Packit 47f805
variable that is used to hold a temporary value. 
Packit 47f805
Packit 47f805
Packit 47f805
                
Packit 47f805
                Chapter 4: Functions
Packit 47f805
Packit 47f805
Document functions.  
Packit 47f805
Packit 47f805
Keep functions as modular as possible.  But don't adhere to artificial
Packit 47f805
line number limitations.  For example, lame_encode_frame() encodes a
Packit 47f805
single MP3 frame and is a long sequence of function calls.  It makes
Packit 47f805
no sense to break this into two or more routines.
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
                Chapter 5: Commenting
Packit 47f805
Packit 47f805
Comments are good, but there is also a danger of over-commenting.  NEVER
Packit 47f805
try to explain HOW your code works in a comment: it's much better to
Packit 47f805
write the code so that the _working_ is obvious, and it's a waste of
Packit 47f805
time to explain badly written code. 
Packit 47f805
Packit 47f805
Generally, you want your comments to tell WHAT your code does, not HOW. 
Packit 47f805
Also, try to avoid putting comments inside a function body: if the
Packit 47f805
function is so complex that you need to separately comment parts of it,
Packit 47f805
you should probably go back to chapter 4 for a while.  You can make
Packit 47f805
small comments to note or warn about something particularly clever (or
Packit 47f805
ugly), but try to avoid excess.  Instead, put the comments at the head
Packit 47f805
of the function, telling people what it does, and possibly WHY it does
Packit 47f805
it. 
Packit 47f805
Packit 47f805