acts_as_rickroll

In the spirit of the day I’ve released acts_as_rickroll. Instead of pranking you, I’m releasing a tool for Rails developers to prank other people. If you want to try this out, download the acts_as_rickroll plugin and expand it in your vendor/plugins directory.

This plugin overrides the redirect_to method of ActionController so that if you pass in a specific :id to the redirect_to method, the recipient will get rickrolled. Once you have installed the plugin you can add acts_as_rickroll ‘foo’ to your controller. Now if you ever call redirect_to :id => ‘foo’ they will be redirected to to a rick roll instead.

The idea behind this plugin is that you can use this on an existing controller that already happens to use redirect_to. Here is a simple example from one of my controllers:

class MyController < ApplicationController
  acts_as_rickroll 'rick'

  def index
    number = rand(100)

    if (number % 5) == 0
      redirect_to :action => ‘most_recent’
    else
      redirect_to :id => ‘rick’
    end
  end
end

Ruby Short Circuit (||=) edge case - response

On DABlog.com there is an article on Ruby Short Circuit evaluation. It’s an interesting read, although it comes to a slightly incorrect conclusion.

The questions is what does the statement x ||= 1 expand to. The initial hypothesis was that it expands to x = x || 1. That struck me as odd because my thought was that it expanded to x || x = 1. Rather than rehashing his examples, I’ll assume you’ve read the original article. If you haven’t, I’ll wait.

Now that we’ve all read the article, the conclusion was that x ||= 1 expands to x or x = 1. Again this struck me as odd, why would you take the || operator and convert it to the “or” operator which has lower precedence. So I prepared a little irb session that demonstrates what the actual expansion is:

C:\home>irb
irb(main):001:0> h = Hash.new(1)
=> {}
irb(main):002:0> h[:x] || h[:x] = 2
=> 1
irb(main):003:0> h
=> {}
irb(main):004:0> h = Hash.new
=> {}
irb(main):005:0> val = h[:x] ||= 2
=> 2
irb(main):006:0> val
=> 2
irb(main):007:0> val = h[:y] or h[:y] = 2
=> 2
irb(main):008:0> val
=> nil
irb(main):009:0> val = h[:y] || h[:y] = 2
=> 2
irb(main):010:0> val
=> 2
irb(main):011:0> h
=> {:x=>2, :y=>2}
irb(main):012:0>

The trick is that in ruby “or” has a lower precedence than ||. Unfortunately “or” has a lower precedence than the assignment operator “=”. This example shows that x ||= 1 in fact expands to x || x = 1 rather than x or x = 1.

Edit: It looks like my expansion wasn’t correct either. mernen on reddit posted a code snippet that illustrates the further expansion. It looks like x ||= 1 actually expands to (x || (x = (y))). The parentheses matter because they make “or” versus || a mute point. The code snippet posted by mernen is:
x = nil
y = 1 + x || x = 10 # TypeError: nil can’t be coerced into Fixnum
y = 1 + x ||= 10 # => 11

Scalable Business Systems

Building scalable systems isn’t limited only to technology. Often you can take the lessons learned from Computer Science and apply those same techniques to business systems and dramatically improve the effectiveness of a business.

During a company meeting at one of my previous jobs one, of the executives was berating the company as a whole for our performance of late. In particular he stressed that “we know the processes work, people just need to be more careful”. This statement struck me as odd, from a software perspective if you are experiencing similar and frequent errors with different hardware in place, you would expect there is some problem with the software itself. Yet rather than looking at the systems themselves they chose to blame the hardware.

During a discussion I had with a director at this same company, he was lamenting that people were not careful enough when they made modifications to production data. I explained that when any data modification is made there is a non-zero chance that someone will mess up the data. Yet with a database you want your data to be as error free as possible. So rather than relying on people to be perfect, you build your systems around the idea that people are imperfect. Good backups, primary and foreign keys, check constraints, triggers, type safety on data, these are all methods to help ensure the consistency of your data. More importantly though they are put in place so you can state expectations about what can go into the database and to prevent people from inadvertantly messing things up. In the case of backups you have a third level of redundancy in case your first two lines of defense fail (people, constraints).

