Pages

2008-11-18

Making a "selectable" list component using facelets

Disclaimer: This is not a defense of jsf. If markup were an animal specie, then using jsf would get you arrested for mistreatment. I'm hoping that JavaFX wil give us a better way to make RIAs.

in web.xml

<context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/WEB-INF/somewhere/you/decide/whatever.tablib.xml</param-value>
</context-param>

in whatever.taglib.xml

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "facelet-taglib_1_0.dtd">

<facelet-taglib>
  <namespace>http://www.yourdomain.com/anothernameyoudecide</namespace>
  <tag>
    <tag-name>li</tag-name>
    <source>li.xhtml</source>
  </tag>
</facelet-taglib>

in li.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jstl/core">
    <c:choose>
        <c:when test="#{selectedPageName == pageName}">
            <li class="#{class} menuItemSelected"><ui:insert /></li>
        </c:when>
        <c:otherwise>
            <li class="#{class} menuItemNotSelected"><ui:insert /></li>
        </c:otherwise>
    </c:choose>
</ui:composition>

(I was particularily impressed to see that I could just add the existing classes as easy as it is done above. It's one of those rare moments when I think that it would be wonderful if it was this simple, -just to find out that it actually is!)

making the menu

<ui:component xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:y="http://www.yourdomain.com/anothernameyoudecide"
    xmlns:c="http://java.sun.com/jstl/core">
<ul class="horisontalMenu" id="topMenu">
        <li jsfc="y:li" pageName="home"><a href="Home.faces">#{bundle['topmenu.home']}</a></li>
        <li jsfc="y:li" pageName="search"><a href="Search.faces">#{bundle['topmenu.search']}</a></li>
        <li jsfc="y:li" pageName="reports"><a href="Reports.faces">#{bundle['topmenu.reports']}</a></li>
    </ul>
</ui:component>

setting the selected top menu

easy as just dropping this into the ui:composition of the facelet your displaying:
<ui:param name="selectedPageName" value="home"/>

2008-11-17

Quick wins with Java 6 update 10

So, the other day I needed a count down timer. For the pomodoro technique. A quick search on the net: java timer applet gives this http://javaboutique.internet.com/JavaTimer/ Seems like it was uploaded 2000-12-11. (Nice thing about Java. Just works, also with old stuff.)

Problem solved. But, it would be funny to see if I could use any of the new features of Java 6 update 10, so I downloaded the source code, updated the html-file to include <PARAM name="draggable" value="true"/> in the applet tag.

Open from the downloaded folder, alt-drag, WOW, it works! Cool!!!

2008-11-08

Using git locally, perforce centrally.

Work In Progress...

It probably comes as no surprise for people who know me that I find Perforce a bit awkward. I have suggested in an earlier post that the only reason anyone would go for Perforce is because they already have a big Perforce repository from before.

That is not to say I'm against version control. I use version control for every project that lasts for more than a few hours, for both strategical and tactical purposes I believe that it makes me about 10% more effective, mostly because it works as a project wide undo, removing the fear of making mistakes. This is, to me, the tactical purpose of version control(*). To have that benefit however, you have to commit your work frequently. If commiting you work is hard, or can have unpleasant side effects (think breaking the build on the ci server), then you'd probably just commit every time you've finished a piece of work.

By using git at your local computer, you can have almost(**) all the benefits of having a modern version control system, without having to move the entire company to another vcs. Just do "git init", "git add ." and "git commit" in your workspace and you have a local repository.

You can now commit your work as often as you like, branch out crazy ideas, and, when you've finished a piece of work, you just commit it to the central repository using standard perforce tools.

Hint: You might find it useful to let the master branch follow perforce exactly, and do your work inside a "local" branch. That way you can even use git tools for resolving conflicts before each commit.

So, what do you loose? Nothing. You still commit to the perforce repository every time you've finished a piece of work, so no significant revision histrory is going to be lost.

Disclaimer: I work at a smart, nice company, where nobody cares if you install cygwin, git or whatever, as long as it is safe, legal and doesn't hamper productivity. Installing git may not be a smart idea at every other company :)

* One of the strategic benefits is of course that you have the revision history when you wonder what went into a specific release.
**You still have to "open files for edit". :/

2008-10-31

JavaZone: Domain specific languages in Java

For me, one of the most inspiring talks at JavaZone this year was by Robert C. Martin. He talked about refactoring at method level, and one of the ideas he brought was that you should be able to read down a class like a book and immediately understand what was going on.

To achieve this, he said, you have to make methods that do one thing, and you have to make them short. When you give those methods meaningful names, it seems like reading a description of what the class does, and the function calls become a domain specific language. Guess I've never thought about it that way before. I used to think Domain Specific Languages was something you could do in Ruby and Groovy because of their slack syntax rules.

I've now rewritten a class that used to be about 1500 mLOC (messy Lines Of Code, that is). It is now about 500 LOC, does one thing (supports a JSF page) and delegates the rest to a nice little well tested class that also does just one thing. All this was accomplished while adding a much needed feature.

Guess it goes without saying: Martins Book: "Clean Code" is on my wish list.

2008-10-18

Why I would never choose perforce

I know two kinds of people who like perforce: People who have never tried a modern version control system, and Roall.

Perforce has one major weakness: It is very obtrusive, forcing you to "open" every file you want "for edit" every time you want to do any change to it. This basically means that perforce is very hard to use without tool support, compared to svn or git. Add to this that when eclipse europa arrived, it took 6 months before perforce released an updated plugin. The result was many crashes with lost workspace as result.

In my opinion there is one reason why anyone would choose perforce, and that is because thy are stuck with a big repository saved with perforce.

Obviously you should not take my word for this, so add a pinch of salt to my opinion, and also try some version control systems before you decide for your next project. I've liked both subversion and git (the only two other version control systemes I've used btw.) And if you like perforce, please write a post explaining why you like it, and add a comment, or you could send me a mail at erik . itland at gmail . com, cause I am stuck with perforce. (And yes, I have already tried once to find an answer.)

