Author: francis

Easter

Got a lot done over Easter but pretty busy.

Rosie was away at a Canoe Camping Club meet near Retford (Notts or thereabouts). We only have one car now so I drove over, camped for the night, drove back and then picked her up on Easter Monday. I managed to take a wrong turn and it took me an extra hour to get home on Good Friday – hey ho. Thursday night was really cold too.

Took Jon cycling at Delamere on the Saturday.  My poor old mountain bike is in need of some TLC and I had the interesting phenomenon of watching the front wheel turn one way when I’d turned the handle bars in another. This was going over a small jump. I was’t injured because us old folks take that stuff fairly easy y’know. Bit embarrassing. As the old guy I was supposed to be carrying tools. Apparently tools are an old guy thing. Another dad (surprise) lent me some alun keys he just happened to have on him, so I suppose the kids’ thesis is correct.

We went to the Yorkshire Sculpture Park on the Monday on the way to pick Rosie up. Only got about an hour in there and would have liked to spend quite a bit more time. Kidded D&J that one of the groups of sculptures was bits of an alien space ship and the big one by the lake was them writing “Help” but in odd letters.

I’ve been reading a lot recently. Still working my way through Living without Regret, (see end of last entry). Enjoying is not quite the right word, but it’s giving me a lot of food for thought. Ask yourself: what does mindfulness actually mean on a day to day basis? Can you remember what you had for breakfast? Your journey to work?

For my technical reading I finally succumbed and bought myself a copy of Martin Fowler’s Refactoring – great book. Read it over the weekend – I am that sad, sorry. I can read a big thick technical book in a few hours if it interests me. At least 2 ideas came out really strongly: the null object pattern, and the strategy/type patterns – as well as the interesting idea that a big comment is probably better replaced with a well-named method. Back to the defensive programming theme of the last blog entry he says it’s really important not to break your contract with the user and actually throw exceptions. I’m still thinking about exceptions and how to do it properly.

Saw some excellent code today – laughed my socks off:

if ( var == null || var.equals(“”) == true ) return ;

If you are a Java programmer and don’t know why that’s funny you need to get some more training. This was mixed in with lots of

if ( var == null || var == “” ) return ; 

Which is just wrong, assuming that var was passed as an argument and not initialised in the same piece of code. It really irritates me that I tried to find a job as a Java programmer for quite a while and here are these “Java specialists” writing such utter rubbish. Not to mention testing a private list variable that only has one access path to see if it had somehow got some objects in it that were’t the expected type – utter utter rubbish.

For reference the correct idiom is

if ( var == null || var.length() == 0 ) return ;

I also finally got the Ruby pick-axe book. Interestingly, I was choking a bit at the price Amazon were asking and found the Fowler book on The Register books  for 40% off, whereas Amazon were only offering 20%. The Ruby book was £5 or so cheaper too. If you’re looking for a popular computing book I suggest checking the reg out – just put the ISBN in and see. In essence I paid what Amazon were asking for a used copy for a brand new one, added in the Ruby book and got free postage. Good service, too.

Spring

Still trying to find 5 minutes to do something with this framework. Life (and selling a load of old crap on ebay) keeps getting in the way. I think Spring and Rails have a lot in common.

Archive Fragments

Got another section done – starting to get a bit knotty now. I’ve junked probably 50 pages of interesting but not relevant stuff. Need to get some head space and work on it a bit more. Problem is – I get home, I eat, I wrap ebay stuff I do blog entry because I think it will only take 5 minutes and suddenly it’s 10 pm and my 6 am wake up is looking very soon … work is getting in the way of doing interesting things … no change there then.

Foo Fighters

Been listening to In Your Honour almost constantly since January, particularly DOA. I want it played at my funeral, I’ll be laughing even if no-one else is.

When I can get the energy I’ll update this blog with some photos… 

Finding bugs in Java code, Hibernate and so on

Find Bugs

Highly recommend findbugs , we’ve been using it at work for a week or so and it finds lots of bad practices and naive code. Like me, in the last post here, it does’t like returning null all the time either. It recommends empty strings or empty arrays. Can run as a GUI, command line or Eclipse plug-in. We are going to write a batch file to integrate it into our source code control system and make it part of the code audit process as things are checked in.

Hibernate

Good tool , but I wish they’d stop trying to push EJB with it. Nasty dead technology that hurts to use.

Better Faster Lighter Java

Recommend this book . Buy it if you are doing anything with Java. I think the content is probably OK for non-techies as well, at least the first half of the book.  One of the authors wrote Bitter Java and Bitter EJB. These people know what they are talking about. I might even buy the pragmatic book on JUnit.

Exception handling in Java

Still trying to find a good clean way of handling exceptions. I remember reading something about Bruce Eckel of Thinking in blah (replace blah with C++ or Java or whatever) fame. TIJ is worth downloading and having around. I need to find some time to do some digging. Of course, there’s the obvious – just use Ruby on Rails and pass exceptions through to the flash handler, Struts does something similar when every exception has’t been eaten by a catch to be fair. Not sure that RoR is prime time yet. Some folk have told me that Rails does’t scale, but I do wonder if that’s the older version that’s now defunct. I can’t see why it wo’t, particularly if you are deploying under Apache.

Spiritual stuff

Highly recommend Living without Regret. About half way through it. I’m beginning to refocus on my Buddhist beliefs after a long time in the wilderness. This book is a good reminder and has made me think about some things. I underlined the piece about the 4 gates to freedom (p25):

  • Forget the Past. Walk away from the past and treat it like a city you have visited a long time ago.
  • Participate in what is happening now, do not hold back.
  • Drop any sense of I.
  • Let go of all notion of the future

The last point is about focussing on staying in the now. I don’t think it means don’t plan or think about the future – just stop yearning about it and being unhappy. Treat the future like you treat the past. I need to talk to my Dharma teacher but it has been so long I feel embarrassed. I know he wo’t mind, but that does’t make it any easier for some reason.

What defensive programming is and isn’t

This is a tech rant so ignore it if you are’t interested.

Defensive Programming

First, a definition (my understanding):

Defensive Programming means:

  • Not trusting the inputs to your methods/modules and validating them – throwing exceptions for unexpected input.
  • Every if should have an else. This is a caricature on my part, a more pragmatic/sensible approach is if you have a lot of if … else if … or a case statement, always put a trailing else or default that throws an exception if you think you have handled every valid case.

This is programming by contract/invariant under another guise, perhaps a little simpler. The second point can be a total pain if you use testing tools that insist on testing every path through the code, because you have to somehow pass invalid input through.

Look at the following allegedly defensive code. I would argue that this is not defensive, because it does nothing with the invalid input:

void bing(LogObject logger) {
    if (logger == null) {
        System.err.println(“logger was null”);
    }
    LocalLog log = logger.getLocal(“bing”);
    // … blah
}
 

So … you’ve logged that the logger is null but carried on to where you’d get a null pointer exception. Why? Either throw an exception, or let the exception throw itself when the getLocal method is called. I have spent most of the last 2 days removing this kind of nonsense from thousands of lines of code. Let’s look at another example:

boolean isSet(String testString)
{
    if (testString == null)
    {
        return false;
    }
    return (testString != null && testString.equals(Constants.ON)) ? true : false;
}

Assume  that Constants is a class that has some string constants defined in it.

I want to kill you! NOW! This is one of the worst examples I’ve seen of just not thinking properly. The defensive part is trying to handle the potential null, but it has been done in a really idiotic way:

  • Why test for nullity twice? Will something change the value of the object reference before it gets to the return statement? Is the Java virtual machine inhabited by pixies that change what an object reference points to between statements just for fun?
  • The expression inside the brackets by the return statement is already a boolean – why use the conditional expression?

if ( true ) return true ; else return false ;

Are you insane? I’ve seen this a lot in Oracle PL/SQL code as well. A boolean is a boolean is a boolean. 

  • If you have read Effective Java you will know that  the equals method tests for nullity in the passed object anyway. The problem is the potentially null testString will not have an equals method. You do trust that your own constants are not null,  pixies excepting. So rephrase to

return Constants.ON.equals(testString)

This is defensive because it handles the null case and it also handles all of the cases where what has been passed is’t a string (not likely in this scenario though). It’s also clean and not obscured by silliness. This leads to a common Java idiom where you always put the constant first and call its equals method, rather than the more natural passed object.

Stop returning null

A really stupid Java idiom is returning null if you’ve hit some kind of error condition, rather than throwing an exception. I’ve seen reams of this nonsense:

        try {
            // some really dangerous operation
        }
        catch ( Exception e )
        {
            // Which one of the 5 exceptions was it? you may want to handle them differently
            e.printStackTrace(System.err) ; // least it was logged somewhere !!

            return null ; // or maybe a runtime exception??
        }

Just propagate the exception or roll your own generic one for your app and throw it after encapsulating the real one. This also means the code that relies on you has to keep checking for nullity or throw random null pointer exceptions. This is inexcusable laziness and results in code like I showed before that tests for nullity all the time. Without an exception we don’t know what the nullity means, so we can’t fix it without dredging through the code (which may not even be ours) and put print statements everywhere. Usually these statements are not deleted and everything starts to bloat, especially your log files. Using nulls like this breaks the contract with the caller of the method. Exceptions are part of the contract. The user can choose to ignore them, but you kept your word with them.

So, stop returning null, please. If it’s an error then let it be one. Returning null breaks the encapsulation, because you don’t know what happened without having to go into the offending code.

