Ad-Hockery

ad-hockery: /ad·hok'@r·ee/, n.
Gratuitous assumptions... which lead to the appearance of semi-intelligent behavior but are in fact entirely arbitrary. Jargon File

Closures and field visibility

I find Groovy Closures can sometimes behave in ways that, once I think about it, make perfect sense but initially seem surprising. One example of this is the fact that closures declared in a parent class cannot “see” private fields of that class when run in the context of a child class.

Read more…

@CompileStatic and polymorphic method dispatch

I’m a big fan of Groovy’s @CompileStatic feature – so much so that I’ve updated the Groovy class template in IntelliJ IDEA to use it by default. I should stress I don’t do this because I believe non-statically compiled Groovy to be slow – it isn’t.

Recently Peter Ledbrook reminded me of one drawback which is that method dispatch is statically bound when using @CompileStatic like it is in Java. This means that the behavior of calling polymorphic methods can change when argument types are not known at compile time.

Read more…

Enabling Groovy’s “invokedynamic” support in Gradle

I posted previously about configuring a Gradle project to ensure that only the indy version of Groovy (that is the variant that supports Java 7’s invokedynamic bytecode instruction) is included in the dependency graph. However, just including that version of the Groovy jar is not enough to make your Groovy code compile in such a way that it uses invokedynamic.

Read more…


Static analysis and strictness

Let me be clear – static analysis tools like Checkstyle and Codenarc are useful tools. But…

I don’t think you should have overly strict enforcement. There is a small class of static analysis rules that are unambiguous – you’re not using that import, that if block doesn’t have braces – but there is a large class of rules that exist in a grey area. Is that method really unused or is it invoked reflectively somewhere? Yes, generally we should declare constants for magic numbers but is it really necessary for the prime seed of a hash code method?

Read more…

Decoupling from the DOM with Angular

One piece of advice you’ll run into pretty soon when working with Angular is that you should never touch the DOM outside of a directive. Especially when test-driving your components this is pretty wise. The great strength of Angular is the declarative way in which the view (HTML) works with the view model (Angular controllers). It’s almost absurd how easy it is to unit test Angular controllers. Controller functions tend to act on $scope properties, trigger or respond to events and all those things are straightforward to replicate in unit tests.

Read more…

Unit testing Angular directives that use controller and templateUrl

I spent an hour or so this morning figuring out how to unit test an Angular directive that uses a controller and a template loaded from a file (as opposed to inline). There’s a useful example in the ng-directive-testing repository but I thought a quick summary would be useful (as much to remind me the next time I have to do it as anything). Also the examples there test by interacting with DOM elements rather than directly with the directive’s scope.

Read more…



Semantic color names in LESS and Sass stylesheets

Most people using Sass or LESS will define variables for the palette of colors in their page. Something like:

$blackish: #231f20;
$purple: #561e31;
$pink: #da2770;
$off-white: #efefef;

.header {
    background-color: $blackish;
    color: $off-white;
}

a {
    color: $purple;
    &:hover, &:active {
        color: $pink;
    }
}

blockquote {
    background-color: $blackish;
    color: $purple;
}

This is good as far as it goes. Tweaking colors in the page is pretty easy as they only have to be changed in the variable definition.

That said, I think there are a couple of problems here.

Read more…