Learning a new programming language is fun – and essential if you want to stay competitive. I try to learn new languages all the time(my latest target is Haskell). As a result, I have a system to make learning new languages easier. This is for people who already know a programming language and want to learn another one.
Requirements
What you must have…
- The interpreter/compiler of the language you are learning
- You must be able to run the program after writing it.
- Language reference manual/documentation
- Usually found at the site of the language. Put a shortcut to that on your desktop – because you will be using this all the time.
- An easy to follow tutorial
- Search for it in Google. If you find one and its hard to follow, ditch it and get another. You may not need this – as manual for most languages have a tutorial in them.
- A ‘todo’ project that you cannot live without
- This is what forces you to learn the language – more on this later.
What I would recommend you have…
Having this will help you learn – but its not necessary.
- A Net Connection
- You may want to ask your doubt on the IRC channel. Or on a forum. Or to google the error message you got.
- A Decent IDE
- This may make your job easier – but if you have notepad, that’s enough.
What you don’t need
- Books on the language
- You may need a book to master the language – but you don’t need one to learn it.
- A Teacher
- Its helpful to have someone to clarify the doubts you may have – but no one can teach you anything you cannot learn yourself.
The Hello World Script
First you have to create the ‘hello world’ application. This is more than just ‘print “Hello World”‘ – it will act as a cheatsheet for you until you familiarize yourself with the language. The point of this application is to use all the most commonly used elements of a language and putting it in a single place so that you can refer to it later.
Your first job is to go to the tutorial, the manual and the internet until you create the Hello World application with the following stuff in it. I will provide an example – how the ‘hello world’ application will look in PHP.
After each step run the application and make sure it works.
- Print “Hello World\n”
-
Why do you think we call it a hello world application? Write the code to print a string ‘hello world’ and save it to a file. Now run it using the interpreter – and make sure it works – see it in action.
// Printing(IO) print "Hello World!\n";
- Comments
-
Put a single line comment(like //) on top of the “print ‘hello world'” code and if your language supports it, a multiline comment(/* – */) as well. Run the script to make sure the comments work as advertised.
// Single line comment. /* Mulitline comment. */
I was just kidding about running the script to test the comments – you did’nt do it, did you?
- Use a variable(a string and an integer) and a concatenation operator.
-
Create a variable called name(string) – and give it your name as the value. Create another integer variable and give it the current year. You don’t have to find it programatically – just put it as 2008 or something. Now you have to print out the a string “Hello, <name> – Welcome to year <$year>”. This string will let you concatenate two different type variable to a string – and it makes me feel like a time traveler ;-).
Yeah – the value of variable name should appear at <name>. Use the concatenation operator if possible – in PHP, write
print "Hello, " . $name . " - welcome to year " . $year;
instead of
print "Hello, $name - welcome to year $year";
This is because different languages have different concatenation operators. The other stuff is the same almost universally. + refers to addition. – refers to subtraction. * is multiplication. My point is that if you know the operators for one language, you know the operators for almost all languages. But the concatenation operator differs from language to language – its ‘+’ in javascript, ruby. Its ‘.’ in Perl, PHP, etc.
There are exceptions to this rule – ‘=’ is the equality operator in SQL while it all other language its ‘==’. The assignment operator is ‘:=’ in pascal – in all other it is ‘=’. If you notice any difference in any operator write some code using that operator in this section.
- Use if/else if/else
-
Create something like this pseudo code…
if (year > 2008) { print "Welcome to the future - yes we have flying cars!" } else if(year < 2008) { print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!" } else { print "Anything wrong with your time machine? You have not gone anywhere, kiddo." }
In PHP, this section will look like this…
//If,else conditions if ($year > 2008) { print "Welcome to the future - yes, we have flying cars!"; } else if($year < 2008) { print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and decent, stay away from your parents!"; } else { print "Anything wrong with your time machine? You have not gone anywhere, kiddo."; } print "\n";
If you use switch(and the new language supports switch), you can write an example of switch here.
- Print ‘Hi there!’ 3 times using a for loop
-
A PHP example of using for loop(my all time favorite loop)…
// For loop for($i=0; $i<3; $i++) { print "$i) Hi there!\n"; }
- Create a list(array) and iterate through it using while/for/foreach loop.
-
I am using a while loop here – to keep things different…
//Numerical Array, While $rules = array( 'Do no harm', 'Obey', 'Continue Living' ); $i = 0; while($i<count($rules)) { print "Rule " . ($i+1) . " : " . $rules[$i] . "\n"; $i++; }
- Create a hash(associative array) and iterate through it.
-
If the language you are going to learn have ‘foreach’, this maybe the best time to use it…
// Associated array, foreach $associated = array( 'hello' => 'world', 'foo' => 'bar', 'lorem' => 'ipsum' ); foreach($associated as $key => $value) { print "$key: $value\n"; }
- Create a function with an argument and a return.
-
If you need, you can create an example for call by reference, optional arguments, default argument value, variable argument count etc. as well – but they are not essential. Make sure you include the code to call a function as well.
// Function, argument, return, call function hello($name) { return "Hello " . $name; } $hello_string = hello("Binny");
Optional Code
I don’t usually included some of these in my ‘hello world’ scripts – but they can be useful in some cases. Some may find them useful – so if you think you are going to use any of these, include it in your file.
- Class, Objects, Member variables and functions
-
If you are a big fan of OOP, create the code for a class. Make sure it has all the following elements…
- At least one member variable
- At least two member functions
- Access the member variable and the other function from within a member function.
- Create an object of this class
- Call a member function using the object.
// One for the OOP fanboys - Class, members, objects and stuff. class Movie { public $name = ''; public $rating = 0; function __construct($name) { $this->name = $name; $this->rateMovie(); } function rateMovie() { $this->rating = (strlen($this->name) % 10) + 1; //IMDBs rating algorithm. True story! } function printMovieDetails() { print "Movie : {$this->name}\n"; print "Rating : " . str_repeat('*', $this->rating) . "({$this->rating})\n\n"; } } //Create the object $ncfom = new Movie("New Country for Old Men"); //It's a sequel! $ncfom->printMovieDetails();
- File IO
-
Include this code – it is sure to come in useful someday. Unless you are learning JavaScript.
// File IO // File reading, easy method... $contents = file_get_contents('hello.php'); // Writing to a file $file_handle = fopen('/tmp/hello.txt', 'w'); fputs($file_handle, "Hello World"); fclose($file_handle);
- Command Execution
-
I tend to include this – but it may be unnecessary for you.
// Command Executing print `ls`; //Execute the command 'ls' and print its output
- Regular Expressions
-
I always include them – I am a big fan of regular expressions. I have lost count of how many times they have saved the day.
// Regular Expressions $string = "Hello World"; if(preg_match('/^Hell/', $string)) print "Yup - its evil\n"; print preg_replace('/l([^l])/', "$1", $string); //Remove an 'l' from both words. Should print 'Helo Word'
Specialized Code
The language you learn may have a specialized field which we may have not used yet. If so, put it down here.
If you are learning javascript, write some code to access DOM nodes here. If you are using PHP, database connectivity will go well here. If you are learning any web server side languages, write the code to fetch the POST/GET request values. For PHP, it may look something like this…
// Database connectivity(native)
mysql_connect('localhost', 'root', '') or die("Cannot connect to the Database server");
mysql_select_db('Data') or die('Could not find a database called "Data"');
// Executing Query
$sql_handle = mysql_query('SELECT name,url,description FROM Comment LIMIT 1') or die('Query Error: ' . mysql_error());
while($row = mysql_fetch_assoc($sql_handle)) {
print "Name : $row[name]\n";
print "URL : $row[url]\n";
print "Desc : $row[description]\n\n";
}
// Just a note here - if you are going to use PHP with database in a production system(and you will, trust me), use a Database abstraction layer rather than the above mentioned native methods.
print $_REQUEST['username']; // Method to get the value of the field 'username' after a form submit. Will not work at CLI execution
Project
Next thing you need is a project – which you are going to build using the new language. Some points that you have to look for when choosing the new project…
- It should not be a huge project.
- At the same time, it should not be something trivial.
- There must be something about the project which would force you to finish it.
The third point needs a little explaining. You should not just pick any project that pops into your mind. It must be something that you need – something you could not live without. If it is important to you, you will have a motivation to finish the project. Otherwise, you will drop the project at the first sign of trouble. But if its important to you, you will spend more time on it – you will look at documentation, post the problem in forums, ask others in IRC channels. And eventually, you will fix the problem. And you will finish the project.
While working on the project, keep the ‘hello world’ open – you will find yourself referring to it often.
Great points. Thanks Binny
Great to know you enjoyed it – I am thinking of making a series out of this concept.
Good post, and I am looking at learning PHP, C(++?) etc. in my hols, so this should help.
But I think you’re getting ahead of newbies like me – what I’d like is a complete expalanation post, considering that I have no programming experience or knowledge.
Like, the compiler and interpreter, where to get them, the tools etc. and then get to this post.
And a tut on PHP would be nice – after developing themes, I’m looking at WP plugins…
Sumesh,
> But I think you’re getting ahead of newbies like me
Thats true – this is for people who already know one language – and want to learn another.
> And a tut on PHP would be nice
I have been planning a tutorial on PHP for some time now – but I still have not started. If interested, here is a list of other tutorials by me…
Tcl/Tk
A Tcl(Tutorial for Cool Languages) for Tcl/Tk
Perl
Beginner’s Tutorial for CGI Perl Language
Abridged Perl/Tk Tutorial
JavaScript
ABC of JavaScript : An Interactive JavaScript Tutorial
Advanced JavaScript Tutorial
Binny,
Thanks a lot. I am a newbie too, but have been progressing well with PHP and this really helped out. Thanks for everything you do.
Jason
If you want to learn a new programming language fast, try using the Program Phases methodology. It embodies much of what is discussed here. Check it out at http://programphases.com
Nice.
btw, do we have flying cars, yet!? It’s already 2009! 🙂