The amazing adventures of Doug Hughes

Archive for the ‘Blog’ Category

What is Java?

This is an article I wrote for my Java class at The Iron Yard. It’s being published here with permission.


Writing Software in Java

To write a program is to “speak” in a language a computer can understand. We don’t actually “speak” to a computer, but we can write messages that it can understand. These words aren’t in a natural language, though. Instead, they’re in a programming language.

My class at The Iron Yard is primarily about the programming language Java. Java is just one of hundreds of programming languages, but it has the distinction of being the most popular.

To a fresh eye, programming languages often look like gibberish. For example:

int total = scores.stream().mapToInt(Scoreable::getScore).sum();
return total / scores.size();

This Java code could be translated into english as something like, “Calculate the average score from a set of scores.” This example is intended to illustrate a point: Java isn’t english.

(more…)

Getting Help

This is an article I wrote for my Java class at The Iron Yard. It’s being published here with permission.


Getting help

Often you will run into problems where your debugging strategies just don’t give you anything useful to work with. In these cases, you have to turn to other resources.

Craft a good question

The first step in getting help is to figure out the correct question to ask. Vague questions don’t lead to specific answers.

Instead of saying “when I click this button, it doesn’t work,” explain what you hoped it would do and what it actually does. To do this, you have to understand what it is you’re actually trying to accomplish. It might be a surprise to realize that sometimes you don’t know this. (more…)

Debugging Tools and Strategies

This is an article I wrote for my Java class at The Iron Yard. It’s being published here with permission.


If debugging is the process of removing bugs from code, then what is programming?

Undoubtably you’ve already run into a few bugs in your code. Figuring out the cause of these problems can be a frustrating and tedious experience. But, it can also be incredibly rewarding. There’s nothing quite like the feeling of finally squashing a particularly challenging bug.

As a professional programmer, you’ll be spending most of your time debugging. It’s virtually impossible to write code that works perfectly the first time. In fact, code that appears to work correctly on the first test will probably start to set off your mental alarm bells. The worry being, “what am I missing?!” (more…)

Today's Office Hours: Setting Up a Local ColdFusion Development Environment

I’m back again this week with another installment of Office Hours. Last week’s Office Hours went well and we all learned a bit about Git. This week Phillip Senn has asked to go over how to setup a local development environment for ColdFusion.

The show starts here at 3:30 pm today, Thursday November 8th. I’ll post a link to the Google+ Hangout and a link to the streaming video in this post around 3:15. I hope to see you this afternoon.

Join the hangoutor watchthe YouTube stream:

Office Hours: Git

I announced on Wednesday that I was going to start holding Office Hours as an open forum where I can help someone work through a problem they’re having. This week I’ll be working with Phillip Cenn to get him started with Git. Our session begins at 3:00PM ET. Up to eight people can join into our Google+ Hangout. Everyone else is welcome to watch the life stream below.

Google+ Hangout:https://plus.google.com/hangouts/_/20d73fe568f5f674d1d7370072e2a2358ae3af26?authuser=0&hl=en

And this is where the live video stream will be:

Office Hours -or- I'm Unafraid of Making a Fool of Myself in Public

A couple of weeks ago I was honored to speak at the very excellent NCDevCon. While there I had a very interesting conversation with a guy by the name of Phillip Senn. Phillip expressed that he was looking for a mentor, someone who would push him towards better development practices and help him get unstuck when he encountered a problem he couldn’t get past. For example, he needs a bit of a push to get started with Git. He’d like to see how people setup their development environments, etc.

While I don’t really have the time or inclination to be a traditional mentor, the request got me thinking. I certainly don’t know everything there is to know about programming, but over the years I’ve gotten really good at learning new things quickly. I feel comfortable being thrown (or throwing myself) into new technical situations. I recognize patterns and can Google with the best of ’em.

So, Phillip and Iconceivedof what he dubbed “Office Hours”. The plan (at least for now) is for me to have a scheduled time to do a screencast where I help someone get started with or learn something new for free. I will be doing this in a Google+ hangout and streaming it live to my YouTube channel. I also plan to make archived sessions availableonline.

The first Office Hours will be this Friday, November 2nd from 3 to 5pm EST. Right before the office hours is set to begin I’ll publish the Office Hours Google+ Hangout URL, the live stream video, and any other relevant information. Those who are interested (up to 8 additional people) can join in the hangout and help out or follow along. Anyone else will be able to watch the video in real time.

For this first I will beworking with Phillip to help him get started with Git. Truth be told, while I use Git, I’m no Git guru. My intention, however, is to help him get it installed, create a new repo, commit code, branch, merge, etc. Basically, to give him a tour of as much of it as I can. I imagine this will be rather organic with a few false starts and dead ends before we really get anywhere. To me, part of what makes Office Hours interesting is that I’ll be learning while I do it too.

I want to make Office Hours a weekly event. I also want you to feel free to email me with your requests and suggestions. For example, perhaps you want help getting started with Node.js (or anything else). Just send me an email and, if I choose your topic, we’ll schedule a time for your Office Hours. I would request that you not limit your questions to what you expect me to know. If the topic is new to me I’ll work ahead and be ready to hit the ground running in our Office Hours session.

So, do you have any thoughts or suggestions? I’d love your feedback on the concept.

 

 

Doug @ NCDevCon – Javascript: things you never knew you didn't know

NCDevCon LogoI was pleased to find out that I’ve been invited to speak at at NCDevCon again this year. NCDevCon is a ColdFusion and web development focused conference event held annually at theCentennial Campus of NC State University in Raleigh, North Carolina. The even is put on by the fine folks at the Triangle ColdFusion User Group, aka. TACFUG.

Here are the details of my session:

Javascript: things you never knew you didn’t know.

So, you think you know JavaScript? I think not! There are a ton of small features hidden under the covers that many developers either don’t know about or don’t know how to use. This topic will go over an ad-hoc list JavaScript related goodies that I’ve picked up over the last year or so, including typed arrays, accessors, array folding, object inheritance, various tips and tricks, and more. Many of the topics relate to newer revisions of JavaScript and may not work in older browsers.

I hope to see you there!

Also, thanks again to TACFUG for inviting me to speak again! You guys are awesome.

Unborking VPN on OS X

For those of you using the inbuilt VPN features on OS X, you may have noticed that from time to time it will stop wanting to connect. For me, pretty much any time I disconnect from VPN, the next time I try to connect I will get an unfriendly message that looks like this:

VPN Connection An Unrecoverable error occurred. Verify your settings and try reconnecting.

This happens to me way too often. It can happen when trying to connect, it can happen after disconnecting and then reconnecting, it can happen without any apparent provocation.

In the past it seemed like the only option was to completely restart OS X. As you can imagine, this is not an acceptable solution to someone who keeps a lot of apps open, needs to use VPN frequently, and who doesn’t wish to waste time rebooting for no good reason.

Thankfully, Joe Bernard was able to track down the solution and was kind enough to share it with me.

Apparently there’s a process in OS X called “racoon”. Racoon is in charge of VPN connections. Here’s what Apple’s man pages have to say about it:

racoon is used to setup and maintain an IPSec tunnel or transport channel, between two devices, over which network traffic is conveyed securely. This security is made possible by cryptographic keys and operations on both devices. racoon relies on a standardized network protocol (IKE) to automatically negotiate and manage the cryptographic keys (e.g. security associations) that are necessary for the IPSec tunnel or transport channel to function. racoon speaks the IKE (ISAKMP/Oakley) key management protocol, to establish security associations with other hosts. The SPD (Security Policy Database) in the kernel usually triggers racoon. racoon usually sends all informational messages, warnings and error messages to syslogd(8) with the facility LOG_DAEMON and the priority LOG_INFO. Debugging messages are sent with the priority LOG_DEBUG. You should configure syslog.conf(5) appropriately to see these messages.

In a nutshell, Racoon gets borked. Sometimes this means that the racoon process needs to be restarted, but in my experience 99% of the time it means that it’s not actually running.

So, you can restart racoon from the terminal like so:

sudo /usr/sbin/racoon

I’ve also found that sometimes you need to restart the various networking interfaces you’re using. Because of this, I ended up writing a shell script I call fixnetwork.sh:

sudo ifconfig en0 down
sudo ifconfig en1 down
sudo ifconfig en0 up
sudo ifconfig en1 up
sudo /usr/sbin/racoon