2008-10-17

So, you're stuck with <insert not so cool technology here>

This is a work in progress. Don't be surprised if this post changes over the coming weeks. This post is about small things you can do, even if you're stuck with whatever uncool technology.

Do something cool while commuting

I personally have spent a almost all my commuting time the last twelve months programming symfony. First a billing system for small businesses and then a web app for organizing work parties. The last one was for a non profit. Not paid of course. But it helps a lot to to something cool for 45 minutes in the morning before you start wrestling obtrusive frameworks that don't even respect basic things like url's. I also believe, out of personal experience, that I arrive at work more fit for work after 45 minutes of doing something cool and relevant, than after sleeping for 45 minutes more.

Use test driven development to save time

When I arrive at work, I can just forget about the comfort of making changes, alt-tabbing to firefox and refreshing to see the changes. There are some smart ways of avoiding server restarts, but the most important trick I've learned so far is to do use tests as far as possible before I deploy to the server and do the final testing. This way I save time, and I avoid sitting and waiting for that server, just to loose the patience and start looking at dzone for something useful. Oh, and then you also have the usual benefits of having a test suite to help you when you find out that you have to refactor that stuff.
Update (2008-10-31): Learn mockito. It is wonderful to just write someComplicatedClass = mock(SomeComplicated.class) instead generating a mock manually.

Refactor that stuff

I've recently read Michael Feathers "Working Effectively with Legacy Code", and it contains a lot of useful hints about how to get your code testable and how to add features in a safe way, but it was first at Robert C. Martin's speech at JavaZone this year that I became really inspired to make existing code more readable. If you've time, watch that presentation. It is available here. He recently published the book "clean code". Guess I'm going to buy the book. Just watching his presentation about functions was enough to go back to work and turn a 1200 line monster class into a much more readable class of less than 500 LOC. And I'm not finished with it yet.

Find some uninterrupted time, any uninterrupted time

