Writing Friendly Code - Part 2, Valid Habits
The Clockwise Box (Valid Code)
- First, write valid HTML.
Refer to the current W3C recommendations, which are downloadable in a choice of formats. In case you are new at reading this rather mild mannered rule book, "Deprecated" means "Do not use this syntax any more".
- Close all your tags.
XML parsers always need to be told where an element starts and ends. Not only does this mean not leaving out any </p> tags, it means you need to close the tags for empty elements as well. In markup, an empty element has no content not described in the tag itself. These tags need to contain their own closers. Like so:
<br />; <hr />; <img src="path/filename" width="50" height="50" />. Don't forget the space before the slash.
- XML is case sensitive.
Use lower case tags and attributes in your code. This one will take some getting used to for a lot of us. Traditionally these have not been case sensitive, and they have for some time been returned from script queries as upper case in our favorite browsers.
- All attributes, including constants and integers, need to be enclosed in
quotes. If you've been sloppy here, tighten up.
- The final rule may be a non event if you already put styles and scripts in external files. You need to use the somewhat obscure CDATA escape sequence for characters like "<,>, and &" in included scripts. You will be fine if you src out your scripts.
And that, Ollie, as Stanley said while the piano above them got bigger and bigger, is all there is to it. It is not a lot to do, mostly changing a few habits.
Comments are welcome