I put this in my home directory, set it to be executable, and can run it like so:

~/fixnetwork.sh

Works like a charm for me. No more reboots to fix borked VPN connections! Productivity, here I come!

CoolBeans – an IOC container for Node.js

I just finished writing my first publiclyavailablemodule for Node.js. If you’re not familiar with Node.js, well, I’m just sorry to hear that. Go learn.

The module I wrote is called CoolBeans. (Thanks for the name, Mr. Chris Peterson).CoolBeans is an Inversion of Control (IOC) / Dependency Injection (DI) library for Node.js. CoolBeans is loosely basedon ColdSpring for ColdFusion and Spring IOC for Java. It’s a single js file and currently appears to be quick and easy.

To install:

npm install CoolBeans

To use CoolBeans you simply create an instance of the CoolBeans and load the configuration file like this:

var cb = require("CoolBeans");
cb = new cb("./config/dev.json");

The above code is the only require function you should need in your entire application. Once you’ve required CoolBeansyou need to create a new instance and pass in the path to its’ configuration. This is shown above.

Once you have the fully loaded CoolBeans you can use it to quickly create fully configured singleton objects based onits’ configuration. The config file for CoolBeans is a JSON file so the entire thing is wrapped in {}.

Each element in the root of the configuration file is a bean (bean = Java for object) that CoolBeans can create. Here’s anexample:

{
  "fs": {"module": "fs"}
}

This is essentially the same as:

var fs = require("fs");

However, we now only need to define this one time for an application, rather than in each file that requires it.

You can also specify paths to modules that are not node_modules. For example:

{
     "Recipient": {"module": "./entities/recipient"}
}

At the most basic, the above means that CoolBeans will call require for the module and cache the results in a variable named Recipient.

As a relative newb to Node.js, I think I’ve handled this correctly. CoolBeans is a node module which means that NPM will install into ./node_modues/CoolBeans. The actual CoolBeans script is in the lib directory. That means, that from the perspective of CoolBeans your components are three directories above it. For this reason, CoolBeans looks three directories above it for the module specified. So, the Recipient module above actually turns into ../../.././entities/recipient. This has the effect of making the paths to modules specified in the configuration file relative to the root of your module or application. So, if you make a module that depends on CoolBeans and later publish it via NPM I think it should work correctly when used in other projects.

You can get any of the configured beans by calling cb.get(“beanName”) where beanName is the name of the bean you want to get. For example:

cb.get("Recipient");

The above will lazily create the Recipient bean, cache it as a singleton, and return it.

You can get a lot more complex with configuration too. For example, you can specify if CoolBeans should call a constructor and what arguments to pass into the constructor. For example:

"codeGenerator": {
     "module": "./util/codeGenerator",
     "constructorArgs": [
         "foo",
         123
    ]
}

What the above is saying is that when we get the codeGenerator bean, we need to load the module specified then call new on the module and pass in the values specified in the constructorArgs section to the constructor. The above means:

new codeGenerator("foo", 123);

You can also specify more complex values to pass into constructor arguments:

"codeGenerator": {
    "module": "./util/codeGenerator",
    "constructorArgs": [
        "foo",
        123,
        {"value": [1, 2, 3]},
        {"value":
            {
                "foo": "bar",
                "bar": "foo"
            }
        }
     ]
}

Note that while you can specify a string without indicating explicitly it’s a “value”, for arrays and anonymous objects you need to provide an object with a property named “value” whose value is the value you’re trying to pass in. The above could be written more explicitly as:

"codeGenerator": {
    "module": "./util/codeGenerator",
    "constructorArgs": [
         {"value": "foo"},
         {"value": 123},
         {"value": [1, 2, 3]},
         {"value":
             {
                  "foo": "bar",
                  "bar": "foo"
              }
         }
    ]
}

Strings, numbers, arrays, and anonymous objects are not the only things you can pass into constructors. You can also specify other beans that could be passed in. For example, let’s say we had a database configuration object you want to pass into any object that is used to access data you could do the following:

"mysql": {"module": "mysql"},

"dbConfig": {
    "properties": {
        "host": "server.hostname.com",
        "port": 3306,
        "user": "mysqlUser",
        "password": "password123",
        "database": "foobar"
    }
},

