Is it really necessary to validate the code?
It’s the mania of those who tend to perfection and write the code with almost a maniacal accuracy, to see the green check mark that indicates that the code of the web page newly developed passed the validation control. But what does validating a code mean? And why is convenient validate the code? And yet, is it always necessary to pass the validation or there can be some exceptions?
First things first, in this article we’ll try to answer to all the interrogatives posted and with which many developers find themselves facing everyday.
What does writing a valid or standard code mean?
Let’s starting saying what is meant for “writing a valid or standard code“.
A bit of history
There’s an international no-profit organization, composed of a group of experts that have been working years on standardizing the languages and technologies for the web. Such organization is called World Wide Web Consortium (or W3C).
The web standards
The W3C in the course of the years has established the standards that have the task to define the syntax and the markers (tag) to use in the creation of any kind of web document. Therefore following these standards web contents are more usable to all the users regardless the type of browser used and in some cases (if even the accessibility guidelines are respected) also by the interpreter in use (for example, normal browsers, browsers based on devices of vocal syntax, mobile phones, personal computers for auto, etc.)
How can one write a valid code?
We just said that writing a valid code means to respect the standards defined by W3C. But how can one write a valid code?
When a web page gets developed, the first thing that is indicated in the document is the Doctype (Document Type Definition) that is necessary to inform the browser the language with which the document is edited.
To write a valid code it’s necessary to check that the markers (tag) and the attributes used fully respect the syntax defined in the Doctype declared at the beginning of the document.
Once our web document is created, how can we verify that we have written a standard code?
How to be sure that a valid code has been written?
To control the validity of one’s code, we can use several softwares – like the Markup Validation Service offered by the W3C - that automatically controls the correctness and the respect of the standards of the document you want to test.
The validator provided by the W3C, besides indicating eventually the errors made during the creation of the web document, provides a valid help in the correction of such errors, reporting the code line where the error is and describing also the kind of error. Obviously to understand and correct such errors it’s necessary to comprehend the code in order to modify at the occurrence the source code of the document.
A Firefox extension for the validation of the code in real time
Validating the code of your web document on the validator’s site provided by W3C involves some complications, for example it’s not possible to monitor the validation during the development of the code, unless one uploads the web document on the W3C ‘s valuator at every new code line edited, with a great lost of time.
If you want you can resolve this problem by installing HTML Validator, an extension for the Firefox Open Souce browser which provides all the tools necessary for the validation of the web pages directly in the window of the browser. The plugin is based on two algorithms developed by the W3C, Tidy and OpenSP.
Using this extension every time you surf any kind of web document, the result of the validation of the document is shown directly in the window of the browser, on the bottom right, as shown in the picture above.
What does writing a valid code involve?
Obviously writing a standard code involves some advantages, among the other things, the web document will be easily usable by all users, regardless of the browser and it shouldn’t be overlooked also the “indexation” factor as the search engine spiders could have problems properly indexing web documents containing certain markup errors.
Is a validated code always synonymous of a good code?
Attention of being fooled by the word “validated” and the positive result returned by the W3C valuator.
The code of a web document can also be validated but this doesn’t mean that this code is a good code as there’re several factors that a valuator, since we’re talking about a software, may not assess correctly.
For example web documents affected by “classness” or “divness” can also be valuated if the markers used for the creation of the web document have been used correctly, the valuator will however return a valid code. But this doesn’t mean that it’s a good code.
Let’s immediately do an example to try to better understand what we’re saying:
<div id="big-title"> <h2>Title</h2> </div> <div id="navigation"> <ul> <li>item 1</li> </ul> </div> <div class="red-paragraph"> <p>This paragraph is red</p> </div>
The code above if it was under the verification of the valuator, would be valid as all the markers (tag) defined by the W3C guidelines have been properly used. But as we said, this code cannot be considered as an optimized code because it uses several elements that can easily do without.
Here’s an example of an optimized code that produces the same result of the previous one:
<h2 id="big-title">Title</h2> <ul id="navigation"> <li>item 1</li> </ul> <p class="red-paragraph"> This paragraph is red </p>
As you can see in this very banal example to write only three elements (title, list and a paragraph) of an hypothetic web document, the first one uses eleven code lines while the second one only seven. This is a typical error committed by visual editors, which fill the code of a web document with many useless elements.
With this example we have demonstrated that writing a valid code is not always synonymous of a good code.
Validate the code: Is it always necessary to pass the validation?
Assuming that for a developer being able to validate the code he’s creating usually represents a personal satisfaction as well as a challenge with himself, is it always necessary to pass the validation?
The green check mark that indicates a validated code is generated by a software in accordance with the result provided by an algorithm developed to verify the correctness of the code under exam. Considering that a software has some very strict rules, it can happen that the code of a Web document, even if perfectly developed, for whatever kind of banality not foreseen by the validation algorithm, may not be validated
<div> <ul id="twitter_update_list"></ul> </div>
If we try to integrate our Twitter account inside a web document using these API, when the time comes to validate the code, we’ll receive an error message as the unordered list doesn’t contain any elements (<li>…</li>). Even in case we have perfectly developed the entire code of the web document, this would not be valid.
Can such an error compromise the validity of the entire work?
Paradoxically we can find ourselves in a situation like this:
- a Web document is validated although it has been developed with big problems of “divness” and “classness” or paged with tables;
- a Web document that – even if perfectly developed – with the integration of an element of third parties (as Google Maps or the fan box of Facebook might be) is found to have an invalid code.
Which of the two documents would you consider as more valid?
Validate the code: Extreme cases
I end this article with a final consideration: I happened to see people who, despite having chosen a Strict type doctype for the development of their web document and then spending hours of time searching for alternative methods in the attempt to validate the code that has attributes no longer permitted, like for example the target attribute.
If we need to use the target attribute, where’s the reason to choose a strict type doctype?
Now it’s your turn: I would like to hear your opinion on the many questions raised in the last part of this article.
P.S. Twitter’s API problem has been reported only for educational purposes , in reality it can be resolved just adding an element of the list containing an empty space: <li> </li>.
In tutti questi anni abbiamo ricevuto centinaia di richieste di approfondimento sulle numerose tematiche del web design vissuto da freelance. Le abbiamo affrontate volta per volta. Ma ci siamo resi conto che era necessario fare qualcosa di più. Ecco perché è nato One Year Together, un vero e proprio master per web designer freelance che apre finalmente le porte al mondo del lavoro.
Scopri One Year Together »