The logic of redundancy and checks can be applied to almost any process. I read this fantastic article about checklists a few days ago. Essentially by implementing checklists for certain procedures, and having those lists monitored and enforced they reduced the error rate nearly to zero. The lists weren’t for some low level uneducated worker. Rather they were for highly trained and specialized doctors.

The moral of the story then is that as complexity increases, it’s important to build scalable business systems that will account for the imperfection of people. Even something as simple as implementing a checklist and having people actively monitor that all of the steps were done correctly can yield much more fault tolerant business systems.

What everyone should know about the proper use and deployment of Tasers

With all of the Taser incidents getting more media attention now, I thought I’d point out a discussion on the Diane Rehm show this morning on NPR. This is the best most thought out discourse I’ve heard on Taser incidents. Not only do they have a spokesperson from Amnesty international and an associate professor of Criminology, but they have Thomas P. Smith Co-Founder and Chairman of the board of Taser international.

Initially I expected this to either be an apologetic piece forgiving police officers for their many abuses, or a one sided slam piece against the use of Tasers. What I got though was more typical of the Diane Rhem show. This discussion very thoughtfully covered most of the issues related to Taser deployments, and really helped to frame the issues in an appropriate way. Most of the discussions I’ve heard or seen have revolved around using Tasers or not using Tasers. One of the call in guests on this show however reframed the issue as where does Taser use fall on the continuum of force? His position was that it should be used as an alternative to deadly force only, rather than as a compliance device. In many of the abuses we have seen police are deploying a Taser much sooner than they could have deployed deadly force. You can’t shoot someone for being impolite to a cop, but some officers feel it is perfectly fine to Taser them for that.

During the discussion they cite several studies that have been done on how many people have died due to Taser related incidents and what differentiates those cases from the cases where people don’t die. They also examine how many cases of Taser use resulted in a conviction. In one such study of 1,000 Taser incidents only 100 of them resulted in convictions.

Overall this was a very well done topic. If you have the time and the inclination, I highly encourage you to head over to Diane Rehm site and listen to the Taser Broadcast

Starting a MicroISV in record time.

I’ve mentioned Patrick McKenzie’s blog before. Patrick’s experience has been very interesting. He started his company called Bingo Card Creator in record time. He stayed laser focused on the problem he was trying to solve, and rather than goofing around with it for months on end, he got it out the door and in front of customers. In eight days and with less than 60 dollars he had built his program. After a year in business he is now grossing over $2,000 a month.

Inspired by his example, another entrepreneur by the name of Ezell has decided to one up Patrick. Ezell had a discussion with his friend about whether or not what Patrick did is even possible. Ezell’s friend doesn’t think it’s possible, while Ezell thinks it is. So to test the theory Ezell will attempt to get his business going in six days with a budget of $20. I’m very excited to follow his progress on this project. His project is called BabyAid and is designed to allow parents to log the activities of their child. I am particularly interested in this program as I just had a baby boy seven weeks ago. Currently we log everything in a notebook. I even mentioned to my wife that a program would be better for this, but she dumped on the idea. ;). In the end though, keeping this type of log is very important, and when we forget to update the log it’s a headache to figure out if our son is on schedule. The biggest problem Ezell will face is making the log entry simple and convenient. If he can jump that hurdle it’s pretty likely that when Ezell gets his program finished, I’ll be one of his first customers.

Ezell I’ll be following your progress and rooting for you all the way.

Freelance Consulting Rates

Here is how I determined how to set my freelance contract rates. To start with you need to figure out how much you want to make in a given year. Let’s start with $100,000 for a nice round number. Whether or not you can actually make that much depends largely on your market and your technical skillset. Don’t take any of these numbers as specifically applicable to you, they are simply hypothetical.

Before we start, I recommend following along by creating a spreadsheet so you can play around with these figures and come up with a set of numbers that makes sense for your situation.

When I first started freelancing my initial thought was I make X dollars a year as an employee, which equates to Y dollars an hour. So when I start freelancing, I’ll simply charge Y dollars and make the same amount of money. Unfortunately that’s not how the world works.

When setting your rates there are some things to take into account:

  • You will almost never be able to bill 100% of your time, especially in the beginning when you don’t have a strong clientele built up.
  • You need to plan for healthcare costs
  • You need to plan for training and conference expenses
  • If you have an office you will need to account for the cost of your office space
  • You will need to factor in time for vacation

