Articles of April 2013

Integrating AngularJS with RequireJS

Update 12/2017: Obsolete, don't just use Angular with Angular CLI.

When I first started developing with AngularJS keeping my controllers and directives in separate .js files for better maintainability I ended up including lots of .js files on my pages. This approach has led to some obvious drawbacks. For example, each of these files will have to be minified or combined before deployment to save bandwidth. There's also little control over load order and inter-dependencies between them, like AngularJS must be available before module can be created, and module must be present before one could attach controllers to it. So was looking for a clean solution to this problem, and that's where RequireJS came in.

Developing RESTful Web Services Using Dropwizard

  • access_time 6 years ago
  • date_range 29/04/2013
  • comment 12
  • share 29
  • label_outlineDropwizard, JAX-RS

Update 12/2017: While I think Dropwizard is overall a nice project I don't see benefit of using it over i.e. Spring Boot. Lean? Focused? Yes, until you reach the point when you need to actually do stuff and start adding to it. In that regard Spring is more complete solution and offers standard way to do these things.

Considering developing RESTful web services there are many options available for Java. To name a few one may consider Spring, writing them for a Java EE container with JAX-RS or even using something like Play Framework. Most of these are (heavier or thinner) integrated solutions though. When you step into one of these you buy it with the whole inventory of things they come with, not neccesserily targeted into developing RESTful web services.

Another approach would be to collect just what is needed to write such a service and glue them together in the project. The downside is, it takes usually some boring code to write integrating all of it into working solution (bootstraping, handling configuration files, etc.). And that is the reason I find Dropwizard helpful - it's more or less a collection of software for writing RESTful web services (Jetty, Jersey, Jackson), that takes boring stuff away and permits to jump ahead into writing the service.

Things To Remember - Java integer division

  • access_time 6 years ago
  • date_range 19/04/2013
  • comment 0
  • share 5
  • label_outlineJava

Another one which is counterintuitive to people programming in untyped languages while doing Java integer division, is that the result will be integer if both dividend and divisor were integers, regardless of the type of the quotient. See the following test case:

@Test
public void shouldShowHowDivisionWorks() {
    int a = 3;
    int b = 5;
    float c = a / b;
    assertEquals(0.0, c, 1e-3); // this is true, but may be unexpected
    c = a / (float) b;
    assertEquals(0.6, c, 1e-3); // this also is true
    c = (float) a / b;
    assertEquals(0.6, c, 1e-3); // this is true too
}

Things To Remember - NullPointerException on if with null boolean value

  • access_time 6 years ago
  • date_range 18/04/2013
  • comment 0
  • share 3
  • label_outlineJava

Update 12/2017: Could just smile at this now.

This one occurred to me today and it's caused by laziness I got used to while coding in untyped languages :) The condition in if statement should evaluate to boolean. If it's null, then NullPointerException is thrown as in the following test case.

@Test(expected = NullPointerException.class)
public void shouldThrowNullPointerException() throws Exception {
    Boolean someFlag = null;
    if (someFlag) {
        // unexpected
    } else {
        // could be expected
    }
}