CPSC 120 | Principles of Computer Science | Spring 2013 |
Consider a paper you are writing for a course. It is certainly important that you get the English grammar correct and arrange your ideas in a logical order - without that, your reader will have no idea what you are talking about. It is similar when you write HTML or Processing code - if you don't get the syntax right and the steps arranged in the right order, the computer won't be able to correctly display your page or run your sketch.
But think back to that paper for a minute. Your professor probably also specified some formatting requirements - that the paper be double-spaced or use a certain size font or have certain margins, for example. The spacing and font and margins have nothing to do with the correctness of the paper in terms of the ideas you are expressing, but are important for making the paper easier to read. Similarly, there are established programming standards which dictate things like spacing, indentation, and naming styles in your program in order to improve understandability and readability for humans beyond simply having a correctly-executing program.
You should follow the style of the examples in the textbook and in class. Some rules are summarized below for convenience. You will lose points for lack of readability, even if your code is otherwise correct.
Put in line breaks so that lines do not extend past 80 characters. (see the section on "80 Characters, Word Wrapping, and gedit" below)
Use indentation to show nested tags. One exception is tags nested within <body> ... </body>.
Give variables and functions descriptive names of an appropriate length:
Use whitespace to enhance readability:
Use comments to explain things for the human reader:
The editor gedit supports "text wrapping", which means it looks like there are no problems with long lines because they automatically wrap when you get to the edge of the editor window - but gedit is only displaying the lines as wrapped rather than actually inserting line breaks to split the lines. This means uglyness and unreadability when someone tries to print your file or open it with a different text editor.
To fix the problem, you should do the following in gedit:
You should now see a vertical gray line near the right side of the editor window. (If you don't see it, resize your window wider.) This line marks a width of 80 characters. Keep an eye on this line as you type - if you have text that would extend past it, put in a line break as needed to keep all text on the left side.