|
Val's Blog
Lots of stuff for Web 2.0 freaks and Java addicts
|
|
|
Albert Einstein: "Intellectuals solve problems: geniuses prevent them." |
[ Login ] |
|
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 |