"recipientDao": {
    "module": "./db/recipientDao",
    "constructorArgs": [
        {"bean": "dbConfig"},
        {"bean": "mysql"}
    ]
}

The mysql bean is simply the same as saying require(“mysql”). The dbConfig is an anonymous object with properties specified (more on this in a bit). When the recipientDao (dao = data access object) is created, CoolBeans will see the “bean” property and will create and pass into the constructor the fully-constructed dbConfig object and the mysql object. Here’s what that recipientDao might look like:

module.exports = function(dbConfig, mysql){
    this.listRecipients = function(userId, callback){
        var client = mysql.createClient(dbConfig);
        client.query(
        "SELECT id, name, addressLine1, IfNull(addressLine2, '') as addressLine2, city, state, zip, taxDeductible, created, updated, 0 as netDonations " +
        "FROM recipient " +
        "WHERE userId = ? AND deleted = 0 "+
        "ORDER BY name",
        [userId],
        function(err, results, fields){
            client.end();
            callback(results);
        });
    }
}

Note that there are no require statements. The object just gets its’ dependencies when it’s instantiated and can immediately use them. These dependencies are also automatically singletons.

Also note that if you want to use a transient object you would still create an instance of it the way you always have.

I also mentioned above that CoolBeans can be used to create create and populate anonymous objects. For example:

"dbConfig": {
    "properties": {
        "host": "server.hostname.com",
        "port": 3306,
        "user": "mysqlUser",
        "password": "password123",
        "database": "foobar"
    }
}

This is a somewhat long-winded way of saying

dbConfig = {
    "host": "server.hostname.com",
    "port": 3306,
    "user": "mysqlUser",
    "password": "password123",
    "database": "foobar"
};

However, once this object is configured in CoolBeans you can easily pass it into other objects when they are created.

You can also specify properties for not-anonymous objects. You can also mix and match constructorArgs and properties.

For example:

"creditCardDao": {
    "module": "./db/creditCardDao",
    "constructorArgs": [
        {"bean": "dbConfig"},
        {"bean": "authorize"},
        {"bean": "mysql"},
        {"bean": "CreditCard"}
    ],
    "properties": {
         "service": {"bean": "service"}
    }
}

When CoolBeans creates the creditCardDao it will first load all the beans specified in the constructorArgs. It will then create the creditCardDao and pass in the four already-created beans to the constructor. Once the object is constructed it will set the service property on the object to the specified service bean. Note, CoolBeans will look for a setter and use that if it can find it. For example, in the service property above, ColdBooks will first look for a function named setservice (note that this is case sensitive). If it can find it, it will pass in the service bean to that function. If not, it will simply set a public property on the object.

There are a few other interesting capabilities of CoolBeans:

Beans don’t have to be lazily loaded. You can set a bean to load when the container loads. For example:

"dateFormat": {
    "module": "./util/dateFormat",
    "lazy": false
}

Also, if you have a factory that is used to construct other objects, you can specify this using the factoryBean and factoryMethod properties. For example:

"knox": {"module": "knox"},

"s3client": {
    "factoryBean": "knox",
    "factoryMethod": "createClient",
    "constructorArgs": [
        {"value":
            {
                "key": "myKey",
                "secret": "mySecret",
                "bucket": "myBucket"
            }
        }
    ]
}

The above s3client bean is configured so CoolBeans uses Knox to create it. The constructor args are passed into the factoryMethod as if it were a constructor. The above essentially boils down to:

s3client = knox.createClient({
    "key": "myKey",
    "secret": "mySecret",
    "bucket": "myBucket"
});

The really nice thing about CoolBeans is that it lets the objects in your system stay focused on what they do best. It shouldn’t be your object’s responsibility to know what they need to work. They should simply get what they need to work when they’re created. CoolBeans also helps avoid situations in complex apps where you have dozens of lines of code just getting dependencies created just to create one object that otherwise happens to have a lot of dependencies. Lastly, CoolBeans allows you to easily change how your application is configured in different environments.

If you’re a Node.js developer I’d love to hear your thoughts on CoolBeans! Heck, I’d love to hear your thoughts even if you’re not.

