Macha

A Self-Taught Programmer's Journey

September 27, 2010 | categories: Me, Programming

I was helping someone who is just beginning to program over the past few weeks, and it led me to actually write a blog post I'd been saying I should write for a long time, about how I got where I am today as a programmer. This is mostly from memory, so some of the timing may be wrong.

My first experience with anything even close to programming was when I was around 12. I had gone through all my mum's ECDL stuff for MS Office years before, and there was one program in Office on my then computer that wasn't even mentioned - Frontpage. So I got bored one day, and decided to check what it was. I gathered quickly enough that is was for making websites, and I sort of got parts of it, but I had to get a book from the library to figure it out fully. Armed with this newfound knowledge, I managed to make a basic website, a horribly primary-coloured frame-filled website, but a website nonetheless.

Of course, my instinct was to show it to people, so I found out about Angelfire for hosting from an even older book, and uploaded it, then showed it to people online. Needless to say, people didn't like it very much. But a few people did give me some somewhat useful advice, to drop Frontpage. They mentioned something called Dreamweaver as being better, and also suggested learning HTML. Which I did, until eventually I was using Dreamweaver as a glorified text editor.

Meanwhile, looking for the next thing to do, I decided to learn actual programming, in the form of Java. In retrospect, not the best choice of language for starting out, but it was one of the few that my local library had books for. The other alternative if I was to stick to library books was Visual Basic 6.0, so it was definitely the better of the two options. I went through the whole book of Java All-In-One for dummies, went through it, did all the examples, got to the end, and wrote a little Java clock applet for my website. I was so happy with myself, that I decided to go even further, and my next plan of a project was to make a Simcity clone in Java. Unsurprisingly, that didn't work out too well, and the failure put 13 year old me off programming for a while.

By this stage, I had long cured any illusions of coding being anything like the Matrix

The next time I came into contact with programming was when I was running a forum with a few friends. It ran off phpBB, and when installing mods, I noticed that all I was doing was editing programming code, which was kind of familiar. I looked into it a bit more, found out about PHP, and then quickly learned that. It was significantly easier to learn, both because it was my second language to learn, and also because languages with weak dynamic types are easier to learn in my opinion.

Of course, the problem with learning PHP, especially if you use the phpBB 2 code base as your example is you pick up bad habits. Now there's nothing inherently wrong with the language nowadays, but this was the PHP 4 days, with it's half-assed attempts at OOP. On top of that, something that is still a problem today is the amount of bad material floating around. Now, this is a problem with any language, but given PHP's large userbase, and popularity with hobbyist coders, it's far more pronounced in PHP than others I have used. So code found on the internet is ripe with a lack of care towards seperating output and processing, full of SQL injection holes, and often even relying on register_globals. But this isn't anything new. But it does lead me to recommend to everyone trying to get a good book. I started with this one, and while it did help me learn PHP, it set me back a long distance in terms of proper coding practices. Eventually, I got this book to learn how to code properly in PHP.

Around that time I started reading programming blogs, and found out about source control, in the form of Subversion, a rather important topic that no programming book seems to mention. Seriously, apart from Code Complete, which mentions it in glancingly a few times, the only book on my shelf that explains source control at all is the Linux Bible, 2009 edition. And that's talking about CVS. Now for all Subversion's faults, any source control is better than no source control. Later I was introduced to git and DVCS in general by some friends, and it's now my preferred form of source control (git for all Linux projects, hg if I have to work with Windows users). It didn't exist at the time, but for anyone looking for a good DVCS tutorial now, I reccomend Joel Spolsky's hginit. Even if you plan on using git or another program afterwards, it's the best basic explanation of the concepts of DVCS I've seen.