This list isn’t necessarily complete, but will give us a good starting point.

Now our desired income is $100,000 per year. In a standard work year of 8 hours a day, 5 days a week, there are 2080 hours per year. Our first step is to calculate how many billable hours we will have in a year.

Starting with 2080 hours we subtract the following amounts, change these numbers to what you find suitable in your own spreadsheet.

  • 80 hours for holidays (New Years, 4th of July, Thanksgiving, Christmas, etc).
  • 80 hours for vacation per year
  • 40 hours for sick time
  • 40 hours for attending a technical conference

After subtracting all of those hours from 2,080 we come up with 1,840 hours. That is the total number of hours we are available to work in any given year. Unfortunately, you won’t always have contracts lined up back to back, so as a business owner you need to plan for downtime while lining up the next contract. The amount of downtime expected will vary depending on your market and the demand for your particular skillset, however let’s start with 25% downtime. That means in any given year you are planning to bill at least 75% of your available hours to a client. 75% of 1,840 hours equals 1,380.

If those were the only factors we were taking into account, our hypothetical hourly rate would be $72 per hour. ($100,000 / 1380 hours).

The next set of things we need to take into account are business expenses. The following is a list of typical expenses:

  • Salary: $100,000
  • Employer Fica Match: $10,000
  • Office Rent: $1,000 per month for $12,000 per year
  • Office DSL/Cable: $150 per month for $1800 per year
  • Business Insurance: $50 per month for $600 per year
  • Yearly license fees (business license): $500 per year
  • Health Insurance: $500 per month for $6,000 per year
  • Training budget: $3000 per year
  • Book or educational material budget: $100 per month for $1,200 per year
  • Computer equipment budget: $2,500 per year
  • Office equipment budget: $500

All of those expenses weigh in at a grand total of $138,100. There are more expenses I’m leaving out simply because this is an example. I just wanted to give you a flavor for what types of things you should be looking at.

Now that we have our expense cost of $138,100, we can divide that by our billable hours per year of 1,380. The hourly rate for this particular example comes in right at $100 per hour if you ignore the pennies. Note, it won’t always match up like that, if your desired salary is $50,000 per year the rate would be $60 per hour for our example.

There is one final set of computations I do. It’s not enough to get an hourly rate from someone, you also need to arrange payment terms. I offer a $10 an hour discount to a company that will pay an invoice within 15 days. If they will sign a long term contract guaranteeing a certain number of hours per month, I will further reduce their rate another $10 per hour. If you are going to offer discounts, it’s important to account for that in your rate up front.

Giving a bonus for paying early versus a penalty for paying late. People don’t like payment penalties, and forcing your client to pay them is something that strains your relationship with them. Rather than impose a penalty, I prefer to offer a reward to clients who pay early. Thus the $10 an hour rate decrease if the invoice is paid within 15 days. If they don’t pay within the 15 days, I will generally give them to 30 days to pay the invoice. If they don’t pay within 30 days, they get an additional 15 days grace period, during which time I will be contacting them frequently. At the end of 45 days, all work ceases until payment is made. It’s important to be firm with this rule, if you let clients string out their payments to you, they will do it. By letting them delay payment, you are in effect financing their business. That’s not something I’m prepared to do, so it’s important to limit your risk and be extremely clear and direct when dealing with business owners. Do not dance around this issue, many a small business has failed because they weren’t good at collecting their accounts receivable (money owed to you).

I add those two discounts into my rate directly so the final billable rate for our example is $120 per hour.

There are a lot of assumptions built into these equations, and as your run your business you will be able to refine them as go along. Just make sure not to start by offering too low of an hourly rate. My initial thought would have had me charging $50 per hour for our example, my actual salary would have been $37,000 a year once we figure in all the business costs. Ouch!

If you’d like to see this example in a spread sheet, I have published my rate calculation example on docs.google.com.

Good luck and happy consulting.

Database Anti-Pattern: Recursive Network Select

Recursive Network Select is a pattern that occurs when a program makes a call to a database returning a resultset and then as it loops through that resultset it makes a call to the database for each record as it loops through the resultset. In extreme cases, the program will add additional levels of recursion while looping through the resultsets returned as a result of the calls made while looping through the initial resultset.