(I don't do this anymore. But it worked 2 years ago, before I got married.) Sometimes I've found it better to trade in a couple of hours of sleep for a couple of calm hours. If your day is filled with interruptions, and you don't have a family to take care of, try sleeping a couple of hours (not more) in the afternoon. By the time the interruptions are going to sleep, you are (hopefully) wide awake and ready to get some real work done. Uninterrupted. And since you're not paid for it, you decide what to prioritize. In the morning, you might find it useful to sleep a couple of hours again, just so you can stay awake the rest of the day.

Update: Learn The Pomodoro Technique

(2008-10-31)I have just started learning "The Pomodoro Technique" and, so far, it has been a very interesting experience. Basically it helps you staying focused and avoid internal and external distractions. Add to that that you could learn the basics in less than one hour, and that you don't have to convert your whole team to increase your own productivity.

Update: Get a headset

Headsets serve two purposes:
  • Inform coworkers that you're trying avoid beeing interrupted
  • "Mask" conversations to avoid beeing distracted.

You found out something useful? Blog about it

How many times have you searched for something just to find the answer at someones blog? If you've had a hard time finding out something, chances are that someone else on this planet is wondering about the same. An example: I recently had a lot of work to remove a trojan until I did a search on google for information updated last 30 days about tdsserv. It turned out to be a very new trojan, first observed around 15. of september, as far as I understood the information. I posted the solution to my blog, hoping that someone else might find it useful, too. Last time I checked, I had three results coming in from search engines, related to that post, so obviously, I was not alone. It won't get me rich, but at least I'm sharing back.

2008-09-25

Delphi for Java developers

Why is there no book titled Delphi for Java developers?

I guess I'm not the only software engineer who has been forced to learn Delphi after Java. The reason is of course not that the company I'm working for at the moment is moving to Delphi, but rather that they have some programs written in Delphi that still needs maintenance.
Learning Delphi after Java give some interesting insights into programming language design. After I started to learn Delphi, I've started to appreciate Java more and more, just because Java is so clean and so simple. I've also started to see that language features that sounds nice when you first hear of them, can turn out to create really hard-to-read code. Here are some examples I'd like to mention:
  •  the with statement in Delphi. The with statement can save you for some typing, and could even make your code look a little more elegant, however, it's one of the features that makes Delphi code harder to read. (BTW: Here is something that you can do to make your code a little more elegant, and even save yourself some typing, hopefully without making it hard to read.) 
  • Another feature that Java "lacks" is global variables. (OK, I admit, when I was younger, I couldn't understand why people whined about global variables. Having to read and understand others peoples global variables really helps a lot on that understanding..)
  • You don't need parenthesis after function/method calls if you're not passing parameters. In Delphi it's not necessary to include parenthesis after a function or method call, if there is no parameters. Seems like it's not even laziness, but rather considered best practice. Sounds smart, but at least for my eyes, it isn't obvious that "SomeMethod" is a method call.
  • More than one public class can be defined inside a Delphi "Unit" (a Delphi source code file) . Even worse, the intefaces of the contained classes are at the top of the file, and the methods that makes the rest of the class can even be mixed at the bottom of the "Unit". Of course I guess mixing implementations is bad style, but guess what: If bad style compiles, chances are somebody is going to introduce it.
  • To make things even worse, Delphi doesn't have package trees or wildcard imports, so it's even more tempting to put many classes into one unit.
  • The Delphi IDE (of earlier versions) is composed of many top-level windows, instead of a main form. Today I heard someone talking about this as one of the features of older Delphi versions, but I clearly disagree. It is however better than gimp's interface in that if you activate one Delphi window, the rest of them are also activated.
For Delphi hackers who might read this: I guess my views are heavily influenced by the fact that I learnt Java first. If I've written something wrong, please write a comment about it, or even better: write a follow-up on your blog, cause I'm trying to learn Delphi, and I know there must be some reason why so many programs are written in Delphi, except for the speed.
Btw: Here is some interesting Delphi links I found a while ago: