Tuesday, June 3, 2008

Why my diverse background makes me a better programmer

I have had a fair amount of very different jobs so far in my life. I have been a Chef, a Sonar Technician in the US Navy, a Medic in the US Army Reserves, an Accounts Payable and Purchasing clerk, and a Financial Systems Analyst. They all made sense at the time, and have all been fulfilling in their own way, but in every case I knew shortly after starting that these were not jobs that I would be happy doing for the rest of my career. Once mastered they would become repetitive and ultimately boring. I was extremely happy when I started down my most recent career path, I had finally found something that I could see myself doing forever, something that would never get boring; Programming.

Something that I find interesting is that in each change I was able to leverage skills learned in the previous jobs, even when completely un-related. I often tend to think “outside the box” and challenge notions that are commonly accepted as fact. Looking at a problem from multiple points of view has given me the ability to find creative solutions that someone with different experiences may not think of.

How to Import a CSV file into MySQL from Linux

Below is a step by step set of instructions on how to import a csv file into MySQL from the Linux command line. If it works correctly you will see the first 10 records displayed on screen. If your csv file has invalid characters you will get warning messages with the line number.

Create a .sql file with the following code:

truncate table table_name;

load data local infile 'csv_file.csv' into table table_name

fields terminated by ','

lines terminated by '\r\n'

ignore 1 lines

(field1, field2, etc.)

;

select * from table_name limit 10;

Then create a .sh file with the following code:

mysql -u your_username --password=your_password --show-warnings -D your_database_name <>

Run the .sh file and your csv file will be loaded into your table.

Monday, June 2, 2008

BrainTree API and ActiveMerchant

Recently I was working on a project that needed an e-commerce solution for monthly subscription renewals. We also wanted to be able to securely store the user’s credit card information to use for recurring billing. At first evaluation it seemed like we where going to need 2 separate solutions here. An e-commerce gateway and 2 way encryption for secure credit card information storage. This seemed like an awful lot of work and we couldn’t be the only ones that need this, it felt like we were trying to reinvent the wheel. After some research into other options we found the solution we where looking for.

The service we found is from BrainTree Payment Solutions. They have an API that will securely store the credit card information and return a unique token. Storing this token has far less risk involved. If someone were to gain access to this token in your database, it would be useless to them.

But that is only a fraction of what this API can do. It is also an e-commerce gateway that supports all the major credit card types as well as Echeck, ACH (automatic clearing house) and EFT (electronic funds transfer) which are all forms of Direct Deposit. It also supports some of the online payment options such as PayPal, Bill Me Later and Google Checkout.

All of that functionality is pretty awesome right? Well if you are a Ruby user it gets even better. You can access all of this functionality through a familiar e-commerce library; Active Merchant. All of the functionality with the ease of Active Merchant, what more could you ask for?

Monday, May 26, 2008

Ruby gems vs. Rails plug-ins

Another area that used to confuse me is gems and plug-ins. From time to time you will see a gem that can also be a plug-in. This caused a lot of confusion to me at first. I started thinking that they were the same things. This couldn’t have been farther from the truth.

When you install a gem it is adding that functionality to your core Ruby installation. This makes the functionality available to all Ruby applications that you develop on your system. An important distinction here is that it lives on your system, if you are developing your application on one computer and deploying it on another you need to install all the required gems on the deployment system also.

A plug-in does not get installed into the core Ruby directory. Instead it is installed directly into the framework of the application that you are working on. That being said, it is not available to any other applications that you develop and will need to be installed into each application that you want to use that functionality. However, if you plan to migrate your application to a different environment you will not have to worry about installing it again, as it is part of your application framework now. This can prevent a lot of confusion and makes changing environments much smoother.

Tuesday, May 20, 2008

Ruby on Rails: what is the difference between Ruby and Rails?

As a newcomer to web development I really didn’t understand that Ruby on Rails was referring to 2 different things. After working with it for a while and getting into the how’s and why’s as I like to do, I came to understand and appreciate those differences.

First let me start of with a quick definition of Ruby. Ruby is an object-oriented, interpreted programming language. The interpreted part means that Ruby is not a compiled language; it is read line by line. The object-oriented part refers to a programming paradigm that uses objects to design applications.

Now let’s get into Rails. Rails is a framework that is written in Ruby. A framework allows programmers to access a set of methods, libraries and support applications which extend the language they are using. Rails imparts a lot of critical and time saving functionality to your Ruby application. Rails follows a Model View Controller (MVC) architecture and a Convention of Configuration concept. One of the other key features that Rails has is Active Record, which controls the object-relational mapping, allowing for easy database integration.

In summary, Ruby is a programming language, while Rails is the foundation that you build your Ruby application on. Ruby on Rails is a powerful team that allows for rapid database driven, web application development.

Thursday, May 15, 2008

