How many ‘i’ are there in “Liverpool”

One of our favorite annoyances in programming education is the useless or even stupid exercises we hand out to our students. This is especially annoying given that we, the teachers or educators or whatever we want to be called, claim that the students are lazy when we’re the really lazy ones not bothering to write proper exercises.

So, enough is enough.  We must stop writing all these stupid (beyond belief) exercises.

An example of stupid exercise

Take a look at the exercise below. It is a version of typical exercises for students learning how to program in C, but it applies to Java and other languages as well.

Ok, to the exercise:

Write a program that counts the number of ‘i’ in “Liverpool”

Why isn’t that a good exercise? Let’s discuss this shortly:

  1. Why write a program for this when everyone already knows the answer? Can you honestly say that there exists a student that can’t answer this wothout writing a program?  .….the exercise is boring and useless.
  2. You’re solving a very specific problem with an equally specific program. …the exercise is not general.

In defense

We (this blog’s authors) have learnt from experience that there seems to exist a, sort of, catch-all explanation to the existance of exercises (and all other things in programming education) like these. The phrase goess:

It’s a classic!

OME (Oh my Emacs)! How could this possibly be a motivation behind anything? It used to be a classic that the world was flat. Is it still a good explanation? We will tag all our posts dealing with this stupid and ignorant explanation with the tag “classic“. If you ever motivate anything with “it’s a classic” chances are you should remove it.

Towards a better exercise

Ok, if the two of us are so damn good (actually we’re not claiming we’re good but we are claiming that we’re trying) what is our take on this?

We’re not saying that the exercise above is 100% useless. Ok, as it is written above it is probably 99% useless and stinks. But fact is, we believe it serves as a nice starting point for a larger exercise with a progression. Here are some suggestions on what can be done to make the exercise more useful. Let’s turn the exercise into several exercises (in progression):

  1. Write a program (with a main function) that counts the number of ‘i’ in “Liverpool”
  2. Put the code doing the count in a separate function. 
  3. Make the function general – any letter in “Liverpool”
  4. Make the function more general – any letter in any text
  5. Make the function even more general – any (sub)string in any string
  6. Make use of existing API functions
  7. Make the function robust
  8. Put the function in one file and the main function in another
  9. Write tests
  10. Write a function printing out the lines containing the string “liverpool”. This is not general – but a useful start. 
  11. Write a function printing out the lines, in a file, containing any string., using the function above. This is not general enough – still rather useful. 
  12. Write a function printing out the lines, as read from stdin (a stream) or a file, containing any string

Compare the program with the grep command. The student now has her/his own version of the program grep. 

Note: in some languages the word function above should be replaced by method. 

Why is this so much better?

Simply because:

  • the resulting code is more general (and quite useful)
  • the code can be reused (since put in a function)
  • the code is tested
  • the student has implicitly been taught and practiced HOW TO go from a specific problem to a general solution

Extending the exercise even more

We could add even more tasks for the students

  1. Use a versioning control system (e g git)
  2. Add automatic build software (e g make, ant, gradle) to control the building/compilation
  3. Add © notice
  4. Add license
  5. Comment the code
  6. Write API manual (javadoc, doxygen…)
  7. Check code coverage on our tests
  8. …..

We try hard to avoid exercises like the above in the books we (the authors behind this blog) are writing:

The books come together with videos:

The books and videos are public and licensed under a free license so feel free to use.

4 thoughts on “How many ‘i’ are there in “Liverpool””

  1. Nice post. Usually when I do personal useful projects at home they start with ambition “I’m gonna do it right this time” but after spending some time with code, fixing, debugging, re-writting etc I manage the code to do exactly what I wanted. At that point the code looks like exercise version 1. And any attempt to bring it to version 2 (with all the steps) dies on step number 2.
    As an example of such exercise for me was the creation of a course on ‘swedish language for job seekers’ which would contain all the swedish words one would need to find a job in Sweden. To collect the words I was parsing job ads on arbetsformedlingen website and aggregating most used words longer than 4 letters 🙂


    1. Thanks.

      Some of my projects contain an aweful amount of crappy code and I, and my users, will have to live with that. Some of the crap was done because I am lazy, some because I didn’t have the time and most likely some of if is done because I suck.

      Our argument is that in a teaching role you have to be a role model. To show students how it should be done. By doing this we hopefully make newbie programmers aware of the fact that you CAN write programs better. If you’ve never seen a good program (good with regards to structure, generality etc) it will be almost impossible to imagine what it is.

      For me personally I’ve felt bad about my teaching for many years. Something alwas felt wrong. I started questioning myself and I found tons of things to improve (that is I think I’ve sucked a lot over the years). The above example is only one of the examples where I’ve done bad as a teacher.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s