Here is a typical example: The program retrieves a list of customers in the database. Then to get a list of orders placed by each customer the program will loop through the customers and for each customer it will query the database to get a list of orders placed for that customer. In the extreme cases, the order itself doesn’t contain all of the necessary information, so the program will then loop through each order and send another query to the database to get the list of products in each order.

Why is a Recursive Network Select bad? Let’s look at some of the numbers.

One hundred customers, each with an average of three orders, and each order averaging three products.

  • One query to get the customer list
  • One hundred queries to get the orders for each customer
  • Three hundred queries to get the product list for each order

That’s a total of four hundred and one queries. Each of those queries is composed of the following steps (simplified, there are actually many more steps than this):

  • Open a connection to the server (some languages/frameworks retain persistent database connections)
  • Transmit your query across the wire to the database server
  • The database server parses and tokenizes the query
  • After processing the query the database server comes up with a query plan to fetch the data
  • Indexes are traversed, identifying rows that qualify for inclusion
  • The server positions the read/write heads to gather the data from disk
  • The server reads all of the data from disk assembling the resultset
  • Criteria are used to exclude data from the resultset based on the where clause
  • The final resultset is packaged up and shipped back across the wire to your program (optionally closing the connection when finished)
  • Your program processes the resultset and makes it available to user level code for processing

If each query ran in 100 milliseconds, it would still take 41 seconds to process that resultset. The reality is that the total round trip time for those queries would be closer to 200 to 300 ms even if the query itself ran in 100 milliseconds. Each query comes with non-optimizable network overhead. That’s with only one hundred customers, the page load time increases with each customer and order added. Imagine this with system dealing with many thousands of customers.

This isn’t an idle speculative post, I have run across this pattern numerous times in the field dealing with database systems. In fact a system I am dealing with currently has this problem, which inspired this post.

There are a number of reasons programmers make this mistake. The biggest reason seems to be lack of understanding about SQL, or the total processing time required for this type of a solution. Unfortunately a large number of programmers do not properly understand how to join tables to get data. Sometimes programmers who know how to join tables, simply opt to do a Recursive Network Select because they view it as easier to do in their language than it is to use joins and get the data in a single call.

So we’ve established that the Recursive Network Select is bad practice, but how do you get around it? If a developer is working with a database, it’s important to understand the basic concepts involved, such as joining tables and/or limiting results with a properly formed where clause. Next, with the advent of good OO mappers, many languages provide framework facilities that will cleanly rip apart the resultset leaving you with a nice set of objects to loop through. Even if you don’t have a nice OO mapper to disassemble the dataset, it’s better to loop through the records and do it yourself than it is to make hundreds or thousands of network calls.

Finally, in most cases your program shouldn’t be returning hundreds or thousands of records to the client. This is why almost all libraries support pagination. People cannot consume hundreds or thousands of records at one time. Typically results should be limited to ten or twenty top level results. In some cases you may need to deal with hundreds or thousands of records, and where possible you should do a lot of the heavy lifting in Stored Procedures.

This article may be obvious to many developers, unfortunately it isn’t obvious to everyone. So the next time you encounter a Recursive Network Select, point the developer to this page, hopefully this will help them understand how performance damaging it can be.

10 cheap ways to get clients for freelance consultants

Many consultants are excellent at what they do, and want to start a consulting firm, yet many have no idea how to find new clients. So I’ve written up ten cheap ways to find new clients. None of these are new ideas, they have been around for years and they are currently being employed by professionals all over the world. Note, I have either used these, or personally known people that have employed these techniques to land paying clients.

The first thing to keep in mind is who are your customers and how can you get your message in front of them? It’s important to focus your efforts on people who will actually need the service you are offering. My clientèle have typically been small business owners, so for this article I will focus on the ways I’ve used to target that group of people. Additionally this article is primarily targeted at technical consultants, although it should be applicable to almost any consultant.

One final note before getting started. Using these techniques I have been able to keep myself as busy as I’ve wanted for a lot of years. What you will find though is that you want to interview your clients every bit as much as they are interviewing you. Do not give away free work, and make sure your client has realistic expectations about the budget required for the project they want. Most business people will not have any idea how expensive custom development is. Your job is to make them aware of that very early in the process.

