Template Method Pattern using JavaScript callbacks

JavaScript callbacks are usually discussed in terms of asynchronous behavior. Primary examples given of this are event handlers in the browser or XHR. Callbacks are used so much in JavaScript that there is even a new evil in the world, Callback Hell.

Callbacks are very useful for more familiar coding tasks and it doesn’t have to be about asynchronous services, or journeys into the compsci land of Functional Programming, or Monad this or that.

Template Method Pattern
The term ‘template’ is not related to templates as used in text substitution libraries. This pattern is used when an algorithm or operation has common behavior and the uncommon behavior can be ‘substituted” or deferred by various means.

Though this pattern is usually presented in terms of Inheritance in Class-based Object Oriented Programming, that is just an implementation example. JavaScript can and does implement this pattern; it is just not called by that name.

Simple example
Lets give a very very simple example of what this means. Lets say you have some widget on a web page that presents to the user some recipe based on some input conditions or properties. For simplicity, lets assume that this can be done in one function that consists of a series of statements that are the same except for a few steps.

The usual way to do this is to create multiple functions that change the widget in the required manner, or create one function that uses conditional statements to invoke the correct steps that vary for each type of recipe.

Below in listing 1, we have the behavior for the widget. Some statements come before and after the call of the doSomething() function. The problem with this code is that if we have to do different things, we must complicate the conditional or create other widgetRecipe type of functions that perform the recipe differently.

