General musings on programming languages, and Java.

Friday, March 23, 2007

Refactoring or Decoupling - which one do you want? Both.

A lot of time is spent on developing and using good refactoring tools, so that if we do decide to change something, we can, with minimal effort. However, refactoring doesn't generally work between codebases, encouraging large, monolithic codebases. The old wisdom about always programming to interfaces, in case you want to change which implementation to use later, seems a bit flawed in the presence of refactoring IDEs. When I'm teaching, to keep things simple I let students use ArrayList references, rather than List ones. Of course, that breaks down with subList, because subList doesn't return an ArrayList. They'll never ever want to change their ArrayList to be a LinkedList, or a Vector, and even if they did, it would be a simple change with a refactoring IDE. Ok, so the difference is that my students aren't generally writing APIs.. Well, Josh Bloch suggests that writing internal code as if you're writing APIs is a good idea. After playing with a dynamic language again, I can see the benefits of that straight away, if you don't have a refactoring IDE. I can also see that the refactoring IDE encourages you to use one codebase. My main project, a network simulator, relies on a set of functional programming utilities (functionalpeas) that I keep as a separate project. If I want to change the API of the FP utilities, I open the FP project, edit, build a jar, then go back to the simulator, copy the jar across into lib/, rebuild and fix the errors. This experience makes me less likely to split my work into several separate modules. I can, at any point in the networking code, get access to any part of the GUI. Obviously I don't do this very often, maybe just during debugging, but it would be nice to be able to actually prevent myself from doing that by accident. I see two solutions to this: either I can get it right first time, every time, or IDEs can start to support refactoring between projects, possibly even pushing the refactor out to remote developers. Imagine this: "A library that this project uses has been updated. Would you like to review the refactoring script that comes with the update?".

Programming Language: Servant or Master?

In discussions about language features for Java 7, I've seen many suggestions that mythical 'other programmers', presumably ones who don't read/write blogs, will write terrible code that uses the language feature. Programmers will write terrible code, regardless of the restrictions you place on them. If you make it harder for them to write their terrible code, they will learn more slowly. Many even stop learning, but that's a subject for another day. Personally, I've had some ridiculous ideas over the years, and those ideas only lived so long because I couldn't try them out very easily. Some good ideas died for the same reason. In C and Lisp, you are the master over the language. That seems to be true of Python, Ruby and Smalltalk too. Of course, C's #define is so coarse as to make communication between master and servant a bit stilted, but the theory's there. In Java, C#, Pascal and other academic languages, you are the servant. Of course, any servant can learn to express himself, but it takes longer. It's like being forced to speak another language, such as the Welsh were. I expect that the literary production of the Welsh people suffered for a time when they were forced to speak English. If I want a switch statement that works on Strings, I can write it myself, as a developer, not a compiler-writer, in a less restrictive language. Then I can come across problems with it (such as fragility) and find a better solution. If I instead have to boilerplate out a load of if..else statements, I'll take longer to realise that my idea is faulty, rather than the switch statement being inflexible, because I'll be blaming the wrong thing for my boilerplate. If you're worried that operator overloading would let your fellow programmers write awful code, ask them what they would do with it. If you worry about what open classes would result in, write a program in Ruby. If you think that the program was too small to determine it, write a bigger one. If you worry about Lisp macros, write some, or define the following macro: (defmacro defmacro () ()) Now you can't define any more macros. You can remove defun in the same way. I imagine that wouldn't be so good for your career.

Sunday, March 11, 2007

Why I should learn Spanish

I work as a lecturer, a programmer, a DJ in a latin club, and a salsa teacher, in descending order of pay per hour. "¿Habla español?" "Como un idiota." On Friday night, a Cuban lady came to my DJ box, during a Los Van Van song, asking for Cuban music. I asked her which country she thought Los Van Van was from (they're Cuban; as far as I know, the second most popular band in Cuba). She didn't understand me. I got the message through via her friend, but she didn't seem to recognise the band as Cuban, and pulled a face like she'd just found out that some meat she was eating belonged to the rattus genus. I asked her which band she'd like to hear. Sometimes this is interesting - I hear a name I've never heard of before, can't work out how to spell it, pull out a pad and a pen, and the requester writes it down. Usually, at this point, I recognise the name, but couldn't understand the requester's flavour of spoken Spanish, partly because I speak Spanish like an idiot, and partly because there are just too many decibels around. At times it's tempting to forget about the other 200 people in the room and turn the music down just so that I can hear someone speak. Anyway, this time; Blank expression, prolonged, then "Celia Cruz". Celia died a few years back, but will forever be known as the Queen of salsa, with a huge number of platinum albums. She is also the only Cuban female artist that many Westerners have actually heard of, apart from Gloria Estefan, who, according to my sources, doesn't count. My suspicion was aroused - was she really Cuban? I'd only seen her speak bad English, and look confused, which doesn't necessarily make one Cuban, though it probably helps. It probably didn't help that I mistook her for a Colombian lady who had made repeated attempts to steal my beer a year prior. Anyway, I'd already played 2 tracks in the last hour by Celia Cruz, so I wasn't too keen on playing another so soon. I did oblige, and played another, about half an hour later, during which she shrugged her shoulders as if I'd ignored her request. I shrugged mine in response, ironically. I'm not sure how well irony works without speech. One reason to learn Spanish is so that I can give sarcastic answers, like "Cuban? Where do you think this track's from, Alaska?". I can speak enough Spanish to say "Where's this song from?", but I don't know how to make it sound sarcastic. I've checked, and latinos do use sarcasm. On the same night, during the third reggaetón track in a row, someone came and asked if I was going to play any reggaetón tonight. It's hardly as if the tracks weren't clearly reggaetón - the dancers nearby made it very, ahem, clear. I'd only heard about this kind of idiocy from other DJs, I'd never seen it actually happen. But two in one night? On the plus side I got a lot of compliments about the music that night, and the night after. Another reason to learn Spanish is the small fact that my girlfriend is Argentinian. I wish I had heard her speak before I'd learned any Spanish though. In theory, the different accent from Argentina should be beautiful, but to me, it sounds almost as strange as Texan English. She sometimes laughs at my Spanish, but in a friendly way, and I do the same for her English, though I do correct her if she repeats a mistake, because she has exams in English soon.

Wednesday, March 07, 2007

A funny compiler bug in Java 5

I made a couple of versioning mistakes recently when moving my development environment from one computer to another (and to another). This basically resulted in me sending a 1.6-only binary to a QA team (read: my boss) with a 1.5 JVM. I decided that the best way to solve it was to move my development environment to 1.5, and just use 1.6 to run applets and ordinary programs (such as IDEA). I was already using -source 1.5 in my ant file, but I had forgotten to tell IDEA about that.. When I downgraded, I recompiled, and found a very amusing compiler bug in the latest release of Java 5. A bit of background: I've never liked the usual workaround for the 'final' restriction on local variables accessed from within a local class, i.e., the single-element array. Hopefully it will go away with Java 7. So, I made a Var class. It used to be all fake-OO, it would have a set and a get, a bit like Shai Almog's amusing properties proposal. Since upgrading to 1.6, I decided that as I did no validation with a Var, I may as well expose its value directly. public final class Var<T> {     public T value;     public Var(T value)     {         this.value=value;     } } The Java 5 compiler was crashing on var.value++ within local classes. I had to change those to var.value=var.value+1. I wonder how that manages to be fixed in Java 6, but fails consistently with Java 5 versions (I've seen it once before, but didn't bother to debug it at the time, as I was mid-refactor). I'm not sure it's even worth reporting as a bug, because somebody must have fixed it for Java 6, and made a conscious decision not to bother for Java 5. Or so I assume. Perhaps the Java compiler is trying to force me to use encapsulation. Personally, I prefer one of Lisp's styles of encapsulation - don't even make the attribute that you're concerned about be a part of the object (e.g., (let ((x 5)) (defun inc () (incf x 5))), assuming I've got that right), but we can't do that, so I just make it public until there's a reason not to.

Blog Archive

About Me

A salsa dancing, DJing programmer from Manchester, England.