Search |
||
I swear, I don't want to talk about Java 7 language in details...Posted by fabriziogiudici on January 11, 2007 at 3:36 PM PST
... but most of the replies to my previous post "Where are we going?" (also at TheServerSide follow-up) ended up talking about closures etc. It sounds like nowadays it's inavoidable to talk about code and syntax. Too bad. I thought that my next post would have focused on the "process" concept (since it looks like there's a sort of language barrier), but I'm postponing it and getting into the mud. ;-)Ok, I was kidding. Serioulsy, I don't want to be involved in the syntax debate. I'm just taking an example and showing how focusing too much on the code makes you loose sight of other problems. Let's take for example Stephen Colebourne's proposal about a special
For instance:
public String getPostcode(Person person)
{
if (person != null)
{
Address address = person.getAddress();
if (address != null)
{
return address.getPostcode();
}
}
return null;
}
would be coded in the really shorter:
public String getPostcode(Person person)
{
return person#getAddress()#getPostcode();
}
Looks neat, doesn't it? Stephen says: "When they do code the null-checks, maybe due to unit tests, UAT or a production issue, the code suddenly loses all readability, and the real intent of the method is lost". At first sight it sounds reasonable - I must admit that sometimes all those "if" keywords annoy me too. But why don't we look at the problem from a broader perspective?
My first question is: what is the semantic meaning of Let's say it's a valid value. Well, being null is always a special case for an entity. In this case I say that the Now let's consider the other case: it's an invalid value. It should never happen. In this case, is the
try
{
return person.getAddress().getPostcode();
}
catch (NullPointerException e)
{
return null;
}
where the But this it's not satisfactory yet. Is it ok to return null or would it be better to return an empty string? Which is the effect of that Please note that with the My point is, if a null address is not a valid case, let's stick with this:
public String getPostcode(Person person)
{
return person.getAddress().getPostcode();
}
after all exceptions in low-level classes should be thrown, and caught at upper levels.
Last but not least, you must keep the control of that null with testing. While a wandering null could go unnoticed in a test case, especially in a world filled with
So, if we really want to keep the # operator, at least let's use a new keyword: PS Stephen considered correctly in his post that there are "a few corner cases where returning null/zero/false is inappropriate": object#equals(null); // returns false when object is null, but should return true list#isEmpty(); // returns false when list is null, but would prefer to return true To solve this, he proposes a special static method to deal with it:
public class Object
{
...
public static boolean #equals(Object other)
{
return (other == null);
}
}
That is, to save a few lines in the application code, we are bloating the Java runtime (a common consequence of the "devil is in details" pattern). Is it still KISS? »
Related Topics >>
Community Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|