Listing 1, psuedo code of conditional use
functions widgetRecipe(){
statements before
statements after

Example with callbacks
Since we have a process that differs in only one or more “steps”, the Template Method Pattern is applicable.

The recipe function in listing 1, can be simplified by replacing the conditional statement with the actual function to invoke. Functions in JavaScript are objects, so can be assigned to a variable.

In listing 2, we use the argument name secretSauce for that function that will be passed in. Some tutorials out on the web use the name ‘callBack’ to make it obvious that this is a callback function. Note how the passed in function is invoked, using “()”.

Listing 2, psuedo code using callback
functions widgetRecipe(secretSauce){
statements before
statements after

We can define that function as: var bitterSauce = function(){ alert(“Add a dash of pepper”);}
Then invoke widgetRecipe like: widgetRecipe(bitterSauce);

One famous programmer, Chuck Moore, inventor of the FORTH language, said: don’t decide, compute. Here the function is computed elsewhere, perhaps a table of some kind.

Of course, you can have multiple callback arguments to a function, even an array of callback functions. You can even inline callback functions or make them anonymous. And, a callback function could have arguments too.

Used in certain ways, a Callback function is a Closure. This can have, if not used correctly, very subtle issues. In addition, if the callback function uses this internally, you have to invoke it a certain way.

Some of the links below give more information on how to use callbacks and things to watch for.

Real world use?
I am almost done solving a programming problem where I have a ‘Callback Hell’ situation, and to make matters worse, the various slices of this hell needed to be reused. I think I solved it by abstracting out the variable behavior into a callback. Lets see if it survives code review.

Since JavaScript is Object Oriented, but not via Classes as in Java, it can do inheritance, via prototypes. Thus, the Template Method Pattern can be implemented using prototypical inheritance. This approach is present here, JavaScript Template Method.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Google Play cannot access music on SD card

There are reports that on the latest Android devices you can now store your music on an SD card and the Google Play Music (GPM) app can play them.

This is incorrect. GPM can store cached music from its own service to an sdcard. That way you can play them offline without losing valuable memory on your device. It is also Google’s way to lock in content and services; maybe due to industry legalities?

Another music player app, Poweramp, can play music from an SD card.

Anyway, if the above is incorrect, please respond with directions on how to copy music from a device onto an Android phone and allow Google Play Music to access it.

I tried all this on a very recent tablet running KitKat.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

‘The Big Bang Theory’, funny?

I guess I’m a geek. I’m into tech, read non-fiction science books, have more than two monitors in front of me at home, have a dull smart-ass sense of humors, etc. So I should love The Big Bang Theory show on TV. Everyone at work is always raving about it. Yet, I don’t. I was able to watch it once for five minutes.

I finally figured out one reason why. Its that obnoxious laugh track. It is so intrusive, and as loud as the commercial breaks. Why? Supposedly there is a studio audience.

There are YouTube videos showing some episodes without the laugh track. This made it even more evident another reason I don’t like the show, the characters are unlikable. Unlike with the “Seinfeld Show”, where you wouldn’t want to associate with the characters, but they were funny, with TBBT, you don’t want to associate with them, and they are not funny. Now that is funny. Que the laugh track.

I think the show is really aimed at faux geeks. Like what real geek or nerd would need to be told when to laugh? And in that sense maybe they should just create an app for that. When a supposedly funny thing is said on the show, the app can signal people when to laugh. Better yet, the app can be social, oooooooh, now you can signal all your true friends with a “haha”. I hereby copyright this idea.

Just to make sure I’m not weird, I searched to see if I’m alone in my view. Nope! Here is one example: 11 Reasons Geeks Hate The Big Bang Theory

And great shows like “Almost Human” get canceled? Maybe they should have added a laugh track.

*The other day I said that instead of developer I should have been a Tooththologist. Get it? Dentist. Oh well, you had to be there.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

dojo on.emit won’t work in FireFox?

Seems simple enough, using the Dojo toolkit click a div and trigger an anchor tag on the page.

The code in listing 1 below works on:
Chrome version 35
Internet Explorer 11

Won’t work on FireFox ver 30.

Dojo’s dojo/on module has the emit() method that provides a normalized way to dispatch events. It is supposed to be cross-browser.

From the info on this page, Disabling native events when using Firefox with WebDriver, it sounds like Dojo may, like WebDriver, be using FireFox’s native event facilities and failing.

<!DOCTYPE html>
<html >
    <script>dojoConfig = {async: true, parseOnLoad: false}</script>
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"></script>
    <style media="screen" type="text/css">
        .bold {font-weight: bold;}
        .box {border:1px black solid;margin-top:4em;margin-left:8em;}       
                var nde = query('#div123');
<body class="claro">
    <div id="div123" class="bold box">click me!</div>
    <div class="box">
     <a id="target" href="javascript:alert('hello')">will trigger this link</a>

Listing 1

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Groovy is a language

A few years ago I wrote a few Groovy language scripts to help with some configuration and testing needs. I discussed these with my co-workers. But, lo and behold, for weeks they thought I was saying that the scripts were Groovy, as in cool, not Groovy as in the JVM based dynamic language. They never heard of Groovy. We had a big laugh.

This situation made me think again that developers have differing interests in their field. No offense, but a certain percentage just go to work and do what is required and punch out at the end of the day. It is Java today, but it may as well be COBOL, there is always opportunity to copy and paste and do the minimum to “solve” the current business stakeholder requirements.

I’m sure this is only true for some corporate IT environments which have a top down rigid structure and legacy applications. Introducing change into the software process and tools is difficult. Stability and safety is paramount. Thus, the worker who toes the line and is not disruptive is an asset. Sure, there is talk of change and new techniques, but at the end of the day: “no, we can’t use that, we have this.” The new web businesses and open-source projects are different, of course.

BTW, Groovy is now slowly being used in my company. But, not sure many have heard of it yet.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Java reflection on a message using MethodHandle

Some code I was looking at recently had an overloaded method and each method just accepted a different type argument. With Generics there are probably ways to reduce this clutter, or not.

Anyway, I started thinking about the original concept of OO; it was all about ‘messages’. But what is the difference between a ‘message’ as per Alan Kay and Smalltalk and a method’, née “subroutine” in Java? I guess it’s intent, design, and the other features of OOP?

Following this line of thought I wound up looking at how to invoke a public method on a class and that method in turn delegates to private methods that match the data within the message. The public method is the destination of a ‘message’. So I searched the web and found mention of the usual Reflection, Proxies, instanceof, and finally MethodHandles. The package java.lang.invoke contains the new approach to reflection introduced in Java 7. MethodHandle is the new class that makes all this possible.

Not much on MethodHandle on the web (search instead for: java “method handle”), but the API docs are very good and this blog post, “A glimpse at MethodHandle and its usage”, is all you need to get started. Below in Listing 1, a simple example is shown. Class Server has a single method that takes a String message and a parser.

The message contains the data that is used to determine the actual method to invoke. Simple, but the point is that this could be a JSON/XML string or some other data container. The parser is just me experimenting with callbacks, here the callback gets the data in the message. Yeah, I know, if the client can get the data, why not just send it? It’s just a ‘hello world’ example.

In listing 1 the Service class exposes one method, send.

public class Service {	
	public Object send(String message, Parser parser) throws Throwable {
		Object pData = parser.parse(message);		
		MethodType methodType = MethodType.methodType(Object.class,pData.getClass());		
		MethodHandle mh = MethodHandles.lookup().findVirtual(Service.class, "call", methodType);		
		Object actual = mh.invoke(this,pData);		
		return actual;

	private Object call(String data) {
		System.out.println("in string method ...");
		return "string data";

	private Object call(Integer data) {
		System.out.println("in int method ...");
		return new Integer(42);
	public interface Parser{
		public Object parse(String s); 

Listing 1

In listing 2, the Main class sends the message to the Service class.

public class Main {    
    public static void main(String[] args) throws Throwable {
        Service app = new Service();
        Service.Parser parser = new Parser();   

        Object actual = app.send("some text", parser);
        assert actual == "string data" : "Should have returned 'some data' string";
        actual = app.send("number: 42",parser);
        Integer expected = 42;
        assert (actual.equals(expected)) : "Should have returned Integer with value 42";
    static class Parser implements Service.Parser{                  
        public Object parse(String s) {
                Integer i = Integer.valueOf(  (s.split(":")[1]).trim() );
                return i;
            return s;

Listing 2

Not earthshaking of course. The output is:
in string method …
in int method …

The example is somewhat like the javax.servlet.Servlet class, only one method, service(ServletRequest req,ServletResponse res) is also used, and usually one invokes other methods based on the “message” params.

I can see the use of MethodHandles leading to some powerful mojo.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Mobile music briefcase using BitTorrent Sync

Streaming, cloud, social, …. Nice stuff! But, what if your behind a firewall and have to rely on the local storage on your mobile device? I was thinking on how to solve this situation, a listen later briefcase.

Use Case

  • You want to select music files to listen to on a device.
  • Costs, Security, and bandwidth limitations preclude streaming.
  • Security will not allow use of thumb drives and other ‘attachable’ devices.
  • You should be able to delete these files.
  • Removing them should not delete the original files.
  • These files should be quickly and securely transferred.

This approach is just a modern version of the “CD storage case” we used in the old days to store some tunes for the long drives.

One way of implementing this is using BitTorrent Sync (BTSync). Basically we create a “briefcase” folder where we will drop copies of what we want to listen to later. To make this easier to follow, I’ll use the origin as being a Desktop PC. We also create a folder on our mobile devices that will be synchronized with the other folder. BTSync will handle the process of keeping these folders in sync. And, since BTSync is so fast, the sync will happen as the files are dropped on the briefcase folder.

    PC                          Device
+--------+     +---------+     +-----+
| Music  | === |Briefcase| ~~~ |Music|
+--------+     +---------+     +-----+

Though mobile example is used here, the device could be anything, even an automobile with advanced music system. While it is parked in front of home, it could be “charging” on some tunes.

How to setup
1. Of course, you have to first have BTSync installed on both the PC and the mobile devices.
2. Then on your PC run BTSync and choose “Add folder”.
3. Click ‘Generate’ button to generate a secret key.
4. Click ‘Browse’ to navigate to the folder to use as the Briefcase. (I used “Music-BTSync” as the name).
5. On mobile devices create a folder that you want to use as the listen later storage. (I created a “Music-BTSync” on my external SDCard).
5a This folder must be accessible to your mobile music player application.
6. On your mobile device run BitTorrent Sync and choose ‘Add Sync folder’.
7. Use the folder created above.
8. Now we have to get the secret read only key from the PC to the mobile device.
8a. On the PC, share the “Briefcase” folder. By using the mobile option, you can generate a QRcode.
8b. On mobile device, scan or enter that key.
9. On the mobile device, on BTSync’s list “MY SYNC”, the new folder should be listed (mine was music-BTSync).
10. Click the settings sprocket to the right of it.
11. Check on “Automatic sync”

Loading: Just copy an mp3 into the Briefcase folder on the PC. It should immediately be copied to the mobile device. With a File app on the device you should be able to see it in the sync folder.
Deleting on PC: Delete the file from the Briefcase folder on PC. It should be deleted automatically on the device.
Deleting on Mobile: Load another file on PC, it gets copied. Now delete that file on the mobile device only. It should remain on the PC. This is because of the use of the ‘Read only’ secret key. To change this behavior, do the sharing using a regular secret key.

This where the problems start. The ability to access and automatically refresh a changed folder varies among portable music player apps. Some like Google Music Player, for example, doesn’t even expose the concept of file folders and other nice to know configuration settings. Poweramp is better in this regard, with some prodding it recognized the new synced files. Still looking for a player that will behave with this sync process.

Various music apps have ways of using caching to store music. They also have various sync capabilities. However, these could be difficult to use in this scenario. Plus, the various parties are trying to monetize content, so optimizing the use of your private music collection is not a great concern.

Does it work?
This morning I put gigabytes of music on a folder on my PC, BTSync synced it to my phone. Now I’m listening to Black Sabbath and Barry White at work. How cool and eclectic is that?

Microsoft Windows used to have a Briefcase feature. It was removed in Windows 8. This was a limited two-way file synchronization process. With BTSync, there can be many folders in sync. For example, multiple mobile devices can sync to the same PC or home folder.

Tested on
Galaxy Note version 1
Android 4.1.2
BitTorrent Sync v1.3.21.0

Windows 7 64bit
BitTorrent Sync 1.3.94

Related links

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Groovy Object Notation using ConfigSlurper

In an old post in 2010 I was exploring the idea of using Groovy syntax as a data interchange format, GrON, just as JSON is a “subset” of Javascript.

Just noticed that Groovy has, since version 1.5, the ConfigSlurper class. “ConfigSlurper is a utility class for reading configuration files defined in the form of Groovy scripts. Configuration settings can be defined using dot notation or scoped using closures.”

Example use of ConfigSlurper

We can store a configuration found on the ConfigSlurper API page into a section of an Inix file:

    grails.webflow.stateless = true
    smtp {
        mail.host = 'smtp.myisp.com'
        mail.auth.user = 'server'
    resources.URL = "http://localhost:80/resources"

Then to read it we just load that section and parse with a ConfigSlurper. As shown below.

import static org.junit.Assert.*;
import org.junit.Test;
import com.octodecillion.util.inix.Inix

class ExampleSlurpInix {
  public final void test() {		
  	def scriptString = new Inix('resources\\test-data.inix')

  	def config = new ConfigSlurper().parse(scriptString)
  	assert config.toString() == 
  	'[grails:[webflow:[stateless:true]], smtp:[mail:[host:smtp.myisp.com, auth:[user:server]]], resources:[URL:http://localhost:80/resources]]'

That’s one of the application types I was envisioning GrON to be used for: An easier format to store and transfer configuration, test, and other types of data. The issue is that as noted in the original post, this is insecure for use as data interchange unless a GrON parser that does not execute Groovy is used.


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Mock Java time and date using JMockIt

Example scenario: A class under test (CUT) gets the current date and uses collaborating classes that invoke month dependent rules. How can it be unit tested?

Sure you can get the current date in the test and set the month. But, the CUT and all of its collaborators may also get the current date. Without changing the environment system time how will you force the month to be, for example, December?

That this is a problem may indicate there is a code smell in the design, but in a non test infected group, tests are added, if at all, after the code is shipped. Thus, changes are scheduled for the next agilefall.

One easy approach is to just change what Calendar.getInstance() gives you. With JMockIt this is very easy. Just put something like the anonymous mock (see lising 1) in the test method.

Source code

  * Test December rules.
  * @author J. Betancourt
public void should_invoke_December_rules(){
    new MockUp<Calendar>() {
    	public Calendar getInstance(Invocation inv) {
    		Calendar cal = inv.proceed();
    		cal.set(Calendar.MONTH, 11);
    		return cal;



Listing 1.

Note that within the mocked method I still have access to the mocked class instance cause I also included an Invocation as the first argument, thus I invoke that to get the calendar as usual, then I change the month. Kind of like an Aspect ‘around’ advice.

JMockIt performs instrumentation via java.lang.intrument and the ASM library, so even collaborating objects will use this Mocked instance for the life of the test. This is a big feature offered by JMockIt compared to other Mocking frameworks afaik.

This should be applicable to mocking other temporal methods like System.currentTimeMillis(), and that in itself effects other methods to get date and time.

Mocking JDK classes may have side effects on running tests. In one of my tests, the java.io.Printwriter was mocked. This caused Eclipse to disconnect from the running JUnit test. The solution was: right after executing the method that involved the use of the writer, doing a mock tearDown(). This was on a JMockIt version 1.2. It probably changed in the latest version.

Shouldn’t time be accessed from a testable configurable component?
Getting time related data via Calendar.getInstance(), System.currentTimeMillis(), or other JVM provided facilities is bad practice in some cases. It is similar to using “new “, creating hidden object graphs, and making testing difficult. A better approach is to centralize the time related access through a service or utility class. One benefit of this is that to change the environment date for a ‘system’ test, you don’t have to change the vacuum tubes in the mainframe system, just change the calendar access via configuration.

1. This worked on JDK 1.6 using JMockIt version 1.2. Is it a good technique? Let me know.
2. After I wrote this I searched for this topic and found many good blog posts discussing this subject. Why didn’t I find these when I initially had the problem?

Further reading

Something I’m listening to while I code …

“Become, Seem, Appear” performed by Oregon on their CD “Oregon In Concert”. On YouTube
Price for a new CD is $5,545.60. I better find my CD and store it in a safe place!

“The Silence Of A Candle” performed by Oregon on their CD “Oregon In Concert”. On YouTube.

Audio CD (November 6, 2001), Original Release Date: 1975, ASIN: B00005RDJS

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Continuous testing with Infinitest

With Infinitest every time you make a change in your code, the JUnit tests will be executed. How cool is that? Infinitest will run in IntelliJ or Eclipse.

I tried it in a very small Groovy project, and it worked. Of course, Groovy compiles to Java, but in an IDE things get complicated.

Unfortunately on a large Java project it did not seem to work, and the web site does not have enough information to determine what the issue is. For example, the status line is showing that it is running a test (unrelated to the recently changed code) and it shows “(400 remaining)”, remaining what?

Whether this plug-in works does not negate the concept of Continuous Testing as discussed below.

I installed the plugin using the update site. Made no configuration changes. Then I changed my code, and nothing happened, just a status at the bottom that one test case ran. In fact, I changed the Unit test code to force a failure.

Then I noticed that Eclipse’ Problems view had red ‘x’. Hmmm, I didn’t make any compile related changes. Opening the Problems view showed the actual test failure. Ah, now I get it. The status bar is just like the JUnit status, red or green.

I thought the plugin would reuse the JUnit view in Eclipse. It doesn’t, instead it adds any failures to the Problems view. Thinking about it, that is the correct approach. When your coding the only distraction you need is to know if a change to code broke something, comparable to a compile just getting compile errors.

Bringing up a new view, especially the JUnit view would be heavy weight. When you focus on testing then the full JUnit view is more useful. But maybe this should be a configuration choice?

Anyway, the workflow is just code as usual. The status message at bottom of the IDE will be Green or Red depending on the test run. When red, the Problems view will show the test failure. Unfortunately clicking on the error line will not bring you to the failing test, but this could be that this is a Groovy Project.

Continuous Testing
We don’t edit code then invoke a compile step anymore. Our IDEs do that automatically. Then why have to invoke our unit tests manually? This “Continuous Testing” (CT) approach enables a smoother Test-Driven Development (TDD), maintenance, or refactoring, work flow.

Is there any evidence of this? Some papers on CT are found here.

If you search for the term ‘continuous testing’ there is not much. Usually it appears in the context of Continuous Integration servers. Perhaps its time to make CT more visible so that it’s more available as a development practice.

Great videos on the CT as implemented in Mighty-Moose, a product for Microsoft Visual Studio, are found at continuoustests.
Mighty Moose Demo, a CT product for Visual-Studio.

Mentioning this to any developer will give you immediate “buts”: But my tests take too long; it would be distracting; I change code constantly;…… I sometimes think developers are driven by a little motor in them, but … but … but … buuuut.

The only thing I see in Infinitest to address these concerns is the use of a filters configuration file. You can add multiple regex to avoid tests that you don’t want to run all the time.

I’d like the opposite too. The ability to easily define what I am interested in testing, inclusion filters. On a large code base you may be concentrating on a few packages, so it should be easy to just add those to a filter without having to drag out the “Mastering Regular Expressions” book.

Though Infinitest is good, why isn’t running a configuration on configured type of changes supported in Eclipse? Is there is a direct way to invoke the JUnit plugin via adding a new custom “builder” to Eclipse? A builder in Eclipse is triggered by resource changes. So, on source code change this builder would have to run an associated ‘JUnit run configuration’.

May 1, 2014: While looking at the Infinitest source I noticed they use AssertJ. Another great library. So many cool things. So why is code bug ridden? Just joking.
May 6, 2014: Tried Infinitest in a larger project. Did not work.

Tested on
Eclipse Kepler Service Release 2
Groovy Plugin 2.9.0
Groovy compiler 2.2
Infinitest 5.1.110
Windows 7


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.