Writingcode
Writingcode
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Text Editors
You may use any text editor you like, but you will be writing a
lot of computer code and there are some features of Crimson
Editor that you will find very useful.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Text Editors
Desirable text-editor features for writing code:
• Parenthesis matching
• Automatic indenting
• Syntax highlighting
• Line numbering
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Good Coding Practice
HTML will be our first example of creating documents
containing computer code.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Example
<html>
<body>
<h1>An HTML Example</h1>
<form>
<input name="rb" type=checkbox value="0">
with<br>
<input name="rb" type=checkbox value="1">
some<br>
<input name="rb" type=checkbox value="2">
checkboxes<br>
<input type=submit name="submit"
value="and a submit button">
</form>
</body>
</html>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML example
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Syntax
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Elements
An HTML document is made up of elements, which are usually
a start tag and an end tag, with some content in between.
<tag>
some context
</tag>
<br>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Elements
The HTML tags “mark up” the content to indicate the structure
of a document.
<h1>This is a heading</h1>
This is normal text.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Attributes
HTML Attributes provide extra information about an HTML
Element.
<img src="auckland_uny_logo.gif"
alt="The University of Auckland">
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Rules
Elements can be nested, but should not overlap.
• This is ok ...
<tag1>
<tag2>
blah blah blah
</tag2>
</tag1>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Special Characters
Anything between a < and a > in an HTML document is
treated as a tag.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Structure of an HTML Document
There should be a DOCTYPE declaration which identifies what
version of HTML is being used.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Minimal HTML Document
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Validation
The result is that the same HTML document can look very
different in different browsers.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
HTML Validation
Web browsers are not very good for testing whether your code
is correct.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Understanding Errors
HTML Tidy will produce a report telling you about the errors in
your document.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Broken HTML Document
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transi
<html>
<head>
A Minimal HTML Document
</head>
<body>
blah blah blah ...
</body>
</html>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Good Coding Practice
There are some important principles to learn for writing any
sort of computer code:
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code Layout
Which of these two pieces of code has the better layout?
... or ...
<html>
<head>
<title>A Minimal HTML Document</title>
</head>
<body>
Your content goes here!
</body>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code Blocks
A simple rule is to indent “blocks” of code.
BEGIN
blah
blah
END
<head>
<title>A Minimal HTML Document</title>
</head>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Long Lines
Sometimes a single line of code can be very long. In such
cases, it is better to break the line and indent the continuation.
Not ...
<table border="1"
cellspacing="5"
cellpadding="5"
summary="just a table">
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code Layout
How much should you indent?
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code Layout
Which of these two pieces of code has the better layout?
<head>
<title>A Minimal HTML Document</title>
</head>
... or ...
<head>
<title>
A Minimal HTML Document
</title>
</head>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code Layout
In addition to indenting code, it is important to use white space
(spaces or empty lines) within or between expressions to
improve readability.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Using spaces
It is usually better to put spaces around operators, between
items in a list, and so on.
<table border="1"width="50%"summary="">
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Using empty lines
Empty lines can be used to separate conceptually distinct
parts of code.
<html> <html>
<head>
<title> <head>
STATS 220 <title>
</title> STATS 220
</head> </title>
<body> </head>
<h1>
STATS 220<br> <body>
</h1> <h1>
... STATS 220<br>
</h1>
...
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Commenting Code
Virtually all computer languages provide a special syntax for
inserting “comments” into code. These parts of the document
are not processed.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Commenting Code
In HTML, a comment starts with a <!-- and ends with a -->
<html>
<!-- This HTML document does nothing! -->
<head>
<title>
</title>
</head>
<body>
</body>
</html>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Commenting Code
Having no comments in your code is bad. But having too many
comments in your code is also bad.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Good Coding Practice
All computer languages have a set of syntax (punctuation)
rules which must be obeyed so that your code works correctly.
The computer can tell you whether your syntax is correct, but
you must also test whether the semantics are correct.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Good Coding Practice
The techniques for debugging code vary depending on which
computer language you are working with.
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Testing Code
Write and test your code in small pieces.
Build your code up in small steps. Get one step working before
adding more code.
<html>
<head>
<title> Check My Title works</title>
</head>
<body>
</body>
</html>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Testing Code
<html>
<head>
<title> My Title works!</title>
</head>
<body>
Now I can add the body
</body>
</html>
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Further Reading
“Code Complete” 2nd Edition, by Steve McConnell
Microsoft Press (2004)
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit