[Y-devel] Technical Notes
Maurice van der Pot griffon26 at kfk4ever.com
Thu Feb 19 20:02:20 GMT 2004
I'm not involved in this project (yet) and I also wouldn't want to help
create a big discussion about something that in most cases is simply a
matter of taste.
Having said that, I would like to add some comments to a few of your
remarks. If I don't comment on something I either don't have a preference
or I agree with you ;)
If anyone thinks I'm stating the obvious, please ignore me.
On Thu, Feb 19, 2004 at 11:21:35AM -0800, Dave Jarvis wrote:
>
> 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.
Usually most short pieces of code can be understood by programmers without
the help of comments, but to facilitate easy reading of large pieces of
code I often comment blocks of code (like loops) with a single sentence
explaining what they do. Only if it is not immediately obvious why I would
want it to do that, I add a few lines describing that.
If you have a piece of code that isn't readable by itself, don't fix it by
adding a comment, try to fix it by making it simpler (divide and conquer
usually works).
> 4. ... Numbers like 0, -1, 1, and 2 don't generally need constant
> values. However, numbers like 10, 12, and such do. ...
> 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.
I suppose it is a personal preference of mine, but I make it a point not
to have _ANY_ magic numbers in the code. Of course everybody who reads
3600 immediately thinks of it as 60*60 or the number of seconds in an hour,
but still defining SECONDS_PER_HOUR or something similar somewhere doesn't
hurt. Using meaningful names also helps increase the speed with which one
can read the code.
> 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.
In my experience using an OSA (Operating Abstraction Layer) that gathers
all #ifdefs together in a few source/header files can really help keep
the code clean. The smaller the code affected by definitions of
preprocessor definitions, the easier it is to test it in isolation with
all possible combinations of definitions.
> 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.
You are only hiding a * in the name of a type. It also means you'd have
to define one extra type for each level of indirection you use for any
type. I don't mind the * at all, I'd just define Socket and if a variable
is a pointer to something, I prefix it with a p. For pointers to pointers,
I prefix with pp, etc.
While on the subject of prefixes, I never prefix any variables with the
letter of their type. The fact that a variable contains a pointer actually
helps when reading the code while it does not bother the programmer when
things have to change types or names. Prefixing boolean variables with b
for example is in my opinion a convention that has no advantages as far
as reading the code is concerned and the disadvantage of having to rename
your variables if the boolean suddenly needs to be an enum.
> 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.
I don't think you can say that a function needs to be split up just because
it happens to be, say, 50 lines. If the code does nothing complex, doesn't
contain loops and walks through a large number of pretty simple steps,
why would you want to split it up?
As long as the function can be thought of as performing one logical function,
the reader can browse the function once to see it really does what it is
supposed to and from then on just think of the logical function whenever
he sees a call.
> 9. ...
> In "console.c", I noticed many calls to strtol giving NULL, 0. Consider
> writing a wrapper function "ystrtol" which provides NULL, 0 by default.
Why?
> 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).
Although limiting the number of "out" parameters is a good thing, putting
unrelated items together in a struct just to return them as one piece of
data is a Bad Thing.
> 12. Looping backwards to 0 is often faster than looping from 0 to a
> specific value.
I think the difference is negligible comparing it to the stuff in the
loop. Also, optimisations like that can easily be done later on. If looping
up from 0 is easier to understand, it decreases the chance of introducing
bugs.
Best regards,
Maurice van der Pot
More information about the Y-devel mailing list
2026 © www.Y-Windows.Org - All Rights Reserved