If you are working with Strings, and you think it’s OK to return an empty string (as in a find method that did’t, say) RETURN AN EMPTY STRING!!! How radical is that? None of this crap

String myVar = params.get(“myVar”) ;

myVar = (myVar != null)?myVar:”“

Which I have written. J2EE designers, yes, I hate you for wasting my time. I end up writing a helper function to encapsulate this nonsense every time. 

Why has this site changed and other stuff

Blog city have changed the way they do the sites and here we have the least worst option I could find. I lke the trimmed head. Anyway, enough of that.

Ruby on Rails

Still like this environment a lot. I feel a bit ahead of the curve in that it’s still relatively small group of developers using it. I like the Ruby language itself as well. I’ve been using eclipse for rails. http://www.napcs.com. You have to do some hand cranking to set up things like calling rake tasks and starting WEBrick. I have’t had a look at rad rails which is also based on eclipse, mainly because the eclipse project settings directories fight with one another and it whines about plugins. I may set up a different Windoze user to try Rad rails – if I can be bothered.

Eclipse

I’ve finally got used to Eclipse. At work I have managed to get it to show a black background after a big fight with it and then managed to lose the settings when deleted stuff by accident. Really glad I followed my own advice and tar-d up my working directory once I’d got a build running, or it would have been a bit tedious. I’ve decided to go with just stopping everything being screaming white and see how that goes. Vim has themes and you can just pick one – why Eclipse is so far behind is beyond me, seeing as the world uses it. I’ve done some reading and this Microsoft/Apple crap about making your monitor look like a piece of paper is very bad for your eyes. The version of eclipse that ships with Eclipse for Rails does’t allow you to colour the text for scriptlets and everything went a bit black until I realised. So I’ve gone for the not-white option at home too.

Work

Bit tired of some of the silliness and the lack of planning. We’ll see.

Archive Fragments

Got some more of this edited/written. I need to think about it and do some planning, which is what I always say.

Even when I’m tired my brain keeps working

Spent a lot of the weekend in bed fighting off a virus that made me really tired. Glad I take supplements, one of the guys at work was off for a week and came back coughing like a 50 a day smoker. I think I got off lightly 

Recast the first 50 pages of Archive Fragments, looking good and tight now. I’m about a fifth of the way through the Rails book as a work book and about half just reading it.

Finished Absolution Gap – cracker of a book, really enjoyed it. It’s always interesting to read stories that make you think about what might happen if people could live for 500 years – what their perspectives would be and so on. Love the idea of the brain being enhanced to run more quickly and go into the quantum. 

Work is sort of fun because it’s new.

<java tech speak> 

We’ve been chasing an irritating bug with the framework we use to build our app. In essence the guys wrote their own IPC using sockets and if a random packet touches one of the sockets they are using it causes an out of memory error which kills sessions and randomly breaks things. A simple config change to use RMI instead, and up the memory to a better aribitrary value from the 100MB default. Bug’s been there for ever but we only just hit it. It took 6 weeks of effort to find an issue our framework supplier knew about already. Sigh.

Did some interesting reading around eden memory and so on, plus Java 1.5 has some sort of elective memory management which is in fact disabled in the app we use. Student’s T test, statistics, rah! Nearly 20 years since I did any of that stuff.

One really annoying thing about the app we are using is that in it’s “raw” state you have to rebuild the whole thing and then redeploy it every time, which takes around 10 minutes. Reminds me of the old days when I was writing COBOL – but I wo’t bore you with that. They also compile all of the JSP’s using Jasper in the build.xml before the deploy. Finding anything that might need to be changed is a total pain. Struts and tiles – really not sure what they give you about from even more XML files and indirection. Ruby on Rails looks better all the time. To be fair I suspect my dislike of S&T is at least partially because I have to keep rebuilding everything and they are’t visible in the eclipse environment because they deploy as part of the framework. Ugh.

If you want EL for all of the attributes of a given map in your JSP:

  <ul>
   <c:forEach var=”k” items=”${props}”>
     <li><c:out value=‘${k}’/> = <c:out value=‘${props[k]}’/></li>
   </c:forEach>
   </ul>

Anything else? Oh yes, I love eclipse but for not being able to make the background black across the whole tool. I read somewhere it’s much better for your eyes and have tried it with emacs and gvim and agree.

JAD

Java decompiler. Excellent tool that there is an Eclipse plug in for. Very useful. 

</java tech speak>

Need some kip now … oh yes. Big weekend in Bradford ahead – Rosie’s mum’s birthday and a conference. Love complication. 

Wot I’ve been doing

Archive Fragments

Got the first 50 or so pages done, in the process of reviewing and carving the crap out at the moment. With a pencil and a print out. Reading parts of the narrative out loud to make sure they work.

