views:

113

answers:

8

Sometimes I add a \n at the end, and the relevant number of \t the beginning, and sometimes not - what's the recommended way to do this?

+5  A: 

It depends whether you want the HTML to be human readable or not. In most cases however, I would suggest that generated markup should always be human-readable, as it makes debugging a whole lot easier.

Douglas F Shearer
+1  A: 

I always include \n's. The reason is simple: sometimes stuff doesn't work and I want to be able to read the generated HTML to help find the problem.

David Arno
+1  A: 

It depends on how often you are going to have to manually debug the HTML. If you will need to look at it yourself, rather than just browsers, then some basic formatting will make life a lot easier. If you are confident you'll never need to look at it (as most people seem to be), then you don't need to. I tend to do basic formatting with basic indentation - but I'm not an optimist in such matters (and I've had to look at the stuff more than once, and been grateful that I've done the basic formatting).

Jonathan Leffler
A: 

Though I do out of habit, there's no need to at all when you've got something like Firebug which shows you the HTML tree all nice and pretty. (Though it does show you the generated HTML, rather than the raw HTML...)

nickf
A: 

I personally include such characters as it makes the initial debugging much easier, albeit at the cost of a little extra code for you. If having humans be able to read it later is a goal feel free. Another thing to keep in mind is that most developer tools probably have the ability to format the HTML for readability.

Try to consider who you are writing the code for yourself right now, other developers to maintain in the future or the person looking at the html (who likely does not care about formatting).

Good luck.

smaclell
A: 

I generally find new lines more important than indentation. If my goal is readability during debugging, that's usually enough to be able to read the code effectively. If my goal is output that someone else is going to read, I sometimes try to add a little more style to it.

acrosman
A: 

I tend to use Firebug when debugging which auto formats HTML and CSS anyway, so I don't bother. I have written tools to create neat tabbed HTML however.

Ross
A: 

I personally include the HTML formatting (tabs/new lines) when I'm doing HTML only, but when I have a loop or generally generating HTML with a script (PHP) I usually don't worry about the HTML formatting. I do sometimes when I'd end up wtih such a massive amount of HTML it'd be unreadable.

To me the most important part is speed--in "properly" formatted HTML, the spaces and new lines can be 25% of the file size. One of the reasons JavaScript is so often minimized.

Darryl Hein