Abstract Bits

A Lazy Coder != A Good Coder

Posted by Jared Stehler

May 1, 2013 12:20:00 PM

Recently, a co-worker recently shared a quote with the group:

working as a programmer is the 1st time in history where lazy people (w.o. connections or personal wealth) get payed well to be even more lazy
I take issue with this on a number of levels, but mainly that this seems to be a common theme in the programming world, that a lazy programmer is akin to a good programmer. This can't be further from the truth.

A lazy programmer:

  • takes harmful shortcuts and hacks his features into production code
  • doesn't take the time to think about how his module will behave in the context of the larger system
  • writes poor (or no) unit tests
  • doesn't care to understand how the API he is using works
  • cuts and pastes code
  • uses global variables
I definitely would not want to have a lazy programmer on my team, or contributing in any way to my codebase.

What I think people must mean (hopefully!) is that a good programmer automates when he can. A good programmer is conscientious, meticulous, and thorough. 

Read More

The case of the mysterious failing date test

Posted by Jared Stehler

Mar 9, 2013 5:58:00 AM

I had such an aha! moment this afternoon that I thought I'd share it.

This test case was failing for me, and I was positive that I hadn't made any changes which would affect that part of the system:


The trick here is when I was running the test: March 9, 2013, at 1:55pm EST. Turns out that tonight is daylight savings, when we "spring ahead" and lose an hour, and of course 23 hours does not a standard day make!


Read More

Topics: daylight savings, joda, Java, test, junit, time

Using StealJS in Jasmine Tests with Grunt

Posted by Jared Stehler

Mar 1, 2013 7:37:00 AM

Grunt Jasmine Configuration

In this post, I'll detail how to use StealJS to author Javascript unit tests in Jasmine, and run them from the command line using the headless grunt-contrib-jasmine runner.

If you look in the documentation for grunt-contrib-jasmine, you'll see a section on using a custom template to run tests which use RequireJS as a dependency manager. You'll end up doing the same bit for StealJS, but will use a different template plugin: grunt-template-jasmine-steal.

First, install the necessary plugins into your local grunt project workspace:

npm install grunt-contrib-jasmine --save-dev
npm install grunt-template-jasmine-steal --save-dev

Next, import the tasks into your Gruntfile.js:

grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.registerTask('test', ['connect:test', 'jasmine']);

Finally, define the connect and jasmine plugin configs. Note that I'm setting up the connect plugin to start a local web server on port 8000, for phantomjs to connect to when running the jasmine suite:

var jsFiles = [
    'src/**/*.js',
    '!src/can/**/*.js',
    '!src/steal/**/*.js',
    '!src/thirdparty/**/*.js'
];

connect: {
    test : {
        port : 8000
    }
},

jasmine: {
    tests: {
        src: jsFiles,
        options: {
            specs: 'src/**/*_spec.js',
            host: 'http://127.0.0.1:8000/',
            template: require('grunt-template-jasmine-steal'),
            templateOptions: {
                stealOptions: {
                    stealUrl: '/src/steal/steal.js',
                    baseUrl: ''
                }
            }
        }
    }
}

Take note of the jsFiles variable I defined; that concisely lists all of my javascript source files, excluding libraries.

Cleaning and Linting your Sources

Another important build step is to clean and lint (error check and validate) your code, and thankfully grunt makes this easy on us. I use the jsbeautifier and grunt-contrib-jshint plugins for this:

grunt.loadNpmTasks('grunt-jsbeautifier');
grunt.loadNpmTasks('grunt-contrib-jshint');

        jsbeautifier : {
          files : jsFiles
        },

        jshint: {
            files: jsFiles,
            options: {
                jshintrc: '.jshintrc'
            },
        },

Note: look at the documentation for jsbeautifier to see the available configuration options (the defaults work fine for me out of the box).

Putting it All Together

Here is a complete Gruntfile.js and .jshintrc, detailing what I went through in detail above. I've set it so that the default grunt task performs the code formatting, checks using jshint, and then runs the full jasmine test suite.




Read More

Topics: jshint, jsbeautifier, javascript, jasmine, grunt, canjs, steal

A deferred loading cache in Javascript

Posted by Jared Stehler

Mar 1, 2013 5:00:00 AM

I was playing around with jQuery deferreds the other day, and thought up a neat use for them, in a client-side ajax response cache. I created a simple expiring cache abstraction, and then wrote a 'loading' cache around it, which populates the cache with $.Deferred instances which are resolved with the result of a $.get.

Here's the simple expiring cache:




And here's the deferred loading cache:




Jasmine spec for the loading cache:




In my next post, I'll show how to use a jasmine spec runner for steal to run jasmine steal specs in grunt.

