Jan 28

Java Needs Properties

OK, this is get­ting ridicu­lous. It’s been, what, 25 years since Java start­ed and we still haven’t fixed one of the sin­gle biggest pain-​points for the language—properties. I mean, just look at this mess:

The amount of boil­er­plate is huge and that’s with­out con­sid­er­ing oth­er boil­er­plate meth­ods like equals():

Ugh, I hate typ­ing all of that. If the object is a bean/​POJO then we can use gen­er­a­tors to help write some of the code at the expense of some weird code: strange inher­i­tance, abstract objects, anno­ta­tions, etc.

Now with prop­er­ties and some com­pil­er mag­ic we could have the same behav­ior that looks more like this:

And this would inter­nal­ly gen­er­ate the sec­ond exam­ple above. If you don’t like adding new key­words we could do it almost as ele­gant­ly with anno­ta­tions:

This could eas­i­ly be extend­ed to sup­port read-​only prop­er­ties as well as over­rid­ing the getter/​setter. Overriding the set­ter is need­ed to val­i­date the val­ue.

This would inter­nal­ly gen­er­ate the equiv­a­lent of this:

Note the assert in the con­struc­tor in the gen­er­at­ed ver­sion. This was added by the @NotNull anno­ta­tion. If the code gen­er­a­tor can guar­an­tee that name is set before the end of the con­struc­tor the it can elide the assert().

Anyway, there is a lot more that can be done. A prop­er­ty could be marked as part of the objec­t’s order­ing and then it would be added to a gen­er­at­ed compareTo() method. The basic idea is to remove the boil­er­plate and to force con­sis­ten­cy in code between pro­gram­mers.

May 08

JavaScript Ruminations

I have used JavaScript of and on for many years but in the last three years I have used it seri­ous­ly for build­ing a large client-​side web appli­ca­tion. During that time I have push many of the bound­aries of JavaScript and uncov­ered some fun­da­men­tal issues with the lan­guage.

One of the most com­mon com­plaints about JavaScript is that its object sys­tem is bro­ken. This is almost always becuase of a mis­un­der­stand­ing of what the lan­guage is. JavaScript is not an object-​oriented lan­guage, it is a pro­to­type lan­guage. What does this mean? It means that I can change the “object hier­ar­chy” of an exist­ing object at run­time by chang­ing the objec­t’s pro­to­type. This is by design and isn’t one of the real problems–it just requires a dif­fer­ent way of think­ing.

Continue read­ing

Jul 08

PHP Reflections

In my (rel­a­tive­ly) new job we use PHP for the back­end. This is the first time that I’ve used PHP for doing any real work and now that I’m using it for real I have found it to be an inter­est­ing lan­guage. I am still on the fence about whether I actu­al­ly like it or not but is does have some nice fea­tures. Given that I’ve only been using is seri­ous­ly for 3 months now I can’t claim to be an expert and I know that I’m just scratch­ing the sur­face at this point.

One of its best fea­tures is that dynam­i­cal­ly com­piled which real­ly short­ens the edit-​compile-​test cycle. I like being able to make small changes and see the (near­ly) imme­di­ate result with­out hav­ing to go through a build phase. It’s also nice that there is a large library of func­tion­al­i­ty avail­able as well as a large com­mu­ni­ty of third-​party solu­tions. I’m just now start­ing to look at some of the frame­works built on PHP and many look real­ly good. I do real­ly like the asso­cia­tive arrays, espe­cial­ly the abil­i­ty to eas­i­ly nest them, how­ev­er, I don’t like the way that PHP merges them with indexed arrays. I know that oth­er lan­guages do this as well, but I don’t like it.

I think my biggest prob­lem with PHP is just how easy it is to com­bine the out­put (view) with the log­ic (con­troller) and even mix in some data (mod­el) into one file. This is the same prob­lem that JSP had. This basi­cal­ly neces­si­tates a frame­work of some kind in any large project. The syn­tax is also prob­lem­at­ic for me. I find it messy and full of unnec­es­sary items like dol­lar signs ($) which I believe come from the Perl past. Perl had a rea­son for using a dol­lar sign for vari­ables: it indi­cates a “sim­ple” val­ue that is not an array (@) nor a hash (%). Given that PHP merges all of these into one pre­fix ($) it would be nice to get rid of it entire­ly.

Basically I think that PHP does a bet­ter job of fill­ing the “dynam­ic, script­ing lan­guage for gen­er­at­ing web pages” than JSP does, but I feel that it could be cleaned up a bit and a lit­tle more struc­ture added to make it eas­i­er to work with.

Jun 14

Time Waits for no One

