One of the discussions that arises from time to time in web development circles is that of validating your code. Some will insist your pages need to validate 100% and others will tell you it’s not even worth checking as long as your pages work. Will people care if your pages don’t validate? Will search engines care? How important is code validation?
What Does Validation Mean?
Validating your code is simply comparing it to W3C standards. It’s no different than comparing a sentence you write to the rules of grammar for the language you wrote it in. In fact the grammar analogy is a good one to keep in mind when you think of validating your code.
Much as different languages have different rules of grammar, the code you use can have different rules depending on the doctype you use. Your doctype is setting the language you’re using and code that’s valid under one doctype may not be valid using another.
<p>This is a sentence
validates under html 4.01 transitional or html5 where the closing tag is not required, but not under xhtml 1.0 transitional where the closing tag is required.
I’ve included some screenshots at the bottom of this post to show the full code I used to check under each doctype. You can click any of the 3 images for a larger and readable image.
Note that I also left off the period after “This is a sentence” above, which violates the rules of grammar, but likely doesn’t cause any problems in communication.
How to Validate Your Code
Since validation is comparing your code to W3C standards I think the best way to validate your code is using the W3C validation tools.
If you click the links above it’ll look like they go to the same page, but I assure you they’re different. Click the More Options link to see on each page to see.
There are plenty of browser extensions that will test the page you’re viewing in the W3C validators.
HTML Tidy is another option for validating pages, though it may not offer the exact same results as the W3C validator. One advantage of HTML Tidy is using an extension you can check your pages directly in the browser without having to visit one of the validators.
Why You Should Validate Your Code?
Ultimately validation is simply a tool to help you code better.
The reality is as long as your site and pages display like they’re supposed to your visitors aren’t going to care whether or not the code behind your site is 100% valid. Browsers won’t care much either as they’ll typically render much invalid code perfectly fine.
The only people likely to care or even know how to check are fellow web developers. Most of them won’t care either. Some might, though most probably won’t. Still there are good reasons to validate your code.
The W3C offers 5 reasons for why you should validate your code.
- Validation can be used as a debugging tool – Validators tell you where you have errors in your code. If your page isn’t displaying as expected a validator might very well point you to the cause. Also invalid code that may display fine in one document may cause show stopping errors in another because of the code around it.
- Validation can help future proof your work – Browsers are built with standards in mind. Pages coded to web standards today will work with browsers built tomorrow. Hacks used to get something working may not.
- Validation can be a great learning tool – Fixing validation errors will teach you a lot about how to best code a web page. It will help you get through the learning curve quicker. I speak from experience.
- Validation makes maintenance easier – Standards are widely accepted by the development community. If your site uses valid code another developer should have no problem maintaining the site. Untangling hacks on the other hand is never fun.
- Validation is a sign of professionalism – Getting the small details right even when no one else notices or cares is a sign of professionalism.
Those aren’t the only reasons though. Valid code is more accessible as it’s compliant with standards. It should work across more browsers and devices making it more usable and accessible. Different browsers may treat invalid code differently and sometimes in unexpected ways.
Some will probably tell you that valid code will help your pages rank better. I’m going to tell you those people are wrong. Other than show stopping errors (which validation can help you find) search engines really don’t care much about your code. The home pages of Google (35), Yahoo (167), and Bing (14) all have validation errors.
However all things being equal I would think valid code better for search engines than invalid code. Let’s face it no one can really list every potential show stopping error where search engines are concerned. The safest thing to do in that case is fix all your errors or as many as realistically possible.
Sometimes when you run a page through a validator the list of errors seems so large that it looks like it will take days to fix them. Often a few simple errors are leading to many more. For example forgetting to close a tag early in your document could end up resulting in dozens, even hundreds of errors. Fix those one or two errors and watch as many more errors are gone when you revalidate the page.
Acceptable Validation Errors
Ideally all your pages would be 100% valid, though it’s not practical or even desirable at times to achieve 100% validation.
Some code that’s commonly used just won’t validate. Vender specific prefixes such as -webkit and -moz will never validate by definition. That doesn’t mean you shouldn’t use those vendor specific prefixes.
Hacks for specific browsers (I’m looking at you Internet Explorer) also won’t validate, though it’s unlikely you’ll find a developer who’s never written a coding hack. Sometimes they’re all you have to get the page working right.
Validators will also issues warnings in addition to errors. 100% valid code means 0 errors, but it could include any number of warnings. Some of those warnings aren’t going to be things you’ll want to fix. Many tracking parameters you add to the end of a url will give a warning for example.
In the end you shouldn’t sweat it if your pages aren’t 100% valid. Some errors and many warnings are perfectly acceptable as part of a well-coded page.
Below are a few articles I collected while putting this post together. They’ll offer similar and occasionally different reasons for why you should be validating your code.
- But it Doesn’t Validate
- Code Validation – Clean Code
- HTML and CSS Validation: Should You Validate Your Web Page?
- Code Validation
- Validating Your Code
My Own Validation Practices
A few years ago I worked to make sure every page I developed was 100% valid and I’d even work to reduce as many of the warnings as I could.
At the time I was learning how to code to the standards and validating pages is one of the better ways to learn. I’m sure I learned to code quicker because I consistently validate every page I developed.
I can’t say I was always good about continuing to check those pages unless the code changed significantly it was unlikely validation errors would suddenly show up.
Today I don’t work toward 100% validation as often as I used to for a few reasons. First I’m past that early learning stage and I’m a better judge of which errors are important to fix and which aren’t that big of a deal.
I also feel pretty confident that any page I develop at this point is going to come close to 100% validation. Maybe not 100%, but probably not all that far from it. Coda, the primary code editor I use is good about alerting me to some errors as I write the code and it completes tags for me as I start typing. I also pull more from a library of code that I know to be valid.
Another reason is that most sites I build end up on WordPress, which means the code I’m developing on top of is already valid. Most of the validation errors you’ll encounter with WordPress come from poorly coded plugins, which is where most of the validation errors on this site currently come from.
I do still validate. Mostly it’s when I’m first developing the template(s) that will later become the working web pages. That’s where I’ve found I’m more likely to make errors.
How important is it to validate your code? That depends on your perspective. It’s really never that important to make sure your pages are 100% valid, though at the same time if you work to make a few sites 100% valid you’ll find most of the sites you build after will have few if any errors other than the typo variety.
No one outside of the development community will ever care if you write valid code as long as the code works as intended and many inside the development community won’t care either.
Still I think validating your code is a good idea mainly for the reasons given by the W3C. It’ll make you a better coder, help you debug pages that aren’t working, help you and your fellow developers maintain your sites, and ensure your pages work across browsers and time.
You should also validate because you’re a professional who cares about your work. There will come a time when you have to let go of you work and hand it off to your client, but until that time don’t you want to build the best site you can build? Validation will help you get there.
Do you validate your code? Why or why not?