Lecture 6 - Reading Code

SCC 111 – Reading Code

Main Notes

-            To be able to write code that’s a pleasure to read

Reading Code

Being able to read great code is just as important for a programmer as reading greater books is for a writer – Peter Norvig, Director of Research, Google Inc.

As program writers – there’s a lot we can do to make things clearer for us

1.       Treat it like a “design” problem – how you are able to write the program to be easy to read as possible

You have to write code that’s good and easy to maintain

Comments

Comments are little descriptions of the code you’ve written – a brief summary

They are point you make to make the code as easy to understand for another person

The comments the compiler ignored – as they aren’t actual code and its smart enough to recognise that

-            //: after single line

-            /* and */: for block of code

Comments you add should add value, to clear up any confusing parts, not give an essay for every single basic line of code – enough for it all to make sense

A good way to ensure consistency of comments is to do it from the beginning, plan your code out of each section, and write the comments in before, or if you prefer write the comments as you go along

Readable Code

-            Indent code contained within braces

-            Set editor to save tabs as spaces

-            Consistent indentation size

-            Don’t leave whitespace at end of lines

-            Add comments that give value to code

-            Meaningful variable names

-            Follow “camelCase” when declaring variables