The following is an example of a simple programming "challenge":
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
Below I've thrown together a solution in less than 5 minutes, BUT, I thought to myself, experimentally, what would the code look like if I needed something more "Best Practice"...
My windows clock is slowly progressing...
Now I can create a window with a QUIT button and display the current system time (only once)...
If you want to know more about the project from the beginning search the site for winclock (or click on the time.h tag).
I've stripped out most of the project header stuff - perhaps in the future I'll just have it as an "include" so that it's in the project but since it doesn't change much I only have to look at it when I refer back to it (instead of at the top of the file ALL THE TIME!)
Ok, enough planning and review, here's the code:
So for fun I tried to "port" my code (of winclockv2.c) into Linux and compile it with gcc.
Remarkably easy since most Linux distributions come with GCC installed (in case you need to build a new application from source code... it sounds scary until you've done it once or twice and then it's easy). Just open up a text editor, paste it in, save it (getchar-loop.c). The only thing to change was my Windows "batch" file,
touch gc.sh
chmod +x gc.sh
nano gc.sh
#!/bin/bash
gcc -o $1.exe $1 -Wall -ansi
./gc.sh getchar-loop.c
Slowly working towards the final product, indeed I do see that I have a working executable at every stage (even if the steps are small and the tests are numerous)...
(The looping part does not quite work yet...)
/* 2010-01-31 john pfeiffer, MS windows clock PROGRAM DESIGN MS window with X, quit button, and current system time displayed Hour:minute:second (hh:mm:ss) HIGH LEVEL FUNCTIONS update current time see if user clicked button quit if button clicked display current time on window ORGANIC ITERATIVE BUILDS
I am trying to follow best practice as I've learned from the Mythical Man Month (Fred Brooks), the Cathedral and the Bazaar's (Eric Raymond)...
C programming seems natural to me but I always want to stretch myself a little bit so here's a "C Clock" program that will eventually evolve into a "Windows Clock" program.
So here is a modular design, released early with plenty of comments, debugging ability, and grown locally and organically:
/* 2010-01-31 john pfeiffer, MS windows clock PROGRAM DESIGN MS window with X, quit button, and current system time displayed
Windows programming is a bit ugly - you have to selectively ignore the stuff "you know". On the positive side I've figured out how to insert code as preformatted (Drupal Input Filter -> extending the "safe HTML") so this should be easier to copy paste.
Also, Notepad2 has a handy "turn tabs into spaces" that I shall start using more often...
I've built on the previous Windows Program that put some text on the screen so most of it should look familiar.