Category Archives: Code

WordPress – simple links to share a post on…

For various reasons you sometimes don’t need or don’t want the JavaScript overhead that comes with the standard “Share On” buttons. I personally find those buttons annoying as they often take a lot of space on a page, are hard to style (alignment) and can be slow to load.

Here is a collection of HTML only links – the way they look in the content.php file of my WordPress template – that you can add to a WordPress post template for your readers to share on Twitter, Google+, Reddit, Flattr & Facebook.

the social media mess

Share on Twitter

<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?> @lroguet" title="Share on Twitter" rel="nofollow" target="_blank">Twitter</a>

Note: you probably want to change @lroguet with your own Twitter username.

Share on Google+

<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" title="Share on Google+" target="_blank" rel="nofollow">Google+</a>

Share on Reddit

<a href="http://www.reddit.com/submit?url=<?php the_permalink(); ?>" rel="nofollow" target="_blank" title="Share on Reddit">Reddit</a>

“Share” on Flattr

<a href="https://flattr.com/submit/auto?user_id=lroguet&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>&language=en_GB&category=text" title="Flattr this post" target="_blank" rel="nofollow">Flattr</a>

Note: you probably want to change lroguet with your own Flattr username.

Share on Facebook

I don’t have a “Share on Facebook” link at the bottom of my posts because I believe that is not the audience I target with my blog (I might miss a couple of readers but I’m fine with that) but I could as well add one with this line of code.

<a href="https://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>" rel="nofollow" target="_blank" title="Share on Facebook">Facebook</a>

measure execution time using EJB3 interceptors

Once in a while I am involved in the recruitment process of Java developers (both junior & senior) and run the technical part of the first interview. After some time I realized that with just a handful of questions you can pretty much tell whether the candidate is trying to fool you or not.

To a candidate claiming 15+ years of experience with Java including the latest and greatest features of various frameworks, from Hibernate to EJB 3.1 (and the list looked quite long on the CV), I recently asked: “how would you measure the execution time of a method?”

Candidate: “Well, I’d get a timestamp at the beginning of the method, one at the end and then I’d print or log the difference.”

Me: “Alright (that’s the first answer you would expect from anyone). But what if you can’t (for any reason) modify the method’s body?”

Candidate after a minute or so: “You can’t. You would have to do it that way. It’s the only solution.”

I don’t pretend to be an expert of some sort but I was excepting a bit more from someone who five minutes earlier claimed to have extensive knowledge of EJB 3.

Here is what I was after (this might not be the best solution – and please comment on it – but it does work and answer the question).

First I would create an interceptor (a class with a single method that is going to wrap the call to the method we want to monitor).

public class MethodExecutionTimeInterceptor {
    
    @javax.interceptor.AroundInvoke.AroundInvoke
    public Object time(javax.interceptor.InvocationContext.InvocationContext ctx) throws Exception {

        Logger logger = LoggerFactory.getLogger(ctx.getMethod().getDeclaringClass());
        long start = System.currentTimeMillis();
        
        try {            
            return ctx.proceed();
        }
        finally {            
            logger.info("method " + ctx.getMethod().getName() + " executed in " + (System.currentTimeMillis() - start) + " milliseconds");            
        }
         
    }
    
}

And secondly I would just annotate the method I want to measure the execution time of with the @Interceptors annotation.

@Interceptors({MethodExecutionTimeInterceptor.class})
public List<String> getAllNames() {
   // do something here and return a list of String  
}

That’s it.

behaviour driven unit test design in Java

Writing unit tests is not that hard but writing good unit tests which, when they fail, tell you exactly and verbosely why and where it did not go right is not as trivial as it seems.

Let’s say we have a class that represents a square. The only operations we can perform on a square are: getting its surface and doubling the side length.

public class Square {

  private int sideLength;

  public Square(int sideLength) {
    this.sideLength = sideLength;
  }

  public int getSurface() {

    return this.sideLength * this.sideLength;

  }

  public void doubleSideLength() {

    this.sideLength = this.sideLength * 2;

  }
  
}

To test that piece of code, we could probably write a unit test that will look like:

public class SquareTest {

  public void testSurfaceWhenSideLengthIsDoubled() {
    
    Square a = new Square(5);
    Square b = a; b.doubleSideLength();
    
    assertEquals(a.getSurface(), b.getSurface() / 4);

  }

}

From a programmer’s point of view the code above perfectly makes sense but – even if the example is rather simple – it does not really tells us what scenario we want to test here. If this piece code would be read by a product manager, she would probably not understand it (oh well, in that case she probably will but unit tests can be a lot more complex than that).

What about making the code a little bit clearer and telling us a story? There are frameworks for that you could argue (like JBehave) but do we need a framework – and all the configuration, XML files that come with – for everything really? What if we just rewrite the test class so it is more narrative?

public class SquareTestEnhanced {

  private Square square;

  public void test_side_length_x_2_gives_surface_x_4() {
    
    when_doubling_is_done_on(new Square(5));
    then_surface_must_be_multiplied_by_4();

  }

  private void when_doubling_is_done_on(Square s) {

    this.square = s;

  }

  private void then_surface_must_be_multiplied_by_4() {

    Square s = this.square; s.doubleSideLength();
    assertEquals(this.square.getSurface() * 4, s.getSurface());

  }

}

Isn’t it enough? Can my product manager understand what we are testing between lines 5 and 10?