Idioms for Programming

Ha! My Kung-Fu is the best.

Here is a list of idioms I use for various purposes. I do not claim to be the first one to discover any of them, but I haven't seen any of them being used in programs written by other people. If you have used any of the following before I did, please send an email so I can give you credit. I would also love to learn new idioms. The address is cinarus at yahoo dot com . So, here goes.

Reading/Processing Input of Unknown Size

Parsing is a very common task in a programmer's life. However, sometimes the input size is not known before reading (i.e. the end of interesting input may be marked). Here is a simple way of doing it.
int read_input(FILE *stream,      /* could be stdin for example */
               char **buf,
               int *len) {
    char tbuf[2000];              /* temporary buffer */
    int clen;                     /* current length */
    int R;                        /* bytes read at this point */
    clen= *len;
    R= fread(tbuf,sizeof(char),2000,stream);
    if (R<0)                      /* error in read */
        return 0;
    *len+= R;
    if (R<2000) {                 /* end of input reached */
        *buf= malloc(sizeof(char)*(*len + 1));
                                  /* plus 1 for the terminating \0 */
        (*buf)[*len]= 0;
    } else
        if (!read_input(stream, buf,len))
            return 0;
    memcpy(*buf + clen, tbuf, R);
    return 1;
}
By using this method you can:

Giving Help About How to Compile a Program

Sometimes a program requires some work from the user in order to compile properly. Printing a help message describing what is to be done is very useful in such cases. Here's how I do it (inside the Makefile) :
help:
        grep '^#1' Makefile
#1
#1 Compiling foo
#1
#1 Set the variables at the top of the Makefile first.
#1 Then type 'make foo'
#1

Counting Builds

This isn't an actual idiom, I have used it only once, for tracking mex's builds. However, it looks like it could be useful in many places. The following program prints a count and updates itself to print the next number upon next call. I used it for tracking mex's builds.
#!/bin/bash
count=1
outf=$0.bak
inf=$0

if [ "$1" == 'i' ]; then
	echo '#!/bin/bash' > $outf
	let count=$count+1
	echo $count
	echo count=$count >> $outf
	tail -n 16 $inf >> $outf

	chmod 755 $outf
	mv -f $outf $inf
else
	echo $count
fi
Here is how you use it:
  1. Copy this file to a writeable directory. Give it any name (let it be count for this example).
  2. Execute it as count i in order to increment the value and print the result.
  3. Execute it as count in order to just print the current value.

Balancing Block Keywords

This probably is not original at all, but I'll include it in just for the rants anyway :)

Some languages with an advanced(!) syntax do not have single-character block markers such as { and } . Instead, they have the more readable(!) begin, end, startexecutingthefollowingcodeimmediately, iteratetheloop and similar. So what I do is, I just put a little comment with a { or } in it after each such keyword. Then I just use the mex command { or } in order to go to the beginning or end of the current block. Of course, I do this only when the nesting gets too deep, or a block spans multiple screens. Here is a snippet (in the ultimate 4GL language from PROKRESS):

foreach id,name in employee do:  /* { */
    display id , name.
end /* } */
Where does the no-lock go, again? 1
Hosted by www.Geocities.ws