1. Get the word out to your friends and family that you are starting a consulting firm and are looking for business. Something I’ve found particularly effective here is to offer a referral bonus. Just asking someone if they know of anyone that needs some programming work won’t get them to think very hard. However if you offer them one or two hundred dollars as a referral bonus for a paying client, they will think much harder about the people they know, and what they may need. Who knows, they might even start asking around on your behalf. Make sure to set your referral bonus low enough that you will still make appropriate money on the client. For custom programming, most contracts are worth many thousands of dollars, making a referral bonus well worth the money.

2. Get some nice business cards made and leave them at restaurants and on bulletin boards whenever you can, hand them out to everyone you know. The key here is to have very nice multi color high gloss dual sided cards printed up. The cost difference isn’t nearly as great as people think, and it’s a cheap way to make your card stand out in a pile. When you are leaving your card, the first impression you make on your potential client will be through your card. Don’t print up a cheap white card, and definitely don’t get the perforated kind at staples. A nice high gloss card stands out nicely in the pile. Since most geeks aren’t very photogenic, I typically opt to leave my picture off the card. Don’t go crazy on the graphics either, you want the card to stand out, but at the same time project a professional appearance.

3. Join your local chamber of commerce and attend the functions. Chambers of commerce are created to allow local business people get together and discuss their businesses and to get to know each other better. You may not drum up any business on your first couple of outings, that’s ok. At this point you are developing relationships, but make sure to let people know that you are looking for new clients. Pass your business cards out like candy here. Everyone else will be passing them out too. Make sure when you attend these functions that you dress up some. Nothing projects unprofessionalism like showing up to a business gathering in ratty jeans and a wrinkled t-shirt. At a minimum put on a pair of slacks and a button up shirt, make sure your clothes are heavily starched. You want to get past the impression most business people have that technical people are from another planet.

4. When you go to someone else’s place of business or you encounter another business owner, take the time to talk to them about their business. Find out what kinds of problems and challenges they are facing in their business. Ask them probing questions to see if there is some aspect of their business you can help them with. Often times business owners deal with problems that can be solved by someone else, but they don’t know that so they continue to deal with them.

5. Use Pay Per click placement in search engines. Hey, this is the internet era, you don’t have to get all of your business from the local area. That being said, I’ve had much better luck drumming up local business than I have over the internet. Most business owners seem to be comforted by dealing with someone locally that they can speak with face to face. With the search engines you can limit your searchers to your geographic area. You can also try places like craigslist. However it’s especially important on craigslist to be careful with your clientèle. Many people on craigslist seem to think software should cost about $300, no matter how complicated it is. However I have seen some very nice consulting relationships come about through craigslist.

6. Develop relationships with other consultant groups in your area. Business comes in ebbs and flows, sometimes you don’t have the capacity to take on a project, but the other consultant groups in your area will. I’ve had numerous offers from other local consultants to take on work they can’t handle. Other times I’ve been too busy to handle the workload myself and passed it off to other firms. Joining a local user group can be a particularly good way to meet other consultants in your area. Typically under this relationship, the person who drums up the work takes some type of hourly fee off the top.

7. Develop a relationship with contract companies in your area. They spend a lot of money on business development and usually have a pretty good pulse on the community. Most of them will contract you out as a 1099 if you prefer. As with getting work from another firm, they will take their hourly cut. One major bonus with going this route is they will typically handle all of the collection and billing for you. They will also do an excellent job at pre-qualifying their clients and will bring you higher paying jobs.

8. Get involved with your local charity group like the United Way or especially the Rotary club. The Rotary club is a service organization comprised of primarily business people. Most businesses have frequent interaction with their local charities. This is an excellent way to meet other business people in a very relaxed setting. My local United Way has a golf tournament once a year, typically you can sponsor the tournament by either providing a number of give aways, or you can pay a modest fee to help provide prizes. This is a cheap way to get your message in front of a lot of local business people and get bonus karma points while you are at it.