Soliciting Stack Suggestions

I am part owner of another web company, which shall remain unnamed. A few years ago I (and a couple others) wrote their current web application. Initially the application was sufficient, but over time weve run into some limitations of the applications architecture.

For example, this was written before I realized that writing fat controllers was a bad idea. In fact, this is such an early project that it ran on a pre-1.0 version of Model-Glue. There was no concept of ColdSpring at all. It used a stand-alone tool to generate the data access layer (which eventually inspired me to write Reactor, which is now also defunct).

Furthermore, the business requirements have changed over time. The site was initially built to satisfy one specific use case: selling a given product in one specific way.

Overtime the business team has requested new features that are difficult or impractical to implement in the existing architecture. For example, they want to be able to edit and add content on the site without involving me or any other technical staff. They also want to start selling other products in slightly different ways from their original plans.

Long story short, its time for a rewrite. I now need to choose a stack of frameworks, languages, etc, that are appropriate for this project. Im afraid that Im over complicating things and was hoping that the general Internet might have some suggestions.

So heres what I can say I think I need:

  • I need a Content Management system that allows the business and marketing team to add pages to the site, edit content, and generally manage the Information Architecture of the site. They need this because I am not as responsive to their requests as they would like. I do have Alagad to run on a day-to-day basis.
  • I need to write custom code that integrates with the CMS to support the unique business requirements of this application. This would include management of the product being sold, reporting, integration with other third party systems, and more. There really are not any off the shelf tools that I could use to replace this custom work. I really want to write this code to leverage IOC and OO. One of the biggest challenges weve had with the existing site is that it is mostly pseudo-OO, which makes changes and enhancements more difficult to implement. Hence, the rewrite we need to do!

For a content-heavy site Id typically use an off-the-shelf CMS like Mura or Farcry. For a custom-code-heavy site Id typically use Model-Glue and ColdSpring to help me structure clean maintainable code.

The problem comes in when I try to do both: Use a CMS and write clean maintainable code.

My experiences (though dated) with Farcry pretty much suggest that you have to write code Farcrys way if you want to do anything custom. I honestly just dont like Farcrys approach.

Ive also worked with Mura quite a bit over the last year on a different project. For that, we used Mura as the CMS and wrote a Mura plugin to run Model-Glue events based on how a content element is configured in Mura. Its my opinion that this was a reasonably good stack.

Ive even considered writing my own CMS that integrates nicely with Model-Glue, but thats really a non-starter.

For this rewrite I have been planning to use the following stack:

  • Mura for content Management.
  • ColdSpring, Model-Glue and Hibernate for custom code that will be run within Mura via a Mura plugin.
  • JQuery
  • This would be run on Railo, which would run in Tomcat.
  • Eventually this would be deployed as a WAR to Amazon Elastic Beanstalk or another Java Platform-As-A-Service provider.

And while that seems like a reasonable stack to me, I have a few concerns:

  • Overall performance: The Mura + Model-Glue project I mentioned earlier has been suffering general performance woes. Many of these could be attributed to the sheer volume of custom code and the fact that it is not running the latest updates to Mura and Model-Glue. These have mostly been ironed out, but its still an area of concern to me.
  • I might be crazy: To me, the stack above makes sense. However, every other developer who has worked with me on this stack doesnt like it. At all. Some developers dislike Mura. Some developers dislike the Model-Glue integration. Some developers think the stack is too deep. Some dislike using unfamiliar platforms like Tomcat and Amazon EBS. Lastly, very few developers have experience with the complete stack (even when you ignore Tomcat and Amazon).

I know I could build this system using the stack Ive outlined above and Im reasonably sure I can get it to perform well. However, I feel like Im not seeing something that everyone else sees. Ive even had three contract developers quit in the early phases of this project.

Im not sure if my stack should change. Thinking pragmatically, can you think of an alternative approach to this problem? One that lets the business and marketing teams have the flexibility they need and also allows developers to write clean, well structured code, using the latest best practices?

Im open to all suggestions. Ive been considering other languages and platforms. However, I also need to keep in mind my learning curve on any other tools or languages that might be used as well as unique business requirements that may make some choices impractical.

Id greatly appreciate your suggestions. What are your thoughts?

Tag Cloud