Code style

Dr Andy Evans

[Fullscreen]

Why

  • We think of coding as all about the computer, but it is really all about the people: users (first) but also developers.
  • For the users, we want code that runs fast and in an obvious manner (we'll come back to the latter when we look at user interfaces).
  • For developers, we want code that is easy to understand and reuse, otherwise our code will die off.

For Users: Efficiency

  • Trying to speed up the program.
  • Knowing how the JVM and compiler work and making sure you program in the way that will make it fastest.
  • For example, using switch rather than if / else if / else ladders, as switches only need one decision to be made.
  • Hard to learn because it relies on understanding how computers work.
  • We'll point out examples, and the book list has some good sources.

For developers

  • We want code that other people can pick up and understand, just incase we spontaneously combust.
  • As it happens, this is also good for us. Your own code can seem like a stranger's work in two weeks' time.
  • This comes down to clarity and comments.
  • Other coders also have an appreciation of the art involved in programming, so you should aspire to elegance in your code - it will also usually help with clarity and efficiency.

Clarity

  • You could write all your code on one line:
    
    public class HelloWorld{public static 
    void main(String args[])char H="H";char 
    e="e"; char l="l"; char o="o";{System.out.
    println(H+e+l+l+o+" W"+o+"r"+l+"d");}}
    
    
  • It would run, but would make very little sense to you or anyone else.

Clarity

  • Instead, because spaces don't matter, we can make it look clearer, and we try to simplify the how it does stuff...
    	
    public class HelloWorld {
    
        public static void main (String args[]) {
    
    		System.out.println("Hello World");
    
        }
    
    }
    
    
  • Note the amount of space, and the tabbing as we enter blocks.

Comments

  • We also put in comments in our code like this:
    	
    // This is a comment.
    
    
    using // at the start of the line. Such comments help when we come back to the code later.
  • Don't leave it until the end to tab and comment - it is useful to do it as you go along, trust me - even if it takes a bit of time.
  • I'd recommend ~third of your code should be the code itself, ~third blank lines, and ~third comments (this may seem like overkill, but we'll look at other kinds of comments later; maybe 10% for now).

Conventions

  • Coders also stick to certain conventions, like capitals for classes, and lower class for variables/objects.
  • This means you're always thinking about what you are using, and others can understand the code.
  • Most coders work in teams, but even if you don't, you need to get used to pretending you do - it will help your code.
  • Java code conventions online at the Oracle website.

Elegance

  • It's a piece of pi to write code that works, but runs badly and is impossible to understand.
  • Programmers therefore have a notion of 'elegance' - doing something in the simplest, clearest manner.

O Poesy! for thee I hold my pen//That am not yet a glorious denizen//Of thy wide heaven-Should I rather kneel//Upon some mountain-top until I feel//A glowing splendour round about me hung,//And echo back the voice of thine own tongue?//O Poesy! for thee I grasp my pen//That am not yet a glorious denizen//Of thy wide heaven; yet, to my ardent prayer,//Yield from thy sanctuary some clear air,//Smoothed for intoxication by the breath//Of flowering bays, that I may die a death//Of luxury, and my young spirit follow//The morning sun-beams to the great Apollo//Like a fresh sacrifice; or, if I can bear//The o'erwhelming sweets, 'twill bring me to the fair//Visions of all places: a bowery nook

John Keats (1795-1821)

Here's an example from poetry. This extract is from John Keats ("Sleep and Poetry"). The poor chap is desperately trying to convey the thrill of his chosen profession, but, bless his consumptive soul, he doesn't half drone on.

Mad with poetry
I stride like Chikusai
into the wind.

Matsuo Basho (1644 ~ 1694)

 

Here's an alternative on the same theme by the Japanese poet Basho. Somehow it manages to convey everything Keats wanted, but Basho was plainly not being paid per line. Chikusai was a famous quasi-fictional poet, by the way.

Course text

If there's any course text for this course, it would have to be 'On Love and Barley' A collection of Basho's poetry translated by Lucien Stryk and published by Penguin.

Review

  • Always tab appropriately and break up your code with blank lines.
  • Comment your code extensively.
  • Always stick to coding conventions and name your variables sensibly.
  • Keep your eyes open for efficiency hints.
  • Keep reading other people's code: you never stop learning to program, and you can learn a lot from seeing how other people tackle problems.