Read More

Topics: cache, javascript, ajax, jquery

StealJS Builds with Grunt

Posted by Jared Stehler

Feb 28, 2013 3:07:00 AM

GRUNT!

I have seen the future of javascript builds, and it is Grunt.  Please, please, please disregard my earlier post on building with maven; it just doesn't make sense to use such a heavyweight Java-based tool for building javascript projects.

Grunt is rightfully taking the Javascript world by storm. It's got a great community around it, as shown by the wealth of plugins available. It runs on Node, allows for package management via npm, and lets you start working with Javascript in a way which makes us stodgy old enterprise Java guys breathe slightly easier ;-) (more posts on that soon!).

Read More

Topics: javascript, grunt, canjs, steal

My First Maven Release

Posted by Jared Stehler

Sep 26, 2012 8:12:00 AM

As promised, I've gone and published my dropwizard-guice library jar to the central maven repository. This was a rewarding experience, being my first such release, and it feels good to be giving back to the community in such an "official" manner!

Here's the official maven dependency:

Read More

Nostalgia

Posted by Jared Stehler

Sep 4, 2012 12:13:00 AM

I've been coding for quite some time now. It just so happened that my freshman year of high school coincided with the release of an exciting new technology / language named Java. This was a language with its own runtime, which included the ability to run inside of a web browser (think Netscape). Seeing as how I already had some experience tinkering with C and C++, the prospect of creating visual applications made Java all that much more appealing to me. I borrowed my Dad's Teach Yourself Java in 21 Days book, read through a bunch of tutorials, and dove head first into the world of applets and AWT.


Read More

Topics: Java, applet, games

Simple Guice integration for Dropwizard

Posted by Jared Stehler

Jul 22, 2012 5:14:00 AM

If you haven't come across it yet, Dropwizard is a nifty new framework designed to streamline and simplify development of lean web services which are inherently "ops-friendly". By "ops-friendly", I mean that it provides out of the box support for and gentle encouragement to use things like health checks and metrics monitoring. By "lean", I mean that these services are run from within their own JVM processes, with embedded Jetty as a server. This idea of multiple, self-contained, fail-fast processes is best described by the Twelve Factor App.

So with Dropwizard, you extend from the base Service class, and add things to the Environment on startup (e.g. Jersey Resources, Providers, etc). I've created a simple extension to the Service class which does some classpath scanning to automatically install your resources, providers, health checks and tasks for you, via Guice, allowing you to use Dependency Injection throughout your application for object dependencies. Check it out on github! (IMHO) It's quite simple to integrate into your project, and I've found it to be very useful so far, in that I can simply create a new resource class with a @Path annotation, create a constructor marked with @javax.inject.Inject, and it will automatically be wired up and installed into the REST container.

Let me know what you think! If I get enough interest (read: any) then I'll throw up the binaries on an accessible maven repo.

Update (26 Sep): I did end up getting interest, and have since made this available on the central maven repo.

Read More

Topics: dropwizard, guice

ProTip: Debugging Minified JavaScript using Chrome DevTools

Posted by Jared Stehler

Jul 20, 2012 8:55:00 AM

Here's yet another reason why I develop exclusively in Chrome: they've got a killer feature in the JavaScript DevTools which will format any script file, even if it's been minified, and allow you to place breakpoints on that formatted output!

If you're working on troubleshooting an issue in a production app, for example, you'll have something like this:

Read More

Topics: minified, javascript, chrome, debug

Building CanJS with Maven (or Ant)

Posted by Jared Stehler

Jul 14, 2012 4:41:00 AM

CanJS and other similar JavaScript frameworks are great for creating modular applications and keeping source files small and cohesive, but we all (should) know that when it comes to web site performance, it's important to keep the total number of downloads to a minimum. This becomes especially important when you start to factor in mobile devices, where the cost per connection is much higher than on desktop browsers.

Luckily, CanJS (and most of the others, I might add) includes the ability through StealJS of compressing and concatenating all of the source files for your application into a single production.js, as well as doing the same for your CSS files, into a single minified production.css.  When you use the generators which come with DoneJS / JavascriptMVC to create a new app, you'll notice in the folder structure a scripts folder, which contains a build.js and build.html. Steal uses Rhino to load the build HTML file, which includes a single call to load the main steal.js core and bootstrap your app (e.g. ../steal/steal.js?yourapp). It then tracks all of the imports, JS, CSS, and EJS, and bundles them into either the production.js or the production.css. Note: the folks over at Bitovi been busily cooking up a new "packages" feature which I haven't yet had the chance to play around with, which promises to shrink the size of the monolithic production.js into more on-demand modules.


Read More