jpereira.eu

A life comited to learning

Where’s my Java reflection?

Some people like it, others don’t. I like it, it’s so cool to make generic code, but as I ‘ve been experimenting with GTW to know it’s capabilities I found that GWT does not support Reflection. It uses instead a mechanism called Deffered Binding to overcome the lack of reflection.

When doing some kind of framework code with GWT, soon you’ll realize that you need reflection, as I did. Bad luck, you’ll have to dig into the underground features of GWT, namely deferred bindings and generators. You can learn the theoretical traits of Deferred Bindings in the documentation: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html

Let me explain my scenario.

Continue Reading

Ahh, a responsabilidade…

Na semana passada estive presente numa formação onde se tocaram em temas bastante interessantes. Um dos temas abordados e que me chamou a atenção foi o “Modelo de Responsabilidade“.

O “Modelo de Responsabilidade” tenta descrever como nós, humanos,  geralmente nos comportamos face aos problemas que encontramos diariamente. Acho que este modelo descreve algo que nasce conosco, as atitudes e sentimentos que foram cultivados durante milhões de anos de evolução.

Continue Reading

Expressive programming in Java

In Ruby On Rails, for example, you can use a construct like this:

raise ActiveRecord::RecordNotFound unless condition

It’s simple to understand and easier to use, though you can also use the more classical format

if !condition
    raise ActiveRecord::RecordNotFound
end

Because of that I like also to use this kind of expressiveness in Java. Instead of thinking like:

if this then do that

I like to think like:

do that if this

Of course, is a matter of felling well while programming 🙂

Let’s take an example in Java. In this example there’s a simple API to put something in a cache and you will use a “key” to identify your objects in the cache. But because I, as API designer, don’t want you to use the string “key” as key, I will code the cache like this:

package eu.jpereira.exprogramming.cache;
public class MyCache {

  public static void addToCache(String key, Object object) throws Exception {
    if ( key.equals("key") ) {
      throw new Exception("Hey dude, key cannot be 'key'!!");
    }
    //do that cache thing
    System.out.println("Cool! You've added it to the cache!!");
  }	
}

But I prefer doing something like this:

package eu.jpereira.exprogramming.cache;
import static eu.jpereira.exprogramming.cache.ExceptionRaiser.*;

public class MyCache {

  public static void addToCache(String key, Object object) throws Exception {
    throwIfTrue(new Exception("Hey dude, key cannot be 'key'!!"),key.equals("key"),);
    //do that cache thing
    System.out.println("Cool! You've added it to the cache!!");
  }	
}

And the throwIfTrue method is coded like this:

package eu.jpereira.exprogramming.cache;

public class ExceptionRaiser {

  public static <T extends Throwable> void throwIfTrue(T exception, boolean condition) throws T {
    if (condition) {
      throw exception;
    }
  }
}

Even If at the first sight it may seems to be a lot of more work, it’ll pay off with my amusement while programming 🙂 Moreover, it’s easier to understand and to program like this.

Continue Reading

Programmer maturity

You’re a mature programmer when you’re willing to accept critics about your code.

You smell a critic. Ohh it’s about your code… There is a fire inside you, burning your pride. You can’t avoid expressing that anger through your actions. You can’t avoid showing me that lack of confidence on your work.
— from “The immature programmer”

Continue Reading

One line of code paranoia

If you have some java method like this:

public void doSomething(String oneArg, Object otherArg, Map<String, Object> mapArgs);

You can use your method somewhere in you code like this:

Map<String, Object> params = new HashMap<String, Object>();
params.add("key1", "Value1");
params.add("key1", "Value1");
doSomething("str1", someInstance, params);

Or if you’re paranoid about the number of lines of code you write, you can do something like:

doSomething("str1", someInstance, new HashMap<String, Object>(){{put("key1", "Value1");put("key1", "Value1");}});

Basically, you’re overriding the new anonymous HashMap and adding it a new initialization block…

Continue Reading
Continue Reading

Set focus on first input of a form

Isn’t annoying when you open a web page with a form and then have to click on the first input to start entering our data?

Well, it’s so easy to make this automatically with Javascript and JQuery

$("input:visible:enabled:first").focus();
Continue Reading

Look for the obvious first…

As I like to experiment cool web development frameworks, I decided to make a quick tour on Play! Framework. It was queued 🙂

So, here I was trying the persistence from the framework, that is basically JPA 2.0 with Hibernate as the persistence provider. I was also very interested in knowing what the framework could provide me for data validation, you know, that basic stuff that a framework should give us for free before it can be called wed framework.

I started with a very simple JPA entity like this one:

Continue Reading
Continue Reading
Continue Reading