Programming

Vim

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.

Tags: ,

Sunday, February 28th, 2010 Programming 1 Comment

First projects

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.

Tags: ,

Friday, February 5th, 2010 Programming No Comments

Seeing your old code

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”?

Tags: ,

Sunday, January 17th, 2010 Programming 3 Comments

Source control and me – Why I use git and github

The biggest change to programming for me this year (apart from spreading out from just PHP and Javascript) is that I now use source control for most of my projects. I started out on subversion because it was widespread, easy to use (with TortoiseSVN, I was still a Windows “everything must be GUI” user at the time), and there was a handy tutorial for it published for it on a blog I happened to read. I used that for a good while until a friend showed me git and github. I’d used Sourceforge and Google Code for a while, because although my projects are too small for anyone else to be interested in adding to them, the benefits of having a remote source control service that’s always accessible from any computer are huge for me. And it’s handy for showing people the program in more detail if I need help on Stack Overflow.

While I was (and still remain) unconvinced about the benefits of distributed source control versus normal source control, and more specifically of git vs svn, the benefits of github versus google code (which I had been using at the time) were more than enough to convince me to make the switch. It also helped that at the time, Linux had recently become my main OS with Windows being relegated to usage for syncing my iPod Touch and gaming, and I had become much more familiar with CLI usage of the system, so the command line orientation was no longer the problem it once was.

Branch history of a small github project

Look at the pretty graphs! (ignore Chromium for Linux's fail at positioning. My cursor is actually on that big green dot on my screen. It's just the screenshot gone wrong. This is the fault of flash+chromium, not github)

Setting up git and github was simple. I could give you instructions on how to do it, but Sirupsen has already done this much better than I could for Linux users. For Windows, there is another tutorial hosted on github,

Usage of git with my github account is equally simple.

  • Change files
  • git commit -a
  • Type commit message into nano (or vim if you’ve changed your system editor, or notepad if you’re on Windows)
  • git push origin master

The only thing I missed during switching was that Subversion numbers revisions like 1, 2, 3…20000 while git uses md5 hashes. But I can live with that.

Tags: , ,

Tuesday, December 29th, 2009 PCs, Programming 2 Comments

Programming: Simple Python Todo List manager

My latest programming project is a todo list manager written in Python.

Features

  • Add, Remove and show items
  • Manage multiple lists
  • Lists are stored in JSON

It also has a GUI, but that needs a lot of work. As always, the source code is available on my github. Unusually for my projects, it is already fully functional (though as I said, the GUI needs a little work).

Tags: , ,

Monday, December 28th, 2009 Programming, The Playground No Comments

The Playground

Over the last 2 years, I’ve done a lot of personal programming projects. Some of these are available on my github, but any information about them is spread among Twitter and a few forums (some of which no longer exist). So over the next month, I’m going to blog about a few of these. I’ve chosen the name “The Playground” for the category because it accurately reflects both the nature of the projects (small projects that are mostly tinkered with, with very little concrete plans) and the reasons for making them (to practice programming, or to mess about, or because I was bored).

Anyway, for the time being, here is a quick summary of my projects currently in my github repo:

Twitter Matter

This is a simple Python program to send tweets from the command line. At the moment, it can send tweets, and get the latest tweets from your timeline, your friend’s timeline, and @replies to you. I made it mainly because although I’ve always said Python looks like a nice language, and I know the basics of it, I never wrote an actual program in it beyond Project Euler maths puzzles.

JavascriptCity (aka Webcity, webicity, JSC)

This is an attempt to write a Simcity clone in Javascript. I got quite far into it, with the ability to set zones, have them develop and build roads actually working. However, at the current stage any change I make is too bad for performance that it is basically on hold. I can say it’s definitely not impossible to write a Simcity clone in Javascript, but you need someone with better Javascript skills than me to make it, and probably someone who knows about <canvas>.

Young Developer Forum

This is a program set up by me and a Iroup of people on a forum which has since shut down. It didn’t really get very far.

Machat

This is an IM server and client that I wrote in Java. It was my first experiment in GUI programming and Threading in Java. Some revisions of it work, and some don’t.

JS Battleship

This is a battleship game I am writing in Javascript. It re-uses a lot of code from JavascriptCity, but without the huge 200×200 table grid that caused a lot of programs.

Tags: , , , , ,

Saturday, December 12th, 2009 Programming, The Playground No Comments

Learning C++

As usual, I’m falling behind on blogging. One of the reasons for that is I am currently learning C++.

A while back, I had a brief exploration into C++, and learnt the basic syntax, then moved on and didn’t do anything related to it for 6 months. The majority of my programming has been done in Javascript, Java or PHP, though I have looked into other languages. Since Java and C++ are quite similar in some respects, and I did learn the basics beforehand, I’m not starting from scratch completely, though in some ways it would be beneficial if I was. An example is this:

int array[somevar]

Perfectly legal, and quite common in Java. In C++, this shouldn’t work. Except it did for me, because it is a g++ extension. Someone with no programming experience would not have tried this. This is something I hadn’t expected. I had expected C++ to be more like Java, and PHP where there was one standard of the language (and the only differences were between new and old), but it seems, like Javascript, there are a few variations per implementation.

Another thing that surprised me was the small size of the standard library. Compared to the Java API, and the .NET Framework, the C++ Standard Library is tiny. No GUI code, no threads, no networking functions.

The final hurdle I encountered is there is so little information about C++ online. On the one hand, I should of expected this, as the origins of C++ predate the internet, and it’s not a web language, but it means I have a lot of difficulty finding material. My local book store has only 3 shelves of computer books, with the programming catalogue nearly entirely taken up by C#, Java and PHP. As well as that, the books I found in the library are quite old and mostly aimed at people coming from C or Pascal, or a variety of other older languages.

I’m wondering what other people’s experience of learning C++ is.

Tags: ,

Wednesday, August 26th, 2009 Programming No Comments

First serious Java programming project

I’ve done a good bit of programming in PHP and Javascript, but up until recently, my desktop programming experience has been limted to calculators and other similar simple programs. However, that is changing as my current programming project is an IM client and server written in Java. The name is Machat (hey, I’m not very inventive) and the source code is under the GPL and available at my new github account.

Currently it is in a VERY rough state (in fact it doesn’t even send messages) and I don’t really expect it ever to be succesful, but it’s still an interesting project to learn about GUIs, threading and network programming (3 things you don’t do a lot of in PHP).

Tags: ,

Monday, July 27th, 2009 Programming No Comments
 
I'm a 17 year old hobbyist programmer who programs in Java, PHP, Javascript and C#. Among the more interesting things I've programmed are a Javascript Simcity clone, and I'm currently doing a Java IM server and client. I'm also a big fan of video games, with my current favorites being Team Fortress 2 and Call of Duty 4.

Github: Macha
Del.icio.us: MachaHack
Twitter: MachaHack
StackOverflow:

SuperUser: