Posts Tagged ‘ 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.

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.

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

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).

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.