Linux Bible 2009
This was the only book to mention version control. Going back to my own journey, after that time in PHP, and several ill-fated projects, countless attempts at building a forum, a incomplete social network, and a completed, if basic, social network that I failed to get anyone to join, I moved on to Javascript. I spent ages messing about in that, making small scripts to move things around, before getting more ambitious. Yet again, I returned to the idea of a simcity clone. Given that this was before canvas became well supported or known about, the core of the program used a 200x200 table for the game's grid. It was every bit as slow as that sounds.

After a while of this, I returned to trying to make GUI desktop apps. I know, I know, many considering this a step backwards from web apps, but at the time, I couldn't think of many examples of web apps that had progressed of the "Oh, that's mildly interesting" stage. I had used Swing before, in my failed Simcity game, but wasn't particularly impressed by it. I gave SWT a go, before giving up again. I guess my problem with GUI apps resolves from my desire to control every last detail, compared to the designer-tool-centric design of most of the GUI toolkits. Somewhat bored with GUI apps again, but also bored of the general dislike of my main language, PHP, I went on a quick tour of several languages, including C#, C++, Ruby, Python, Ruby again, before finally deciding on Python as my new favourite.

Around this time, I switched to Linux as my primary OS. I'd tinkered with Linux for a while before this, but had always been too used to Windows to actually make the switch until then. Since then, Windows has been mostly relegated to use for gaming and iTunes.

I spent a while coding in Python, before going on a short hiatus from programming for a while, being somewhat bored as all the languages I learned being roughly the same at this stage, and no new ideas for a project to keep me interested. Projects are another thing I recommend to anyone starting to teach yourself programming, as even the failed ones or ones you get bored of are a good learning experience, especially if you have others looking over the code.

After a while, I decided to learn a less mainstream programming language, and after a quick straw poll on Twitter, Haskell emerged the winner. Luckily for me, Haskell has a good beginner's tutorial, Learn You a Haskell. I've heard it described as like Why's Poignant Guide, but it's not really. It's much more to the point which works for me (I'm sorry to say I found Why's Guide a rather boring read, having too much unneeded meandering with stories. But I accept it's good for others). It is a rather basic tutorial, but there is also Real World Haskell for when you need to go further, even if it isn't as well done.

And now, my current project is to write an IRC bot in Haskell to learn Haskell better. I'm still learning and improving, and it's going to be a handy headstart for college that I have all this done already, so the story goes on.

Programming Books

August 21, 2010 | categories: Programming

It used to be the case that when I wanted to learn more about programming, I would go down to the book store, buy a book about the language I was using and read it through. However, the problem with these books is that after a while, you only really want the last 5 or so chapters, detailing the bits of the standard library you are going to use. The language docs are usually a pretty good explanation of the syntax. You definitely don't need a full chapter devoted to if statements and loops, for the fiftieth time.

What you need on if statements after the first language is just a small paragraph like:

Python if statements are done like so:

if condition:
    doStuff()
elif other_condition:
    doOtherStuff()
else:
    doOtherOtherStuff()

Seven lines, not a whole chapter. Oh look, the language docs do that. Never mind then. Those chapters are now totally redundant. Spending €40+ for the last 5 chapters really doesn't make sense.

The other problem is one of quality. Several books promise to teach you everything about something, yet obviously fall far short of it. An extreme case can be seen in the two books I bought around 2-3 years ago, when I was first learning PHP.

1300 page long "Beginning PHP and MySQL", 200 page long "How to do everything with PHP & MySQL"

Which title do you think is more accurate?

Even apart from the amount of extra information in the Apress book, the quality of that information is far better. The smaller book doesn't even mention how to create a class. And it would have you believe that the only way to interact with a database is through the old mysql library, using functions like mysql_fetch_assoc(). Oh, and to really rub it in, it does actually mention MySQLi, but basically as a slightly faster version of the MySQL functions that needs to you put an i in your function calls, and nothing else. The example code in it always mixes code and markup, and then you wonder why PHP has such a bad reputation, when there are so many books like this floating around.