9. Get to know the professors at your local university. Particularly the ones in the department that teach your specialty. Many business owners will contact the universities and find out if they know of anyone that can handle the projects they need done. The school may want you to take one or more of their students on as an intern to help grease the skids. This is typically a win-win situation, the student gets invaluable work experience, and you get someone to handle those parts of the application that are tedious but non-complicated to work on. Make sure they send you several candidates to choose from. I’ve written previously about how to interview and hire programmers.

10. Take out an ad in the classified section of your local newspaper. While the internet era is definitely eroding the power newspapers once had, they are still an effective way to let people know about your business. Most business people tend to lag behind the internet power user curve, and so a substantial portion of them still look through the classified section in the local paper.

These are not the only ways to drum up business for a consultant. These are just ten ways I’ve used or have seen used, that are very effective. Something to keep in mind is that there are typically dozens or hundreds of other professionals in your area that are marketing to the same target group that you are. Talk to them, ask them how they get new clients. Find out what are the most effective ways for them to find new clients in your particular market.

Good luck and happy consulting.

Productivity Tip: Visor

I am a big fan of attending local Ruby groups and/or conferences. There are a number of advantages to attending them, but one of the biggest personal advantages I get is exposure to new tools and new ways to get my development done. It’s interesting to me how the addition of a new tool to your work flow can almost make the entire process feel new and fresh again. Today I’ll talk about a new tool call Visor.

For anyone who ever played the game Quake, Visor is based on the command line concept used in Quake. In a nutshell, the command line was always available to you with a single quick keystroke. Once you hit the key, the console would immediately drop down and cover half the screen. If you hit the key again, the console disappears. Visor is an OS X extension for the Terminal.app to give you that same functionality. Visor is from the same guys who make QuickSilver, so you can expect the application to work very well.

The benefit to me is the ability to drop into a terminal window while I’m coding without having to think about command tabbing to the window. It’s nice to have certain keystrokes that are guaranteed to bring up certain applications. If you find yourself working on the command line a lot and would like quick guaranteed access too it, check out Visor.

The one drawback I’ve noticed when using Visor comes when you have multiple terminal windows open. When doing Rails development, I find I typically have three or more open at a time. One terminal to start the web server, one to start autotest, and one to do script/console or script/generate depending on the circumstance. The third one is the one I like to use in Visor. The drawback is that if you are in TextMate and activate Visor you will get the drop down terminal just like you expect, but when you deactivate it you find yourself dumped into one of the other Terminal windows, instead of back into TextMate. The best solution I’ve found so far is to simply minimize your other Terminal windows and you will then be dropped back into TextMate as expected.

JavaScript is not security

Recently I attended my grandmothers 85th birthday party. The family made the decision to have a professional photographer come to the event and take some pictures of the family. The interesting part came when the photographer put the pictures online for us to view so we could decide which pictures we wanted to purchase. The site was perhaps the most irritating photo gallery I’ve ever seen. It loaded thumbnails of the images on the page, and then as soon as you moused over the image, it replaced the image with a blank image. You could click on the now blank thumbnail and have it load a full sized version of the image. The full sized image also used this irritating mouseover trick. Presumably this was to prevent people from right clicking on the image and choosing save Image as…

Anyone who is web savvy will realize this is of course not secure at all. In order to display the image on my machine, the image has to be sent to my machine, which in most cases means there is a url directly to the image. So of course a quick browse of the source code revealed the direct link to each of the images. It would have been trivial at that point for me to download any of the images I wanted.

Here are some lessons to be learned:

1. JavaScript is not security. Preventing right clicking on something does not protect that resource. It will prevent the technologically challenged from grabbing your content, but it will also probably confuse and irritate those same users far more than intended.
2. Web Developers need to fully appreciate the difference between server side code and client side code. You as the developer have absolutely no control over the client side code. You create it and send it to the client, but you cannot be sure that anything coming back from the client is what you originally sent them. This is a huge source of site vulnerabilities.
3. Irritating your users is probably not the best practice. When someone mouses over an image and it simply disappears, most users will think it means your site is buggy, and therefore not to be trusted. It’s doubtful that most neophyte users would realize this is a flawed attempt at a security measure.

What could the site have done to achieve the same end but at the same time be far less obtrusive? The easiest solution that comes to mind would be watermarking the image. That way, the photographs have much better protection. It’s very unlikely that people will add watermarked images to their photo album. At that point they will actually purchase the image as desired.