Code Formatting
One of the things that bothers me most about working with other programmers is dealing with code formatting other than my own, and I have never had the patience to sit down and write all of the rules to format code just the way I want it. In normal Java or C/C++ source, I can deal with varying levels of whitespace and curly brace placement, but code formatting moves from minor annoyance to productivity killer when dealing with markup languages like HTML, JSP, or XML.
When working with markup involving open and close tags, the best method I've found to keep track of opening and closing tags is to consistently indent. This means that any enclosed tag is indented from its enclosing tags. We're all used to this in XML where indenting is usually fairly consistent, but let's look at a few examples where indenting can help prevent you from making mistakes.
Most programmers I've worked with enter the basic structure of a document as follows:
<html> <head> <title>Title</title> </head> <body> </body> </html>
More often than not, these same programmers frequently leave out the closing html or body tag. With a little indenting, it becomes much easier to spot problems:
<html>
<head>
<title>
Title
</title>
</head>
<body>
</body>
</html>
With this form, you immediately spot the problem when you have a gap in your closing indent sequence. Of course, I know there are tools that help you with this, but do you want to spend your time learning tools or coding? I prefer to code so I try to use a form that will let me spot problems with or without a tool.
Taking this a step further, you can see where this kind of code formatting really pays off:
<html>
<head>
<title>
Title
</title>
</head>
<body>
<table>
<c:forEach items="${list}" var="item">
<tr>
<td>
${item.a}
</td>
<td>
${item.b}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Note how easy it is to see that any code printed in the forEach loop will be part of the table tag. I have yet to see a programmer indent their JSP/JSTL this way yet it makes it so much easier to read and maintain.
Here's another approach I use in the case of long code lines, especially when there is no closing tag:
<input type="submit" name="submit" value="Text of the Submit Button" onClick="return confirm( 'Are you absolutely sure?' )" />
When you write code, you have to keep in mind that all programmers are different. Some use GUI editors, some text mode. Some run at uber-geek, high resolution while others still run at 800x600 because it's easier to read. Keeping your code within a 78 character width often works best for everyone involved so finding ways to format to that width is important to productivity.
You can also take this approach into normal code:
return functionWithLotsOfParameters( param1, param2, param3, param4, param5 );
These are just a few of my methods of making code more readable while at the same time increasing productivity by reducing compiler errors, whether browser, JSP, or compiler. It may feel like it takes longer to format everything this way, but it pays off in the long run.
Posted at 04:16PM Jun 19, 2007 by Jason Koeninger in General |