Making brownies with my son

Last week I was in the kitchen making some brownies when my 9 year old son came in. As any of you with children know their curiosity can be endless at times, and this was one of those times. I was measuring out all of the ingredients just getting everything set up and he started asking me a million questions. He wanted to know what I was doing and what each ingredient did.

I explained each step as we went telling him what that ingredient was, what it did and how it reacted with the other ingredients. At some point during this process I thought to my self how similar this was to the way I like to learn about things. I find that just knowing how to do something, blindly following a recipe, is not enough for me. I want to know why it works, how each ingredient interacts with the others. I have found that if I understand how it works, I can make subtle changes that make drastic improvements to the taste of what I am making.

Since I have started web development I have found that this same way of thinking has really helped me. Taking the time to understand how and why it works has made it possible for me to do things that I may not otherwise have been able to do. While this does take extra time it is an investment that has always paid off for me.

The Brownies turned out amazing. If you would like to just follow the recipe I have included it below. If you would like to know what each ingredient does and why leave a comment and I'll be happy to explain.

½ lb butter (room temp)

12 oz chocolate chips (bittersweet, 60 to 70%)

1 ¼ cups flour

1 t baking powder

½ t salt

4 eggs

2 cups sugar

2 T Amaretto DiSaronno (don't worry the alcohol cooks off)

1 ½ cups almond (chopped)

Sift together flour, salt, and baking powder and set aside. In a double boiler combine butter and 8 oz of the chocolate until smooth and glossy. While that is melting, in a separate bowl, combine sugar, eggs and amaretto until smooth and sugar has dissolved as much as possible. Once chocolate mixture is ready slowly combine with egg mixture. Once completely combined slowly stir in flour mixture until smooth then add in chopped almonds and the remaining chocolate chips. Pour into a 9x9 buttered pan and bake for 35-45 minutes (until a tooth pick when inserted comes out clean) in a 350 degree oven.


Saturday, May 10, 2008

How I learned Ruby on Rails

It was time for a career change. My fiancée and I had just relocated across the country (from Boston to LA). It was her job that relocated us so we decided that I would just find a new job after we moved. But I didn’t want to go back to the same type of job, I had always wanted to be a programmer and was determined that this would be the time. I had taken a few programming courses while working on my Computer and Electronic Engineering degree but I didn’t have any experience so I knew this was not going to be an easy task. I put my resume together, posted it out on the World Wide Web and then started my search. About 2 weeks into the search I stumbled on a post on Craig’s List. It was an original add and caught my eye immediately. The line that really grabbed my attention was “if you don’t know everything that’s ok we can teach you”. That was all I needed and decided to reply to it.

About a week later, after several emails back and forth and a few phone interviews the potential employer presented me with a challenge: Build a complete Ruby on Rails web application. I had never used Ruby or Rails and had never even heard of them but I accepted the challenge. I headed directly to the closest book store and bought 3 or 4 books on Ruby and Rails and MySQL and jumped right into it. There was no timeline set but I wanted to get it done ASAP, I really wanted to impress. It was a pretty complex project and I was worried that it was beyond my abilities but I never gave up. One week to the day after the challenge was issued I completed it and sent it back. I am not sure if I impressed him or not but I felt great. I had just written my first web application.

A few weeks later he offered me a contract position and I accepted it. To this day my fiancée still likes to tell the story of how I got into programming when ever the opportunity presents itself. I enjoy hearing her tell it, she is so proud of me. For me this story is just an example of what you can accomplish if you really want something badly enough. If you put your mind to it, you really can do anything.

Thursday, May 1, 2008

Ingredients: the foundation of something great or a complete disaster

Selecting the right ingredients sets the stage for the outcome of the final product. This can be said of both cooking and programming, choosing the right cut of meat in the kitchen or the right language in programming for instance. But the key ingredient that most people over look, the most important of them all, is the person that is combining them together. Even with the best ingredients possible, if the cook doesn't know what to do with them the outcome will be risky and potentially inedible. However, a talented cook can take lesser ingredients and make something completely unexpected and wonderful out of it. Therefore the ideal situation would be pairing the talented cook with the best ingredients. The same analogy can be used in software development. The cook and the developer are the foundation on which everything else is built.

Some chefs use fancy techniques to do something that is very straight forward and simple. Nothing is gained by these fancy techniques, the food doesn't taste any better. I have seen the same behaviors in programmers, using a dozen lines of code to do what a single line or two can do, just to show off. I tend to the straight forward approach, both in cooking and programming, which affords me the time to spend where it has more impact.

In both, there is no one right way to get the job done. Every person has their own techniques, their own methodology. I prefer an agile approach both in cooking and writing code. Spending a little extra time preparing before you start can really make a difference; it can help you avoid potential pitfalls or worse, burning dinner!