Ask Mike: How do you learn a new programming language?

I found myself sitting on a couch the other night explaining to one of my good friends how to learn a programming language. His background was in computer engineering; most of his experience, which was scarce to begin with, was focused on low-level C-like languages.

So, here my friend sits trying to learn a language that is totally different from C, C++, and Java: Ruby. Ruby was my first choice because it illustrates a fundamental learning concept to a language: syntax. In Ruby, we see a lot of unique conventions, short hand notations, and other syntactic sugars and that seems to be the biggest hurdle in learning a language for me.

So here’s my list of concepts and information I seek when I’m treading unknown soils:

  1. Define a class, instantiate it, and make it do something
    • This exercise will give you the opportunity to toy with inheritance, class vs. instance methods, public and private scope, and optionally instance-specific functions like constructors and destructors.
  2. Find an article on “ten neat tricks to ” … or .. “ten things I wished I knew about
    • Explore why others like this language… What is cool to them about it? Do these tricks show you anything about the language that the book(s) or tutorial(s) you’re following do or don’t do?
  3. Read about the language data types
    • Are the data types what you’d expect to see? Do floats auto-magically grow? Are strings immutable? Sometimes by looking at the data types you’ll see some interesting features of a language. For example, in Ruby you can take an integer and call methods on it like .days.from_now (5.days_from_now).
  4. Find an article highlighting the ten worst things about the new language
    • Yup. Understanding the language’s perceived weaknesses is very important. In Ruby, the MRI interpreter supports green threads — this sucks in some cases. But along with the criticism often comes a solution. For the case of parallel threads, using the jRuby interpreter solves that issue by supporting OS-level threads.
  5. Code something fun
    • For my friend, he is building a small BlackJack dealer simulator. Perfect. The important thing here is to pick an idea that you can articulate well, understand how you’d theoretically build it, and would enjoy spending time working on.
  6. Optional: Go read some “Why I Switched from Lang 1 to Lang 2 ” or ” Lang 1 to Lang 2 side by side ” articles.

PHP 5.2 mysql_connect w/ same parameters does not return a new connection!

This is subtle… I was writing some “reconnect” logic for a DB Class to catch and handle MySQL error code 2006, Server Gone Away. After a solid 5 hours of wasted time I traced the poor logic on my part back to the mysql_connect() function. I was thrown off track at first when the code was hung up on a mysql_select_db() function. Turns out that mysql_connect(), if called twice in an execution series with the same parameters, will take the old link and return that! What The French. So, yes, I read the manual and a fourth param (boolean) can be passed into the function to say I want a NEW connection.


The fourth param is called new_link, and the excerpt from is as follows:

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

Good to know. Better to remember. Necessary to understand. Fail on my part.