Ruby on Rails

If you want to build a database-centric web app in 5 minutes flat … Get hold of a copy of Agile Web Development with Rails and dive right in. This is interesting because I’ve been holding back from it for ages, simpy because I was wary of spending energy on YAPF (Yet Another Programming Framework). Sun have apparently started putting money into it (probably because Oracle bought Zend of PHP fame). It just works and is secure. It’s what a framework should be – I hate the Java and M$ frameworks that make it hard to do anything without and IDE and 20 weeks training. Rails just works, and you don’t really need to know Ruby, although I think understanding what yield does would be an advantage.

I also love Ruby-esque things like

errors.add(:price,”should be at least 0.01”) if price.nil? || price < 0.01

Concise and clear. I also love the idea of having a series of migration objects that update your database and create test data. 

I think it wo’t be long before we have Rails for J2EE, JRuby is already there and you can call out to underlying Java classes anyway. Looks useful and cheap. Testing and so on are built into the framework…

Settling in

I’ve been working a lot on Archive Fragments, completely rethinking it to explain some of the important characters better. Doing a mind map and some planning. Have gone as far as I can with the “just write something” mode of working. That’s how I work to get a feel for the shape of things. When I left my last job the guys kindly bought me a novel writing book that I’m really getting into, although a lot of what is says are things that you kind of know if you have done some writing and thought about it.

I strongly recommend Quicksilver by Neal Stevenson. It’s absolutely fascinating to read about how intellectual life started in the 17th Century and how England was by far the worst-off trading country in Europe but, interestingly, the development of natural philosophy started to make it dominant. Plus all of the court intrigues and the politics of keeping your court poor so they depend on you, which is why Louis XIV build Versailles outside of Paris to force his court to have 2 houses. Plus the flow of gold betwen warring parties – very interesting.

Very excited about AF, but can see it’s going to take a while before I have anything I can send to anyone. I suppose though that you can always send the submission pack and see if anyone bites. I’ve been using google documents to do the actual writing because I can work on it anywhere I have a web connection.

I’m missing working with the people at my old job, but not the work, not even slightly. I think I took the piss for most of the last 6 weeks, even though I did’t want to. I have no idea what would have happened if I had’t found another job, maybe I’d have gone postal – just as well I don’t own a gun!! They also gave me some Argos vouchers that I spent on some headphones, a Bluetooth dongle and a camera so the kids can wave at their mates when they’re on Messenger.

New job quite challenging. I’m helping the config manager do his job before his contract finishes at the end of the month (or thereabouts when he finds a new contract anyway). It’s all Unix scripts and Java stuff, I’m in my element. Have also helped the poor mug who’s had the Oracle database work dropped on him get some things going.

Commute is great. Takes about 50 minutes and practically no queues. We also work a 38 hour week which means we finish at 12:15 every other Friday. I’m adjusting to starting work at 8:15, but only leave about half an hour earlier than I used to leave to get to the old place for 9 ish so pretty good. Supposed to leave at 4:45 but guess what? I have’t done so yet.

People are really easy to get on with and we have a good laugh. I feel so much better being busy with interesting things to do, I can’t describe it.

New Year – ish

Had a quiet Christmas. Mostly spent plastering, waiting for plaster to dry and then painting it. With a little bit of replacing skirting boards for variety. Roger did the plastering because he knows how to do it. I did a bit and learned a lot from him but am not sure when I’ll ever use the skill.

Kids had a good christmas. Generally quiet.

I have finally found another job, starting on 22nd Jan. Sad to leave old job in some ways, particularly if it had been the job it was supposed to be in the first place.

Trying very hard not to take the mickey, but very hard because I don’t want to do any of the tasks I have been given. None of it is what I was supposed to be doing. I feel I’ve wasted the last two and a half years, not being able to give what I know I can and being forced into a role on the back of some broken promises. We’ll see how the new role shapes up. I’ve never been in such a ridiculous situation ever in all my working life. The CEO appointed an old friend in a role senior to me who did’t have developer skills so I got pushed into a development role. Ridiculous. Incompetent. Some other words I can’t put here. I’ll be glad to see the back of it all.

I sent my novel Archive Fragments off to a publisher and got it back. Had a long discussion with one of the human beings at work, who also writes, and I’m thinking I will restart it and look at the narrative punch. Also, if I can make it new again I wo’t be totally bored with it!

I made a traumatic discovery after losing my temper with Jon when he turned his back on me and walked away. After 40 years I’m still carrying round all the grief from my father’s suicide. Rejection makes me really angry. I’m quite scared of the powder keg but now at least I know it’s there. Carrying all this rubbish around is really boring, remembering it all is really boring too – these stupid habitual mental loops that just make you feel bad. Habits can be broken.

Onward.