4 ms·
Since he's writing Java, I would rewrite his initial function: public int getIntFromHeader(Request req, String headerName) { if(req != null && req.
by jgrahamc 14y ago
Since he's writing Java, I would rewrite his initial function:
public int getIntFromHeader(Request req, String headerName) {
if(req != null && req.getHeaders() != null) {
String header = req.getHeaders().get(headerName);
if(header != null) {
try {
return Integer.parseInt(header);
} catch(Exception e) {
return -1;
}
}
}
return -1;
}
as
public int getIntFromHeader(Request req, String headerName) {
return Integer.parseInt(req.getHeaders().get(headerName));
}
That way it either returns with a valid int or throws an exception of some sort (null or otherwise).
Also, -1 is a perfectly valid integer and so this function can't distinguish between a header with -1 in it and an error. The fact that there's a req != null check seems silly. Surely, the caller should be responsible for that and if they aren't they should get an exception.
- tantalor 14y agoAgreed. The caller should be responsible for checking whether req is null. As for getHeaders, why can it return null? When does a request have null headers?
- justinsb 14y agoMy preferred approach here is to have two methods; one that throws and one that also takes a default value. If the caller would handle the not-present case by assuming a value then use the default-value version; if it's an error then use the throwing version.
- prawks 14y agoAnd even in this case, a method full of != null checks seems silly when you can just catch NullPointerExceptions and return the default, no? Also, Java is statically typed. If you need such dynamic capabilities, look elsewhere?
- NateDad 14y agoThe problem with catching an NPE is that you don't know what was null, so you can't know if it was expected or not. Was req null? Or was the NPE thrown from inside GetHeaders()? Or something else entirely?
- prawks 14y agoRight, but in the provided example what was null doesn't seem to matter. Only that there was a null reference.
- justinsb 14y agoIt's normally considered bad form to catch NPE, particularly if a simple (x == null) check is the alternative. But that's largely a code-style issue; each to their own. I'm not sure what you mean by your static typing comment; I don't see where dynamic typing would offer a better alternative to anything we're talking about here?
- prawks 14y agoIt just seems like in a statically typed language, users should be more aware of nulls and handling them. Maybe I'm being naive, but it seems inherent in the paradigm that you shouldn't be playing as loosely with null and not null pointers. You should be fully able to expect a value to be there or not, by virtue of the certainty of your types and your conventions. I suppose when you talk about a large codebase used by people who are unaware of your conventions then you can run into issues, but I'd imagine that either null serves a purpose in your system, or not. If it does, handle it with the appropriate logic, if not, you shouldn't have to worry. Dynamic typing would offer you the convention of inherently being uncertain about what you're receiving and being overly defensive about everything.
- msmith 14y agoI agree that it's a bad idea to have a null check on req & req.getHeaders(). One might think that this kind of thing makes your code more resilient, but instead it just makes it harder to track down the inevitable bugs like forgetting to initialize Request#headers.