[Y-devel] Technical Notes

Dave Jarvis djarvis at invoqsystems.com
Thu Feb 19 21:31:52 GMT 2004


Hi,

I guess the main point I was trying to make, which got mired in mucky 
details, was that it is better to step in at the start of a project with 
a list of guidelines and styles, rather than trying to get everyone to 
adopt it later.

This includes technical aspects like:

o Notation.

o Source formatting.  Automate this.

o Line length.  What is an unacceptable length?  Although this is very 
much a personal preference, I've seen object methods over 500 lines 
long.  I certainly don't want to be the newbie on the project 
responsible for maintaining THAT code.  If you don't set an upper 
boundary, people will do absurd things (this is a fact, not a slippery 
slope).  Personally, I find 50 lines too long -- chances are good there 
is room for refactoring, then reusing the smaller functions elsewhere. 
The point: set a "best fit" limit (i.e., try not to exceed 35 lines, or 
what have you).

o Comments.  Function comments are a must, internal comments are good. 
It's nice to know what role the functions play in the grand scheme of 
development.

o Constants.  Code with magic numbers either needs a comment or a constant.

o Avoid duplicate (and redundant) code.

There are other details, of course, like source layout, which compilers 
it must compile against (gcc v2.95, gcc v3.3, TP 6.0, etc.), how many 
warnings are acceptable (none, right? ;-), and so forth.

With respect to wrapping API calls:

        strtol( vA, NULL, 0 );
        strtol( vB, NULL, 0 );
        strtol( vC, NULL, 0 );
        strtol( vD, NULL, 0 );

Correct me if I'm wrong, but the NULL, 0 value is actually loaded into 
the code segment of memory each time it is written.  This increases the 
size of the binary, and thus the memory requirements of the system -- 
unless compilers these days are smart enough to find ALL calls to strtol 
in ALL files and reduce the code.  Whereas:

        ystrtol( vA );
        ystrtol( vB );
        ystrtol( vC );
        ystrtol( vD );

Ensures (regardless of compiler) that the NULL, 0 values are only in 
there once.  This gives the compiler the opportunity to inline as it 
sees fit (speed/memory tradeoffs).

First way, calling strtol 30 times:
$ gcc -O2 test.c
$ ll a.out
        12115 Feb 19 13:02 a.out*
$ strip a.out
$ ll a.out
          3580 Feb 19 13:02 a.out*

Second way, calling strtol 30 times via ystrtol function:
$ gcc -O2 test.c
$ ll a.out
         11907 Feb 19 13:03 a.out*
$ strip a.out
$ ll a.out
          3372 Feb 19 13:03 a.out*

Call me a miser, but if I can save an average of 6 bytes per call, 
without adversely affecting readability, I'll do it.  Especially when 
the exact same call, with nearly the exact same parameters, is being 
executed hundreds of times.  In my opinion, the difference between 
ystrtol and strtol does not require a large number of neural connections 
to figure out what's going on (especially when other wrappers, like 
ymalloc, already exist), nor does it add an excessive amount of 
complexity.  In fact, I actually prefer ystrtol for the following reasons:

        1. Less typing (typo-prone), or copy/pasting of parameters
        2. Less reading (keeps lines shorter)
        3. Faster to compile (albeit not noticeable except over time)
        4. Saves (albeit cheap) disk space (in CVS)
        5. Allows debugging (read: assertions) of all calls to "strtol"
        6. Generates smaller bytecode
        7. Allows substituting better algorithms. (I used an open_memory and 
close_memory paradigm in a project [indirect calls to malloc/free], 
noticed poor performance from malloc, so simply changed open_memory and 
close_memory to use someone else's open-source memory allocation scheme. 
  By changing two lines of code, I was able to eliminate a substantial 
amount of memory fragmentation that I didn't even realize would occur in 
the first place.  This is a Good Thing.)  "better" could mean faster, 
less memory, more portable, etc.

Like I said, it's easy to get mired in a myriad of details -- the main 
point I'm trying to make is be consistent.  This can be done by 
establishing a living programming guidelines document for the project. 
Make it required reading for developers, that stipulates the desire for 
a consistent codebase.

Sincerely,
Dave Jarvis
-- 
Invoq Systems Inc.
t. 250.380.0304
e. djarvis at invoqsystems.com
w. https://www.invoqsystems.com



More information about the Y-devel mailing list









2026 © www.Y-Windows.Org - All Rights Reserved