Skip to content

James Carr - Rants and Musings of an Agile Developer
Syndicate content
Rants and Musings of an Agile Developer
Updated: 3 hours 2 min ago

Read Only Properties in ES5

Thu, 07/29/2010 - 23:58

I thought I’d take a quick moment to provide some examples of making object properties read only in EcmaScript 5 (and by extension node.js). There’s several ways to accomplish it, so I’ll just iterate over all the different ways.

Object.freeze

The quickest way to make all properties of an object read only is by calling Object.freeze on it. The interesting thing here is that (at least in node.js) no exception or warning will take place if you try to assign a read only property… it will appear that the assignment succeeded when in reality it didn’t.

Let’s try an object with some additional types… nested object and an array.

In this example, we see that the array and object represented by b can in fact be modified, they just can’t be reassigned to something new. It really just locks the reference. However if we loop over each property and freeze each one each will be unmodifiable and the attempt to push an element onto the array with throw an exception stating TypeError: Can't add property 4, object is not extensible.

Define Only a Getter

Another way to make a property read only is by only defining a getter for it. You can do this both via defineProperty or defineGetter

Both will throw an exception on an attempt to reassign them.

Defined as Not Writable Via Property Descriptor

One more way is to define the writable attribute in the property descriptor.

That’s just a quick overview, there’s also quite a few interesting tricks to locking object instances.

Categories: Blogs

Using an SSL Certificate to Make a Secure HTTP Request in Node.JS

Mon, 07/26/2010 - 06:17

I’m documenting this only because I had some difficulty finding info online… the API docs tell you what you need, but it took me a couple hours to get things working and a little known bug that threw my work off track.

So say you want to make a secure request to a website, perhaps a secure API call (as is most common with payment gateway APIs) and you’re using nodejs… what do you do? Assuming you already have a private key and an SSL Certificate handy and that you’ve compiled nodejs with ssl support, I’ll show you how in the following steps. :)

First, place the cert somewhere that your script can access it from, I usually prefer a location like ./certs. Make absolutely positively sure that you have no trailing newlines at the end. You also have the option of embedding it within your script (I’ve seen it done) but I believe this is a poor practice. Still, you have the option of doing that.

Given that your script/app/module is located in the same directory as the certs directory, load the contents of both keys into memory and use the crypto module to create the credentials.

With the credentials now available, we can set up the client and make a request

That is, createClient(port, host, secure, credentials). If everything goes well you should be able to make a request. One thing to watch out for is a trailing newline at the end of your key or cert. Often times I add key.replace(/\n$/, '') to it just to be safe.

Categories: Blogs

Payflow Pro API released for nodejs

Sat, 07/24/2010 - 22:35

On and off over the past couple days I’ve been working on a nodejs module to interact with Paypal’s Payflow Pro API to allow the acceptance of online payments within node.js apps. The feature list is steadily growing and soon I hope to implement the parts of the API that let paypal do the heavy lifting as well as certificate based authentication.

It’s available via npm and installation is a snap. Given that you have npm installed, just type

and bam, it’s now available for use. Here’s a quick sample to get started (this is using my existing sandbox account):

If all goes well, you should see some console output for either success or failure.

Feel free to check out the github repository, especially if you’d like to contribute.

Stay tuned… there’s more to come both from this module and more payment modules I plan to develop. ;)

Categories: Blogs

Defining Getters and Setters in NodeJS

Tue, 07/20/2010 - 02:01

Recently I discovered something interesting while messing around with NodeJS… you can define getters and setters using the ECMAScript5 syntax. For a useless example I could do something like this:

This is kind of boring though because it’s just a more complicated way of doing

however you can notice in the first example you can add any kind of custom behavior to the getter and setter and access it via person.age. Where this does become useful however is if you want to create a read only property, which you can do by just defining the getter alone:

This will print the initial age as expected, however the last line where the attempt is made to set the property will throw the exception “TypeError: Cannot set property age of #<a Person> which has only a getter.”

A small discovery, yet very useful. ;)

Categories: Blogs

links for 2010-07-17

Sat, 07/17/2010 - 15:01
Categories: Blogs

Life in the Fast Lane

Tue, 07/13/2010 - 07:04

When I was growing up I always though that my life would hit some kind of point where everything will start moving very quickly and that I’d have to keep up with the current before I get swept away.

And today I finally feel like I’ve dipped my toe into that point in my life. :)

Our baby girl was born yesterday (a month ahead of schedule) and I’ve been kind of in a rush to get everything put into place. Further, the future is looking quite busy the rest of the year. I’ll be speaking on RabbitMQ at the Gateway Java User Group in September, BDD with EasyB at the Groovy Usergroup and BDD with Scala at Strange Loop in October, plus unspecified speaking arrangements yet to be made at the Rich Web Experience in Florida in the end of November. I’ll also be participating in a Code Retreat September 18th and possibly the Node Knockout in the end of August. I’m sure I’ll be learning a lot more and engaging in more activities before the year is out as well!

But most importantly, I’ll be raising our daughter and learning the lessons of fatherhood firsthand. I’m sure I’ll make a great father and one thing is for certain… I’m in for a hell of a ride. :)

Categories: Blogs

Parsing CSV Files With NodeJS

Thu, 07/08/2010 - 04:24

I’ve been doing a lot with nodejs lately and thought I’d share a small spike I just did for fun to explore some of the features more in depth and gain a better understanding of some of the IO features I haven’t had a reason to use yet. Keep in mind I did this as a spike completed within an hour so it is quite sloppy! :)

I kind of envisioned a really simple API with an interesting catch. A function that takes a csv filename as the first parameter and a function as the second parameter to handle each record read in with each record containing the header names as keys and the values to match them. Something like this

Implementation was quite simple using fs.createReadStream.

Here’s the output that gets printed to the commandline:

An earthquake of magnitude 2.3 in Southern Alaska
An earthquake of magnitude 1.0 in Northern California
An earthquake of magnitude 3.3 in Southern Alaska
An earthquake of magnitude 1.1 in Southern California
An earthquake of magnitude 1.2 in Central California
An earthquake of magnitude 1.1 in Central Alaska
An earthquake of magnitude 5.6 in Bio-Bio, Chile
An earthquake of magnitude 3.5 in Baja California, Mexico
An earthquake of magnitude 2.4 in Alaska Peninsula
An earthquake of magnitude 1.2 in Northern California
An earthquake of magnitude 5.5 in New Ireland region, Papua New Guinea
An earthquake of magnitude 2.0 in Southern California
An earthquake of magnitude 1.5 in Southern Alaska
An earthquake of magnitude 1.2 in Central Alaska

This spike uses a csv file I got off of data.gov containing earthquakes for the past 7 days. Fun stuff. ;)

Categories: Blogs

Doing What You Love

Tue, 06/22/2010 - 15:08

Recently I had an interesting conversation with a couple guys at my client site. One of mentioned he heard about my presentation at a recent user group and also saw my invite I sent out for an internal Domain Driven Design study group and asked “How come you like work so much?” The other guy then said “I think it’s something that is just different about Americans versus people from other countries… they’re workaholics man! I mean, I heard that Ryan always works weekends.”

I simply corrected them… yes, Ryan is a workaholic and in my opinion he is overexerting himself. But I don’t consider my programming, reading, and speaking that I do on the side as work; I just love software development and it’s something that has interested me, both from a technical and social standpoint. It just so happens that my job has me doing something that I love (although dealing with a mess of legacy code isn’t always enjoyable). There’s a huge difference between working all the time and doing what you like to do in your free time; and you won’t catch me “working” at work more than forty hours a week.

I think it’s important that people do what they love to do because at that point their job isn’t just something they do through the week while waiting for the weekend to arrive. One of the best stakeholders I ever worked with on a project was a guy from marketing and guess what he does in his free time? We were out at dinner once when he and the rest of the product owner team was in town he confessed what he does when he gets home from the gym after work: he plops down in front of the TV and watches recordings of commercials that were recorded throughout the day. He doesn’t watch any shows or movies… just the commercials. Another friend of ours is a math professor and she enjoys solving math problems in her spare time. I know I would go nuts if I spent my leisure time solving math problems! But it is what she likes to do and her job just happens to be related to what she likes to do.

