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

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.

May 14

The Story Begins

In which our intre­pid (or is that tepid?) Hero finds him­self on the shores of the Sharp Sea look­ing through the Windows to the Presentation of the Foundation of all things.

Last time I men­tioned that I was work­ing on a project to sim­pli­fy con­fig­ur­ing our main prod­uct and that I decid­ed to make it a reg­u­lar Windows appli­ca­tion instead of a (shud­der) VBA appli­ca­tion.

I like learn­ing com­put­er lan­guages and so I thought that this would be a good time to learn C#. I had dab­bled with it over the years and had writ­ten some small pro­grams in it, but noth­ing serious–nothing that required learn­ing the details or the dark recess­es of a lan­guage. And with 3.0 out I fig­ured I could learn about all the cool new fea­tures. That and I just can’t bring myself to use Visual Basic. It is a pow­er­ful lan­guage with some neat fea­tures but it just brings up too many old, painful mem­o­ries.1

So I fired up Visual Studio and told it to cre­ate a new Windows Forms Application.

Hey, what’s that WPF Application tem­plate?

Oh, that is Microsoft’s lat­est attempt to make a good UI library. It’s just anoth­er rework­ing of their old stuff. Don’t wor­ry about it.

Shouldn’t we at least look at it and see if it’s any bet­ter? I mean learn­ing some­thing new is fun and it’s one of the rea­sons were doing this on the side.

No, we know some­thing about how Forms work and we don’t have the time to learn a com­plete­ly new sys­tem if we want to show some­thing soon to con­vince peo­ple not go the VBA route. Don’t wor­ry it will all be fine.

OK, if you say so…

Talking to my inner self can be annoy­ing. Where was I? Oh yeah, the appli­ca­tion solu­tion was cre­at­ed. Add a few sup­port projects, steal code from oth­er projects, rename a few things, and com­pile. Yay it runs!

Time pass­es…

Microsoft annoys me.

You talk­ing to me?

Yeah. They’re real­ly big, have very tal­ent­ed peo­ple and they still cre­ate com­plex, hard-​to-​use stuff like this. It’s a real pain to get things on the win­dow, look­ing nice, and be func­tion­al. You have to write so much code. Ugh.

You know, there is the thing I heard about once…I’m think it was called WPF. Someone once said we should look at it. I can’t remem­ber who…

OK, OK. I get the mes­sage. Just to make you hap­py I’ll take a quick look, you’ll see that it’s just the same old stuff repack­age like always and we can get back to mak­ing this work.

Thanks, this will be fun…

Fire up a new solu­tion, this time a WPF Application. Looks pret­ty much the same, an emp­ty win­dow. OK, the tool on the left look pret­ty much the same, maybe one or two new things. Nothing sur­pris­ing there. I guess it time to read up on this. I’ll give it an hour or so.

Time pass­es…

OMG, this is even more com­pli­cat­ed than Forms! Look at all the options and set­tings!

Time pass­es…

Wait, bind­ing? Like Adobe Flex? We liked bind­ing in Flex.

Time pass­es…

Item tem­plates? Data tem­plates? What?

Time pass­es…

I’ve got it! I get it! Wait! Microsoft made this? This is…good. This is…right!

I told you we should look at it.

But it was just anoth­er “Microsoft threw too many peo­ple at the prob­lem” thing. How could I have known?

You should lis­ten to me more you know. This half the brain has good ideas too you know.

The Recap

For those try­ing to keep track, I, like many oth­ers, do not have the most favor­able opin­ion of Microsoft prod­ucts. I do have a lot of respect for the peo­ple who work for Microsoft. Some of the bright­est stars of my field (soft­ware devel­op­ment) work for them. But in my opin­ion, the com­pa­ny some­how man­ages to infect them and caus­es them to pro­duce things that are, shall we say, not so good. I think that it may have some­thing to do with bureau­cra­cy and iner­tia, but what­ev­er the rea­son, I am not a fan of most of the prod­ucts that have come out of Microsoft for the last few years (many years in fact).

But WPF changes all of that. Now mind you, it is not per­fect. There are some things I would change but over­all this is how a win­dow sys­tem should be. The sep­a­ra­tion of con­cerns, the sim­plic­i­ty of exe­cu­tion (once you get over the “how does it work” hump), and the abil­i­ty to change just about every­thing is fan­tas­tic. As time goes on and I learn about new fea­tures in the sys­tem I go “wow that’s great. I wish I had known about that before.”

And the great thing about this is that it works as a Windows appli­ca­tion and a brows­er appli­ca­tion. And with Silverlight using the same basic tech­nol­o­gy I see great pos­si­bil­i­ties ahead.

  1. The new Visual Studio 2010 is going to include F# as a stan­dard lan­guage. Finally a func­tion­al lan­guage may become main­stream. I miss Scheme. 
May 12

Something a Little Different

For the past few years I’ve been using Java for the major­i­ty of my pro­fes­sion­al and pri­vate devel­op­ment. Using it pro­fes­sion­al­ly was sim­ply because that is what my com­pa­ny want­ed. Privately was because I was using it pro­fes­sion­al­ly and so the envi­ron­ment was there and start­ing up a new project was easy. Java is a good, sol­id lan­guage with a pletho­ra of libraries and sup­port but every time I went to make an appli­ca­tion with a nice UI I was stymied. Everything just seemed hard to do in Swing—much hard­er than it should be.

Recently, I was put on a new project at work. For this project we are build­ing a sim­ple con­fig­u­ra­tion tool for our main prod­uct. Configuring usu­al­ly requires gath­er­ing lots of sim­i­lar items and so some­one thought that Microsoft Excel would be a good plat­form. One of the biggest things that it had going for it was that it was the tool that the require­ments gath­er­ing peo­ple were already using. They were just using it to col­lect the tables of data, but with no pro­grammed sup­port. The they would give that infor­ma­tion to some­one else to input into our con­fig­u­ra­tion files. So, some­one thought that it would be a help­ful idea to auto­mate many of the steps they went though and gen­er­al­ly improve the expe­ri­ence. The only prob­lem was that they used VBA.

VBA is a great macro lan­guage, but as a tool for build­ing appli­ca­tions it sucks. It real­ly just blows. Try hav­ing mul­ti­ple peo­ple work­ing on it at the same time. And this was the project that I was put on. Oy.

So to keep myself sane I decid­ed that I would look into alter­na­tives. As this was a tool tar­get­ed at a small group of peo­ple (say 20–30 peo­ple total) and all of these would be on some form of Windows I fig­ured that we should just go ahead and make this a full Windows appli­ca­tion. I had dab­bled with C# and GDI so I thought I’d try my hand at mak­ing a tar­get­ed appli­ca­tion (while work­ing on the VBA/​Excel deba­cle).

So the next few entries will be my expe­ri­ences with doing an appli­ca­tion not in Java. We’ll see how it goes.