I have very little experience in web development, so this may be a very basic question.
It\'s just, from the limited experience I do have (
Semantically correct validated HTML is important; very important. A few spaces and line breaks are not important - any decent formatter can resolve this easily. Most of the time nothing apart from a browser will view it, so it really isn't that important compared to being validated.
Of course if you do take the time to indent and format it will look better to those of us who bother to look..
To view formatted html that is a little better, you can use the 'Inspect Element' feature of Google Chrome, or the firebug add-on in Firefox. I deal with dynamically generated html all the time (using php), and I never use 'view page source', these other tools work much better.
Its bugged me in the past, but I also think that making it look unreadable has it's advantages. It is semi-obfuscated code, by proxy.
However, many tools (such as FireBug) will format the code nicely in a DOM structure, so for a developer it really doesn't matter that much either.
The prevailing opinion is "it doesn't matter" because if you do want to view it there are plenty of formatters available and further, that using an automated formatter is a waste of system resources.
For scenarios where it does, you might consider:
You're right, it simply doesn't matter and when using different functions and classes it gets hard to avoid.
If you need the source code to be correctly formatted, most IDE's and programming orientated editors contain a facility to "tidy up" code into a correctly indented version.
Love it or not, HTML is a browser-readable format, not necessarily a user-readable one. Tools (WYSIWIG editors, markup languages such as Markdown, MediaWiki, etc. and your favorite web framework) should be used to used to generate HTML. Lots of programs are available to make HTML readable to developers; see the other posts.
XHTML and XML-generating tools hold some promise as regards readability since their output can be post-processed more easily (by browsers' source view, for example) to obtain proper indentation, but (IMHO) aren't quite mature yet, judging by the number of so-called "W3C XHTML-compliant" websites that really aren't. Newer versions of, e.g., WordPress make an effort, but their extensions, plugins etc. are full of bad (X)HTML, not even considering the amount of JavaScript they produce. (How are we going to indent that?)
Advice for now: