Interfaces considered Important

Coding Conventions – _I_ want to kill the Impl. This is actually a fun debate… I’ll wade in:

First, Cedric pontificated recently about using Hungarian notation in Java. You know, stuff like “lpszStringName”… You can just guess my thoughts: Oh. God. No. And this isn’t just because it’s a Microsoft thing, it’s because it’s damn ugly. And it doesn’t even make sense in Java… I think lpsz means something like “long pointer zero terminated string”. How does that even relate to a String object in Java? That’s generally what’s wrong with Hungarian notation, it just doesn’t fit most of the time (though I’ve seen people try).

Cedric then continues today (yesterday?) with some thoughts about interfaces – specifically using an I in front of interface names to differentiate between them and classes (e.g. “IWorkspace”). And THIS is where it gets interesting because Charles thinks that that’s about the most evil thing he’s heard in a while. Yeah! Let the beetle battle begin! (Sorry, too much Dr. Seuss lately…)

I detest, loathe, hate, and want to vomit on any class that ends with the letters “Impl”. UUUUGH! IBM stuff is super-famous for this. Almost everything that comes out of there has this convention going. It’s like someone at IBM skipped a chapter or two in the OO book and decided that classes were less important than interfaces, so the interfaces get all the readable names and the classes get a Impl stamped on their ass. It must have something to do with the IBM mentality. I don’t know. What I do know, however, is that classes are the principle objects in Java, not interfaces. Interfaces are handy, dandy and cool, but they’re there to help structure your classes and allow interoperation without multiple inheritance, NOT to be the prime way of programming. If you’re thinking “I’ll just program to interfaces and forget about those classes”, you need to come up to speed because I think that fad went out a couple years ago.

-Russ [Russell Beattie Notebook]

This is interesting, mostly because I used to agree, and now disagree (sorry Russ). I think interfaces are one of the most under-used and useful parts of Java. I certainly believe that if you do have a class and an interface that it implements, the interface gets priority, ie. the unadorned name of the thing. The class is usually ThingImpl (or SimpleThing, PersistentThing, etc). Because if there is an interface, then you really, really should be programming against that, and not the implementation directly. I seem to spend a lot of my time retrofitting unit tests to legacy code, and interfaces are my most powerful weapon in this arena. Non test-driven code is frequently tightly coupled and hard to pull apart. Making the legacy objects implement simple interfaces immediately gives me a point of leverage to pry apart the coupling, and stub out parts of the system with Mocks, or make the test cases themselves implement the interfaces. This lets me very quickly build ‘scaffolding’ around the parts I’m testing, and makes refactoring a much less fraught affair.

Not to mention that well thought out use of interfaces makes it possible to do all sorts of cool stuff with your code, like adding dynamic proxies. Which, as we all know, are pretty much the gateway to AOP, developer nirvana, world peace etc. etc.

Going retro

Blocks in Java and C#

James talks about some syntactical sugar enhancements that can be made to Java borrowed from C#. Although it sounds petty, this stuff really makes a difference when you’re staring at code all day. Clean and readable code makes for relaxation which makes for smarter thinking. Syntax is closely related to karma.

The foreach and using operators are particularly nice as it provides a cleaner sequence for commonly executed sequences of events. But why stop there?

One of the features that made Smalltalk and Ruby so popular was the use of blocks. These allow you to create your own constructs.

Having (finally) twisted my brain far enough to start to understand Perl, I’m actually starting to appreciate it. This alone should probably scare me. It is (proudly, deliberately) a derivative language.

Perl shamelessly borrows paradigms from C, C++, Lisp and a few Unix tools. I’m still learning, but it looks like a lot of the AOP, block/closure & attribute stuff could easily be (or has already been) implemented in Perl, and has been available for years. I sometimes wonder if language designers are doomed to reinvent things that some guys over the road already implemented a decade ago.

Take this:


my $block =
sub {
my ($target) = (@_);
print "Hello, $target!\n";
}

Dangerous Perl Oversimplification 101:

my is like ‘private’.

$ means ‘scalar’ – essentially a single variable.

my ($target) = (@_) means (roughly) ‘take a copy of the first argument and assign it to $target in list context’.

$block now holds a reference to a function that will print a greeting every time it gets called. You can treat it like any other kind of variable, including sticking it in an array, or indeed a hash. Dynamic dispatch becomes a simple matter of using your arguments as the key to a hashmap. Yes it can be done in strongly typed languages with some creative use of interfaces and the Command pattern. The point is, its built-in, and has been for some time.

Why is Perl hard?

  • Well for a start, it has no grammar. You can’t generate a parser for Perl, its too irregular.
  • It is contextual. Like English, meaning can vary depending on the topic of conversation. For example, if I ask you “where are you going?”, and you replied, “to the shops”, we would both know that it was you we were talking about, and that you were going to the shops. Perl is a bit like that. A lot of those evil looking magic variables are to do with this. $_ is essentially the ‘topic’ under discussion. Unless told otherwise, most Perl operators assume $_ which is one of the biggest hurdles to overcome when learning Perl.
  • Perl is laid-back. So lines like ‘unless ($flag) {return 0};’ can also be written ‘return 0 unless $flag;’.

XP in Perl?

Can be done, of course. In a language as powerful and undisciplined as Perl, the discipline of XP is more important than ever. Refactoring will be harder than in other languages, possibly so much so that the (heretofore undiscovered) ‘refactor to other language’ may need to be applied. Test first is more rapid than in languages with a compilation step as you might expect.

One of the most compelling reasons to use Perl? CPAN. As has been said elsewhere, the true strength of many modern languages lies in the extent of their libraries. Chances are, if you need to do something in Perl, someone else has already done it and put it in CPAN. Something that made me laugh today is the Date::Manip module, which can parse strings like “second wednesday in 1996” to date values.

Take a walk on the wild side.

Book buying bonanza

The GoF Design Patterns of enterpise application development.. I just got my copy of Martin Fowler’s Patterns of Enterprise Application Architecture. I can best describe this as the GoF Design Patterns of enterprise application development. Beautiful book. I followed the public review process on his web site very closely. Martin’s style makes for such an easy read. I see this being a staple of any serious developer’s bookshelf.

I’m off to Ireland to drink my Thanksgiving dinner. Bet your ass that this book will be in my carry-on. [crazybob.org – web log]

Just placed the order for my copy. I’ve been following its evolution on Martin’s site for over year, so it seemed only fair.

Written any good books lately?

Michael Feathers of Object Mentor has started writing a book (currently) called Working Effectively with Legacy Code. Everything you need to know about tackling that aspect of software development that most of us spend a large chunk of our time on, and would probably rather we didn’t have to. Excellent stuff.

Follow its development, and discuss refactoring in general at the ‘refactoring’ yahoo group here.

XP Misconceptions

This

article makes for an interesting read, but it suffers from a common

misconception about agile methods and planning. Namely that Agile doesn’t do

planning. I’m more familiar with XP than the other agile methods, but as far as

I know, none of them claim that ‘planning doesn’t work so lets throw it out’, as

the article states. There’s even a book called “Planning Extreme Programming”,

by Kent Beck and Martin Fowler. What XP does recommend is that you should have

just enough planning/design/whatever for your specific needs. Its about

removing anything that doesn’t add value to your process.

The shroud of the dark side is falling

I like Perl. I feel like I should go to a meeting or start a 12 point plan or something. I’ve volunteered to work on a project at work that is using Perl, as I’ve wanted to see it in action for a while, and I have to say that it is scarily seductive. It just does what you want and gets out of the way. Provided you know how to tell it what you want.

One thing in my defense: I am entirely unconvinced that there should be ‘More Than One Way To Do It’. I got terribly confused with some date manipulations due to the ‘localtime’ function returning zero-indexed (0-11) months, and the Date::Calc module expecting 1-indexed (1-12) months. Sometimes its just easier to have Only One Way To Do It.

The OO stuff also looks like a dodgy aftermarket bolt-on…

Language Loss

Books and Words

Learning another language is tough. Amazingly tough. Cedric is going to post about accents some other day, which should be interesting. My accent in Spanish is horrible… I feel for the people who have to try to have conversations with me every day. I need another 3 years here at least.

-Russ [Russell Beattie Notebook]

Learning another language is certainly tough. Knowing that you used to speak one and can no longer is also a bummer. Its hardly my fault – I was 5 when we left Brazil. But when my parents recount stories of how my mother used to get me to translate for her, I feel gutted that I have (apparently) utterly lost all my Portugese. I would love to go to Brazil or Portugal to work just to see if I could regain my childhood facility with the language, but unfortunately its hard enough to maintain a software career in your native country currently, let alone try to make a go of it overseas. Darn.