The problem is, many people will look at these books, and see the Apress book looking much more formal, and presume that's it's full of unnecessary detail. I did at first (hey, I was just starting out), which is why I initially bought the second book, then had to go back and buy the other one when I finished the first one, and realised I could of learned the same amount of stuff from 2 hours and an internet connection.

Of course, eventually, every programmer moves on from these books to more general ones about coding in general, rather than specific technologies and languages. The problem is, many book stores don't bother selling anything more advanced. And the only computer shop where I live, PC World, has an even more pathetic collection. So you have to wait ages for something to turn up. Or have someone recommend a book, and then buy it online. You can't browse through books, flicking through a chapter of each to see which looks better, on eBay or Amazon unfortunately, which rather limits the options for just looking around for something good.

Moving on to better books, like this one.

The first proper programming book I got was Code Complete, second edition, which I bought roughly a year ago. I'd heard a few people recommend it before, but hadn't went to buy it. I was amazed when one day, among all the copies of Excel for Dummies and iPhone for Seniors, there was a copy of Code Complete in the book store. I presume someone had ordered it in, and the store had just got a few of them, because it's sadly not the kind of book they usually get. I suppose they rather books that don't require the customer to have some knowledge already. (Which is odd, because surely they'd be able to sell two books with an offer like "Buy this book to learn how to program, and this book to learn how to program better.")

Unfortunately, even when you know what book you want, the book stores don't do themselves any favours. Around this February, I had €30 in book vouchers for the local bookstore left from Christmas, and decided to buy Design Patterns with them. I had the PDF version, but I still read best from dead trees, so I had a look. Unsurprisingly, they didn't have it there, as most of their books are of the Language x for dummies/Buzzword y in 24 hours variety. Oh, and they have Code Complete again as well, but I already have that. So, as I had expected, they would need to order it in. I had checked beforehand, and the book was €30 delivered from Amazon. I expected it might be maybe €40 in the bookshop, cheaper once you factored in the vouchers I already had. It wasn't €40. It was €82. I don't know what planet they are on in the bookstore, but needless to say it's led to Amazon being €30 richer.

A welcome development is the trend towards free online books. Examples such as Dive Into Python, Programming Ruby, Dive into HTML5 and Real World Haskell do help with their respective languages. And the fact that they tend to be more concise than the usual books on the shelf (I know they come on dead trees too, but nowhere near me sells any of them) also makes them more useful, even despite my preference for reading from a physical book.

Vim

February 28, 2010 | categories: Programming

On the advice of Sirupsen, I have been trying out vim during the last few weeks. Up until now, my editor of choice has been gedit on Linux, or Notepad++ on Windows for most stuff. I have also used VS on Windows, which while good for C#, it doesn't really help writing multi-platform programs. And I've used Eclipse a bit, back when I used Java for a while. But, most of my programming is done in languages such as Python, PHP and Javascript, and for the small size of my projects, IDEs tend to get in the way, and they also aren't quite as good for dynamically typed languages anyway.

So first of all, to quote one of my tweets:

After using it a while, I apologise to vim users. It isn't weird and crap, it's just weird.

I haven't exactly been the most positive of vim. After all, as a Linux user, it's nigh impossible to use many of them without one of them using vim or a vim-like interface for something. And while nano manages to be relatively easy to understand, vim does not. Because vim is weird.

But after a while of using it, I realise it's weird for a reason. Want to get rid of a line? In other text editors it's:

  • Left arrow to start of line
  • Hold shift
  • Hold right arrow to end of line
  • Press delete

In vim it's:

  • Type dd

Don't like that 13 line function anymore, and want to delete it?

In normal editors it's:

  • Up and left arrows to start of function
  • Hold shift
  • Down and right arrows to end of function

Vim is:

  • Type 13dd

Weird, obtuse, but once you learn it, it's far more efficient. A final example. Suppose you want to rename that variable from $car to $vehicle:

A normal text editor?

  • Edit
  • Search and Replace (somethings this is part of search anyway and under Ctrl-F, other times it's its own menu, under Ctrl-H)
  • Type in $car.
  • Type in $vehicle

Vim?

  • Type :%s/$car/$vehicle/

At this stage, I'm still not getting the most out of vim. After all, I only know a few basic commands (and even then I'm missing a few. Any vim users care to tell me how to Select All?). Yet I'm already finding it easier and faster than I did in other editors. vim is also cross platform, unlike the Windows-only Notepad++ and Linux-only gedit, which means I can use the one editor across all OSes. For that matter, it's even available on my jailbroken iPod touch.

Another useful feature of it is it's huge styling and plugin community. Again, Sirupsen pointed me to BusyBee. I didn't quite like that, so I found Mustang, the theme it was based on. I haven't yet found plugins that I'd reccomend, but the choice is huge. One of the inbuilt ones, allows you to set different options per language. I'm doing a project in Ruby atm, and in any other editor I'd have to change my settings to and from my personal preference of tabs and Ruby style of double spaces. With vim, I can set it to do tabs with all other files, and just do the silly double space thing with Ruby.

First projects

February 05, 2010 | categories: Programming

One of my online friends has started learning programming in C++ and Python a few months ago. While he seems fairly competent in it, he was complaining that he feels he hasn't acheived anything worthwhile in it. He said he just can't think of anything useful to make.

This reminded me of when I was starting out (and again when I was starting out with Javascript, and again with PHP, and again with... you get the picture). Like him, I also struggled to think of something to make. My first trivial program was a Java clock applet when I was 13. I saw one on another site and decided to make one for my own. My first non-trivial program was a social network, written in PHP when I was 15. Two years to think of an decent idea that I could keep working on until completion.

I think the problem with beginners projects by self-learners is one of overreaching. I've certainly done it a lot. If they try to write a social network, they want to write Facebook. If they write a forum, they want to write vBulletin. If they write a game, they want to write Fallout 3. All of these were written over a significant amount of time by large groups of people.

My advice is this: Start small. Reduce whatever you plan to build to it's basics. A social network can be reduced to users who can add friends, edit profiles and leave comments on other's profiles. A forum can be reduced to users creating threads and posts. A game can be something like Pacman. Implement these basics. When you have them done, you will have enough motivation to get the rest of the stuff done (login, logout, etc.). While you can't assume designing a successful product is as simple, as the linked blog post shows, designing a beginners project to practice your coding is easier than designing a product to base a business around.

Despite not being quite that simple, many successful products are based on simplicity. Compare Twitter to Facebook, punbb to vbulletin, Google Chrome to Firefox. Simple and fully-featured are two equally valid design strategies (although I tend to favour simple products myself), but simple ones are much easier to get going.

Once you have your basic program written, you can then add features to it. You could add image uploads to your social network, bbcode to your forum or extra enemies to your game. Further again, you can add groups to your social network, tagging to your forum, and new level types to the game. Since you already have a working base to start from, it's much easier to look at it feature by feature, instead of waiting ages until you have anything that works.

Seeing your old code

January 17, 2010 | categories: Programming

Lately I have been working on one of my oldest pieces of code again. This code is that of a website I created for my school about 2 years ago, when I had just started programming.

Given that (a) it's written by a beginner (me two years ago) and (b) it's PHP, you wouldn't be surprised to hear that the code is bloody awful. Even for a basic CRD app (I hadn't programmed the U bit), it sucks.

Hardcoded numbers and categories everywhere, no sign of code organisation, redundant code, copy/paste as a design pattern and inconsistent naming. It's a wonder that information going into the database was even escaped. And let's not talk about character encodings.

Needless to say, now that I've picked up the code again, it's had a swift rewrite. It's still pretty crap, but at least it doesn't screw up completely if you type a euro sign.

Has anyone else ever seen some of their first code and gone: "I can't believe I wrote this crap"?

Next Page »