|
Val's Blog
Lots of stuff for Web 2.0 freaks and Javaholics
|
|
|
Alan M. Davis: "If you believe that you know the requirements better than the customer, you are part of the problem, not the solution." |
[ Login ] |
|
It is commonly admitted that dynamic languages are usually more flexible and permissible than static ones. On the other hand, another recognized fact about dynamic languages is that they provide more ways for you to write buggy software if you don't pay enough attention and don't play by the rules. For instance, I recently came across a nice bug in a web application making heavy use of Spring 2.0.2, JSP 2.0 and JSTL. Here is a piece of code that closely resembles the original code.
...
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<%@ taglib prefix='spring' uri='http://www.springframework.org/tags' %>
...
<ul>
<c:forEach items="${someValues}" var="value" varStatus="status">
<li>${status.index}. ${value}</li>
</c:forEach>
</ul>
...
<spring:bind path="address.zip">
<c:if test="status.error">
<c:out value="The ZIP code is not properly formatted." />
</c:if>
</spring:bind>
...
Can you spot it? Ok, it might not be immediately clear what the problem is, but remember that the JSP Expression Language is a dynamic language, and thus, variables are weakly typed. The problem lies around is the
Now the thing is that the javax.servlet.jsp.JspException: javax.servlet.jsp.jstl.core.LoopTagSupport$1Status
Very explicit as anyone can judge. In the end, the problem was that the first
For the record, the Spring 1.2.2 codebase used to explicitly cast the I hope this post will help other people not fall into the same nasty trap.
TrackBacks[0]
Comments[0]
Posted by val on May 20, 2008 2:58:07 PM CEST
A very common mistake that might seem obvious (or so I hope), yet that I've seen in many places is the one where we need to read characters from any kind of input streams and we cast an int read from that input stream to a char a little bit too early. The usual code goes like this:
InputStream in = ...;
char c = 0;
while ((c = (char) in.read()) > -1) {
//do something with c
}
The
The reason for this is that the type
InputStream in = ...;
int i = 0;
while ((i = in.read()) > -1) {
char c = (char) i;
//do something with c
}
//EOF reached
That way the loop will always break at some point and you'll be safe.
TrackBacks[0]
Comments[0]
Posted by val on May 14, 2008 2:36:12 PM CEST
I'm going to start a new weekly series of postings called "Spot the bug". The idea of this series goes along the lines of The Daily WTF, yet the goal is not to point fingers at anyone, but more to help people write better code, learn from others' mistakes and sensibilize them to bad practices and bugs present in existing codebases. The first "Spot the bug" entry will be posted tomorrow, so make sure you stay tuned. Thanks to you all for your ongoing support!!
TrackBacks[0]
Comments[0]
Posted by val on May 13, 2008 3:18:16 PM CEST
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Content © Val | Powered by Pebble 1.9.1 |