Thursday, November 22, 2007

Null Evilness is a Java fault

This week Marty touched a sensible spot in his article "Returning None is Evil", where, aside from what's obvious from the title, he claims that this behaviour is something popular on the Java mindset.

As many people point out, it doesn't seem like a fault of Java, as nothing is really different here between Java and Python (both have exceptions, both have null|None).

But I think it is a fault of Java. Java is statically typed, Python isn't. If the compiler makes me type a bunch of information about the possible values of the data of my program (in fact, more than what's really needed), I want something back. Something like checking that I will never get an unexpected value into one of my variables.

The problem


Java, by allowing null values everywhere (except on primitive data types), doesn't fulfill my previous expectation. I must be always remembering that every method could return null. Same with every method argument. That's not acceptable when considering how much I'm paying for static typing.

Yeah, I know that there are cases where a "nothing here, move along" value is reasonable. But that doesn't explain why Java takes that to the extreme. Is it reasonable to have a null List? A null String? Nope, that's not reasonable and I'm wasting time when I'm forced to check for that.

Some solutions


So what would fix this? IMHO, every language that aspires to be a Good Statically Typed Language(TM) must have a way to differentiate between types that allow a special None|nil|null value and those that don't.

The easy way would be to annotate null or not null types, such as in:
public static void main(notnull String[] arg)


At least then you could look on the signature of a method to know if it can return null. And unexpected null arguments wouldn't happen anymore. Nice takes this strategy, using a question mark to annotate nullable variables (?String foo = null is valid, but String foo = null isn't).

But the Really Good Way would be to use enumerate types and pattern matching as it's done on many functional languages. I'll show how that looks on Scala. There, Python's dict.get would be somewhat like:
def get<K,V>(key:K):Option[V] = ...


So you can't just use the result of it without taking in consideration that it could be None (the previous solution doesn't necessarily force you to do so).

Therefore, you can pattern match it:
dict.get(key) match {
case Some(value) => [do something with value]
None => [dome something else]
}


Or, when getting a Option[T] parameter, you can use a default value:
def greet(name:Option[String]) = 
"hello " + name.getOrElse("anonymous")


Here I used the getOrElse method of Option, that saves me making a trivial (but somewhat verbose) pattern match.

The sad part is that in Scala, as in Java, every subclass of AnyRef (the equivalent of Java's Object) allows null value, so nothing prevents having an Option[String] variable pointing to null. It may be the price for being a Java-integrated language, but it's really annoying:

val foo:Option[String] = null
foo match {case Some(s) => s case None => "none"}

scala.MatchError: null
at .(:5)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)


Doh! Well, it's still better than Java, as hardly someone will object the summary execution of the programmer that assigned null to an Option (or to its value).

But Java won't change tomorrow!


Hey, this thing surely won't change ever. In the meantime our tools are assertions, the "NullObject pattern" and prominent documentation on methods that may return null.

Wednesday, November 14, 2007

Context Switching Burns Programmers

Here is yet another essay about programmers getting promoted into management. I agree with many things exposed, and disagree with others, but here is something I couldn't agree more:

The programmer, though, wants to be involved deeply and profoundly in just a few projects — he wants to own them, top to bottom. Maybe it’s a whole program, or a single feature, or some underlying library. Whatever. He wants to live in it, neck-deep. He has to worry about all — literally all — of the obscure technical details that make computers go. Jumping between projects — context switching — is a great way to burn a programmer out, because the cost of unloading one project from his head only to load up another one is enormously high. The idea of switching between two projects in a day, much less ten, is not only exhausting, but depressing.

Tuesday, November 13, 2007

Fixed-width font on Gmail 2

Update: As of March 2009, this doesn't work anymore. But here is updated fixed width font trick for Gmail

After the latest update to Gmail (you know you have the new version because a "Older version" link appears on the upper right corner, and the UI changes slightly) , almost all tweaks ceased to work. This includes the custom CSS for fixed width font.

So, here is the trick again, updated to the new version (you should include this into the userContent.css file under <your firefox profile>/chrome/ directory):


@-moz-document domain(mail.google.com)
{
/* GMail messages and textarea should use fixed-width font */
.ArwC7c, .iE5Yyc {
font-family: MonoSpace !important;
font-size: 9pt !important;
}
}

Monday, November 12, 2007

Software is hard, part II

...as already pointed out.

I know I don't really need too much evidence, but here we have some numbers about failure rate on IT projects. They can be handy someday ;-)

It summarizes different studies, including some embarrassing bad numbers:


  • 51 % (of the surveyed people) viewed their ERP implementation as unsuccessful

  • 31.1% of projects will be canceled before they ever get completed. Further results indicate 52.7% of projects will cost over 189% of their original estimates

  • only 16.2% for software projects that are completed on-time and on-budget


Yeah, I've not given the details about how these numbers were obtained. See the linked article, if you are intrigued.

Tuesday, November 6, 2007

procinfo: pgrep, pkill and pmap for Win32.

Procinfo is a tool made by Daniel Molina, a colleague of mine at Imagemaker.

It lets you use regular expressions to find and kill processes, using the command line on Windows. Simple and useful.

Monday, November 5, 2007

Top Posting


A: Because it breaks the logical sequence of the discussion.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Heh. Despite it being on wikipedia, I didn't know this succinct and funny critique to top-posting.

It seems that you always learn something reading pypy-dev...