Now, I don’t think that in order to be a productive developer (or even just a really good developer) you have to code in your free time all the time and this guy really misses the point. I definitely don’t think that companies should make it a requirement that they hire only people who code in their free time because otherwise they might miss some really good developers who just do what they like to do in their free time. But I think that someone who really enjoys software development and spends their free time improving their skills at their craft and doing it just for fun often happen to be much more remarkable.

Enjoying software development in your free time doesn’t always mean you’re going to really love your job. As Chad Folwer once pointed out, What Would You Rather Be Doing? However I think that when you do something you love you will find it fulfilling and sooner or later you really will either land the job you love or you will motivate radical changes at your current workplace to make enjoyable not just for yourself, but for the people you work with as well.

Are you doing what you enjoy doing?

Categories: Blogs

Friday Scala Kata: Weekday Heptagram

Mon, 06/21/2010 - 05:52

Last week’s scala kata was a bit challenging as it required a bit more advanced scala knowledge than the katas I’ve done so far. Breaking from last week I’ve decided to head back to the basics and dream up a kata to help focus on some of scala’s language features versus a full blown program.

For this week’s scala kata I decide it would be interesting to model the Weekday Heptagram. Conceptually the kata should be to take one of the seven Stellar Objects ( Saturn, Jupiter, Mars, Sun, Venus, Mercury, and Moon) and the hour of the day using the 24 hour scale and deduce what day of the week it is. For example, if it’s the 1st hour of the day and the stellar object is Saturn then it is obviously Saturday. If it is the second hour of the day and the stellar object is Saturn then it is Monday. See the table near the top of this page for additional examples to use for your specifications.

Other operations that could be useful include:

  • identify the dominant stellar object for a given date/time
  • given a day and hour what is the stellar object n hours from now
  • given a specific day and hour, what are the stellar objects for that hour for the remainder of the week?

Hopefully this should be a fun little coding exercise. :)

Categories: Blogs

Scrumbutts Abound

Tue, 06/15/2010 - 18:18

Lately I came across this posting on an agile related mailing list. I know that job postings on these lists are often a low hanging fruit for criticism, but I really just could not help myself.

THIS IS NOT A PM, but a SCRUM MASTER POSITION. The right candidate needs to have background as a technical lead in J2EE AND a recent background in management and/or Project Management

The Scrum Master will be responsible for managing the Scrum activities of 4-6 Java Developers. He/She will need to deliver quality software releases on time and within budget. This person will work closely with development staff to develop delivery plans, monitor and control development milestones, complete software designs and documentation, serve as the point of escalation to resolve technical issues, and will mentor and coach junior developers. They will be accountable for enforcing development standards and best practices, and ensuring the team is delivering on its commitments.

Required Skills:
* Proficiency with Microsoft Project
* Posses a good understanding of Project Management practices, plus good planning and organizing skills
* Be the right mix of management and technical skills, background and expertise
* Have 3+ years experience as a senior software engineer, technical lead, technical project manager, or software development manager
* Have proficiency with the JAVA language and in Web technologies
* Have experience with Test Driven Development practices and their implementation
* Possess strong written and verbal communication skills

Whatever.

You might take this post as saying I dislike Scrum and it’s not. My annoyance is all these high profile companies jumping on the bandwagon and renaming their project manager position as Scrum Master, hiring them by the buttload and teaching them the wonders of gnatt charts, microsoft project, and “managing the development staff” (aka watching over the serfs) and then dumping them onto the open market with freshly minted resumes listing Scrum Master as a position previously held.

Now when a company really does get serious and wants to adopt Scrum, they’ll search for a Scrum Master to help their teams out and hire one of these guys, further spreading the poison. Then said company will have disastrous results and either “learn that agile just does not work” and switch back to whatever process they had before or keep the course with a pseudo agile setup that will cause people to only complain about how much they hate agile.

What’s the solution to this mess? In my opinion “Fake Agile” is the biggest threat to our industry as only widens a certain problem and puts a nice doormat over it. :(

Categories: Blogs