[Y-devel] Technical Notes

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


Hi,

I haven't had a chance to give an in-depth overview of the code, but 
since the project is still young, I would like to make the following 
suggestions, and comments:

1. Make a consistent source style.  Document it.  Don't mix tabs and 
spaces (I usually suggest 2 or 4 spaces, 8 is unwieldy).  This can be 
automated by getting CVS to launch a code formatter before entering the 
code into the repository.

2. Keep function calls commented.  This is extremely important, and even 
though it is time consuming, and nobody likes to do it (except me ;-), I 
suggest even a minimal standard (in keeping with either JavaDoc or some 
C-based documentation standard).  An example template:

/**
  * DESCRIPTION
  * At least a one-line description of what the function handles.  (Don't
  * simply repeat the name of the function, that adds no information.)
  *
  * PRECONDITIONS
  * What values must be set (could be combined with contracts), what
  * assumptions are made, etc.
  *
  * POSTCONDITIONS
  * What values were set (could be combined with contracts), what effects
  * could happen, etc.
  *
  * PARAMETERS
  * parameter - What's its function, what are valid values?
  *
  * RETURN SUCCESS
  * What values are returned if successful
  *
  * RETURN FAILURE
  * What values are returned if unsuccessful
  */

3. Way back when I was taught that for every line of code you should 
have a comment that states WHY the line of code is written.  This is 
completely different from WHAT the line of code does.  As a programmer, 
I should know that "x = 4" means "assign the value of 4 to x"; what I 
don't know is that 4 indicates the number of sides to a rectangle (in 
this case).

4. Use constants.  Already I see things like "text", "Button", 
"clicked", creeping into the code (button.c).  This is best avoided from 
the get-go.  Numbers like 0, -1, 1, and 2 don't generally need constant 
values.  However, numbers like 10, 12, and such do.  Better is to make 
them parameterized (using defaults when not available).  If they truly 
are constant values and will never change (regardless of screen 
resolution, or if a person wants round buttons by default), then comment 
what the 10 and 12 mean.

This is especially important for words.  We speak English.  Not everyone 
else in the world does.

5. Think about compiler independence.  I've written a socket-based 
application that compiles (without changes, or configuration scripts) 
on: HP-UX, Solaris, Linux, AIX, and Windows.  There are a couple if 
#ifdefs lying around, but they are usually unobtrusive and hidden in 
header files.  This might mean making the code stricter to old ANSI-C 
standards.

6. typedefs in C are the handiest things.  I can't stand pointers, and 
hate trying to read code full of 'em.  Consider:

typedef struct Socket {
        int descriptor;
        struct socket_addr_in host;
} *SocketPtr;

Now all your references to sockets can be done via the SocketPtr type, 
and you will rarely see the dreaded *.  This is a fairly standard 
mechanism that I didn't see adopted.

7. You've kept function length down to a very good size.  Keep this up! 
  As a general rule, if I find a function longer than 25 lines, it is 
time to split it up (helps for reusing code, too).  Enforce this guideline.

8. I haven't noticed much code duplication (likely because of small 
functions).  Great work!

9. You are avoiding direct calls to standard APIs, creating your own 
wrapper functions instead.  This is an incredibly good thing to do.  It 
helps for future items like:

        o debugging (since all calls to "malloc" are via ymalloc)
        o porting to other platforms
        o optimizations

In "console.c", I noticed many calls to strtol giving NULL, 0.  Consider 
writing a wrapper function "ystrtol" which provides NULL, 0 by default.

10. Limit the number of parameters per function call -- anything more 
than 5 and it you should look at creating a structure (e.g., x, y, dx, 
dy -- could be a MotionPointPtr structure which could contain two 
internal structures PointPtr point, PointPtr delta).

11. Try to limit the number of values that are passed by reference.  In 
other words, fewer "out" parameters are better.  I normally return a 
structure.  Although this means the calling function must clear the 
memory when finished, it also means that you can never accidentally 
modify existing data in strange ways (always modifying new data in 
strange ways, instead).

12. Looping backwards to 0 is often faster than looping from 0 to a 
specific value.

Thanks for reading!

Dave
-- 
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