Joda Time is a library that improves the func­tion­al­i­ty and usabil­i­ty of dates and times in the Java envi­ron­ment. The built-​in Java class­es are hard to use, miss­ing com­mon func­tion­al­i­ty, and do the wrong thing in many cas­es. Thus was born Joda Time. It has been so suc­cess­ful that it was made the basis of JSR-​310, a pro­pos­al for adding the Joda Time func­tion­al­i­ty to the base Java libraries. This post is not about Joda Time. At least not direct­ly.

In November of last year (2009) I joined with Jon Skeet and oth­ers in start­ing the Noda Time Open Source project. The goal of this project is to port the Joda Time library from Java to .NET. The built-​in class­es in the .NET run­time are woe­ful­ly incom­plete, inad­e­quate, and inel­e­gant. The orig­i­nal plan was to do a straight port with as lit­tle changed as pos­si­ble to make it work with .NET but as time went on (about 2 days) we real­ized that the Joda Time way was just too Java-​centric and would look too out-​of-​place in a .NET sys­tem. So after much wail­ing and gnash­ing of teeth we decid­ed to diverge.

As my first set of posts in this blog’s reboot I thought it would be fun to cov­er this process and along the way maybe talk a lit­tle about the issues that came up—both in inter­fac­ing with .NET and with time itself.

May 18

The Sharp Sea

I like C#. A far as object-​oriented pro­ce­dur­al lan­guages go, I think this is my favorite. It has a good feel to it and just about every­thing in it seems to fit. Most fea­tures are there to make my life eas­i­er, not the lan­guage design­er’s. This is how it should be. Here are a few of the fea­tures I’m most excit­ed about.

Extension Methods

These rock! Having the abil­i­ty to add func­tion­al­i­ty to class­es you don’t have the code for is awe­some. Don’t get me wrong, there is the poten­tial for mis­use, but once you start to use them you will nev­er look back.

This is much eas­i­er to read and under­stand:

than this:


Generics are good. Generics are great. Where I’m work­ing we still have to use JDK 1.4 so I can’t use gener­ics in Java. Argh, so prim­i­tive. Beyond the usu­al act of mak­ing a gener­i­cized list, when you com­bine gener­ics and exten­sion meth­ods then you have some­thing that real­ly rocks. This is how all of LINQ is built. And because of the way things work you can make your own LINQ oper­a­tors.

But most impor­tant to me is that by mak­ing an exten­sion method gener­ic you can cre­ate one exten­sion that is applic­a­ble to many (if not all) class­es. Trust me, this is real­ly big!

LINQ and Query Expression Syntax

When I first heard about LINQ I thought “OK, yet anoth­er way to embed SQL in a lan­guage.” Boy was I wrong. While you can go against SQL data sources with LINQ, I haven’t yet used LINQ to SQL and I use LINQ all the time. The way that it inte­grates with enu­mer­ables is just amaz­ing.

Given a vari­able People which can be con­vert­ed to IEnumerable which has the appro­pri­ate fields, try to do this with­out LINQ:

This returns an IEnumerable over a list of anony­mous types con­tain­ing three fields: FirstName, LastName, and Age. These items are sort­ed by LastName then FirstName and have been fil­tered to only those who are over 50. Wow!

Partial Classes

I’m not sure when these were added to the lan­guage but these are extreme­ly use­ful. If you have gen­er­at­ed code you can have the gen­er­a­tor gen­er­ate into one file and your hand-​written code go into anoth­er and the com­pil­er will merge them at com­pile time into one class. This gets away from acci­den­tal over­writes com­plete­ly. Or say that you have a bunch of ugly import/​export code that you must have because of the data for­mat you’re sup­port­ing but you real­ly don’t want to see it. Before you would have had to put the code is helper class­es but that means you may have to expose data that you real­ly would rather not. Now you just put the code into a side file which has a bunch of par­tial class­es. The I/​O code has com­plete access because it is part of the class but you don’t have to look at it unless you need to. Brilliant!

Partial Methods

Imagine hav­ing an abstract method that can be imple­ment­ed but does not have to be and (this is the best part) the call to it will not be com­piled into the code if the method is not imple­ment­ed. The pur­pose (I guess) for this is so that auto­mat­i­cal­ly gen­er­at­ed code can gen­er­ate a call to the method and a strong­ly typed def­i­n­i­tion can be cre­at­ed but still leave imple­ment­ing it as option­al. So far that’s all I’ve used it for but it is real­ly help­ful for that case.

Enough Already

Well that’s enough for now. Each of these fea­tures has made using C# much more pleas­ant and pro­duc­tive. Next time I’ll men­tion some Visual Studio fea­tures I like (and maybe a few I don’t) just in time for the VS2010 Beta.