JoC #8: Keeping your code clean

      Always code as if the guy who ends up maintaining your code will
      be a violent psychopath who knows where you live.
          — John F. Woods

Here it is: The Joy of Code, Episode 8.

This time: housekeeping. No new functionality, but good coding practice. Enjoy.

Download video

Below is a version of the scenario to download as it is so far (at the end of this episode). If you haven’t been coding along with the previous episodes, but want to start here, use this version.

Concepts discusseddefining methods, code quality

Scenario download:

5 thoughts on “JoC #8: Keeping your code clean

  1. Pingback: Joy Of Code #8 – Keeping it clean - Compu2Learn

  2. I have recently started watching your Joy of Code series on YouTube, and I like it a lot!
    I have a question; In this episode you made a method for “tryToEatLettuce” and you called it “void tryToEatLettuce”, but all the other ones were called with a public at the front. Why was that one not called with a public in front?

  3. Hi Mateusz,

    Well spotted!

    That was a mistake, not. intentional. As you can see, the code also works without the ‘public’, but the meaning is slightly different. I haven’t yet really discussed the meaning of writing ‘public’ or ‘private’ or nothing at all in front of methods, but all three are valid and mean different things. (And there’s a fourth that we will discover later: ‘protected’.)

    All these affect how widely the method can be seen and used (from which other classes). The variant without any modifier (which I accidentally used here), should normally not be used at all. However, just by chance, in our little example it makes no difference.

    I will discuss the meaning in detail in a later video. For now, always write either ‘public’ (if the method is intended to be called from outside the class) or ‘private’ (if only called from within the same class) in front of the header.


  4. Excellent as always – I like the shortcut way of setting up comments in the Greenfoot IDE. As I’m watching these, i am thinking about the sorts of problems to set students – sort of in the vein of Karel the Robot learns Java.

  5. Wouldn’t it be more interesting if the turtle turned Greenfoot.getRandomNumber times? It only just dawned on me so I went ahead and stuck that in the code 😀

Leave a Reply

Your email address will not be published. Required fields are marked *