Learning to program sample program - past its prime, but still useful
Archive - Originally posted on "The Horse's Mouth" - 2014-12-02 08:06:51 - Graham Ellis
During the course of the day, delegates will ask "what about xxxx" and "could it do yyyy", and the example's expanded to show that. It's also expanded to illustrate the background behind the language and how certain things work.
The final result is a fabulously useful example for delegates ... and something that looks a bit past its prime for the outside reader - rather like a straggly dead flower. However, there is still some benefit in such an example, especially for pulling back during future courses for me to highlight one feature or another - so [here] from yesterday is such an example in C.
So what would I do if I was to 'refactor' this program?
• I would move to one statement per line (take out the illustration of how statements can be split between lines and put several per line
• I would move away from so many compile time constants and make values that would probably vary in the future into variables (a config file, perhaps)
• I would change the messages into something more prosaic and less fun
• I would turn the capacity into an integer variable
• I would turn type into a string
• I would remove the "program completed" message which is printed in the middle of the program (it now shows how conditional code ends with everything coming back together.
• I would add some sort of counter on my final loop to see how many times I had been round it - the user is probably going to want to know how many trains he needs to take his soccer crowd without counting output lines
• and I would certainly improve commenting!
All of our public programming courses start with an optional "learning to program in xxx" day, for delegates new to programming, or who are rusty, or who feel that formal teaching will help them. Delegates who have strong prior programming expereince in anothe language can join us instead from the second morning onwards, when we do a fast start / revision to bring the whole class together. Not only does that get the experienced newcomers running quickly without the need to sit though a slower day, but it also ensures that newcomers get a repeated thorough introduction to the vital fundamentals, and in two slighly different ways to help enforce that most critical element of the language - the basics!
Upcoming public courses - [here]. If you have a group looking to learn to program in C, C++, Java, Lua, PHP, Ruby, Perl, Python or Tcl, please ask about private courses too. Writing in early December - I'm running a public course this week, and then I have 2 weeks of private courses in the run up to Christmas. But we're open for 2015 bookings and they'll be honoured at 2014 prices if you book by 31st December.