Programming (silly stuff)

[ style | nuts | hints+tips ]

On programming style

I've been known to get quite religious when it comes to programming style, but there is some good thinking behind bits of it.

Full bracing

I'm a strong believer in `full-bracing' of C/etc. constructs, e.g.:

    if (condition) {
        do_something ();

Instead of:

    if (condition)
        do_something ();

Besides just being nicer, and avoiding the `ambiguous else' problem, it also prevents unpleasantness when `do_something()' is a macro. For instance, if some included header file contained:

    #define do_something()   magic = MARKER; do_that_thing()

Then the substitution in the un-braced `if' would break the logic -- the resulting `do_that_thing()' call wouldn't be inside the `if'. Of course, the sensible macro author would define that as:

    #define do_something()  { magic = MARKER; do_that_thing(); }

This gets around the problem, but macros aren't always guarenteed to be nicely written like that..

There are a multitude more of macro-related pathological brace breakage cases. E.g.:

    #define do_something()   if (broken) { abort(); } else { do_something_else(); }

That, although it seems fairly benign, causes a problem when substituted into the brace-less `if':

    if (condition)
        if (broken) { abort(); } else { do_something_else(); }

Which `if' does the `else' belong to ..?

How to crack nuts

LanguageHow to crack nut
C Use a standard size hammer. Hit the nut square on and it will crack.
Miranda Create a list of hammers and a function which recurses the list hitting the nut as it goes
Occam Get 50000 small hammers to hit the nut simutaneously. This will provide enough energy to crack the nut.
C++ Create objects which represent the various hammer components and the nut. Define functions which will let these objects interact to crack the nut.
Java Keep hitting the nut with an inflatable hammer until it cracks (or the hammer bursts).
Assembler Define the nut as a register and the hammer as a mask. Simply twiddle one of the bits until the nut breaks.
Pascal Use a small hammer. You may need to hit the nut several times before it will crack.
Basic Do people still program in this language ?
Lambda-calculus Define a recursive function that will hit the nut until it cracks. Writing the function will take more time than executing it, so it's probably not worth it.
Lingo Create the hammer and the nut as actors and ask the hammer (nicely) to crack the nut. "Please Mr. nut, crack".
Prolog Use constraints to search all possible nut configurations for one which is cracked/broken. Additionally, use branch-and-bound to search for the configuration which is the most smashed-up nut.

Hints and Tips

Last modified: Mon Mar 22 14:18:15 2004 by Fred Barnes.