Three things I learned about software

June 29, 2007

Via Reddit, I found a post by Scott Hanselman about what he learned about software in college and while not in college. He finishes by asking the readers to post what they learned. Here are mine.

Learned in college

  1. COBOL sucks
  2. RPG/400 sucks
  3. AS/400 sucks

Yeah, the programming classes were pretty bad there :-/ I mean, who teaches a language (RPG/400 v3) with no local variables, where identifiers can only be 6 characters in length and where subroutines can’t take parameters?!

Learned while not in college

  1. When you are asked to just make “a few quick and easy” modifications to an existing software, it actually means “make modifications that will take you a lot of time and hard work for which I do not want to pay.”
  2. There’s a lot more software written by people who really have no clue how to program than you may think.
  3. If you don’t have unit tests, any modification you make to a program will break another part of the program.

Man, just three? I feel like I could go on! I think I’ll make another post about this in the near future :)


Xbox 360 adventures

June 13, 2007

My friend Joel has written about his absolutely awful experience with Microsoft Xbox 360’s support. He got the 3 red LEDs circle which is code for hardware failure. Since this problem is acknowledge by Microsoft, all you need to do is call customer service, say what you have (they may make you run some tests) and they’ll ship you an empty box. When you receive the empty box, you put in your defective console, send it back and Microsoft sends you a brand new Xbox 360. The whole process is usually quite fast; within 5 business days I had my new console when the same problem happened to me. Joel has been waiting for his empty box for over two weeks now, and no one at Microsoft can tell him what is going on, where the empty box is, or if it’s even been shipped.

Read it, it’s quite funny. I also posted the story on reddit. Up-vote it if you like the story.


Back online

June 11, 2007

My last post on my top 5 vim scripts was posted on Reddit and I’m guessing the heavy traffic (1622 individual IPs on Sunday) quickly overwhelmed the server, a small Pentium 3 450 MHz.

We’re back online now, sorry if you were not able to read all the important things I post ;-)


My top 5 vim scripts

June 9, 2007

Linux.com has an article about five scripts that make life easy in Vim (for the author at least.) I skimmed it very rapidly to see what the author used, but none seemed too useful to me. This is why I decided to write my own top 5 list of vim scripts.

  1. BufExplorer1: For the longest time, I worked with Vim by editing one file, saving and exiting, editing another file, saving and exiting and so on. Working with Emacs, I learned to stay in the editor for extended periods of time and opening a lot of files. When I came back to Vim, I needed something to make it easy to edit a large number of files in one session. :ls and :b were passable, but I disliked the two-step operation to switch from one buffer to another. (Note: it was only after I installed BufExplorer that I learned you can Tab-complete buffer names.) The folks from #vim quickly pointed me to BufExplorer, and it’s excellent! You use \be (or in my case, ,be) to display the list of opened buffers in your current window (the list is the same as the output of :ls.) You go over the buffer you want to visit and press Enter and you’re there. It’s quick and easy. I’m so used to it, sometimes I write ,be in irssi.

    Some people use minibufexpl, I tried it, but it didn’t seem to play too well along with Project, so I uninstalled it

  2. Project2: One thing I always liked about IDEs and from the Textmate screencasts was the file list on the left of the screen. You can have that with Vim just by opening a directory, but you can sometimes lose the list and it’s annoying to bring it back. Listing the content of a folder also showed files you didn’t care about. Project.vim is a script that displays only the files you want in a vertical split window that always sticks around (even when you use C-w o.) You start a new project with the :Project foo command. Once that’s done, \C will ask you a few questions (name of the project, absolute path of the project, file filters) and the files and directories will be loaded in that buffer. You write it (:w) and next time you open vim, you do :Project foo and the file list comes back. I found this script to be extremely helpful in projects with a large number of files and directories.
  3. NERD_Comment3: I write code, and often I want to comment/uncomment large portions of code. I could do it manually, but it’s usually slow, especially in languages such as Python where the comment character comments only until the end of line. NERD_Comment gives you a handful of commands to comment and uncomment code with a simple command. The most basic way to use it, select code in visual mode, press \c<space> and the section will be commented. Select the same section again (use gv) and do the same command, and the code will be uncommented. This is not a big time saver, but it’s quite useful.
  4. Align4: Align is a plugin to align code according to a list of characters. Suppose you have the following code:
        firstname = 'Vincent'
        lastname = 'Foley'
        age = 23
        height = 170
        weight = 180
        blog_url = 'http://gnuvince.net'
        favorite_drink = 'beer'
        

    Personally, I don’t like code that’s unaligned like that, I find it hard to read (Damian Conway, the author of Perl Best Practices agrees with me on that point.) The solution is to align the equal signs, but that’s kind of a drag to do manually. The Align script automates this process; the command :Align = will align the visual selection on the = character. You can use the :AlignCtrl command (which has a lot of options) to set how many spaces to pad with, left align or right align, etc.

        firstname      = 'Vincent'
        lastname       = 'Foley'
        age            = 23
        height         = 170
        weight         = 180
        blog_url       = 'http://gnuvince.net'
        favorite_drink = 'beer'
        

    Small problem with Align however, in UTF-8, it does not deal well with accented characters. I contacted the author about this problem, but I haven’t seen an update so far.

  5. taglist5: When you are brought into a large project, it’s sometimes hard to learn quickly where things are defined and whatnot. A good way to help is to use taglist (given that ctags supports the language you’re using) which will list classes, methods, functions, variables and other elements of your language and make them easy to access. This makes it easy to jump from one usage of a function to its definition.

1: http://www.vim.org/scripts/script.php?script_id=42
2: http://www.vim.org/scripts/script.php?script_id=69
3: http://www.vim.org/scripts/script.php?script_id=1218
4: http://www.vim.org/scripts/script.php?script_id=294
5: http://www.vim.org/scripts/script.php?script_id=273


181 lbs

June 3, 2007

I mentioned earlier that I was trying to lose weight. I’m happy to report that I’m now at 181 lbs, down from 190 three weeks ago!


Some Django tips

June 3, 2007

I’ve been using Django for web development at work for a few months now, and here are some tricks I’ve picked up along the way.

  • Install IPython: IPython is an interactive Python shell, much like when you just type “python” at your prompt. The nice thing about IPython is that you can use it like a breakpoint in your code. You import IPython.Shell.IPShellEmbed and you call IPShellEmbed()() where you want execution to stop. When this point is reached in your code, you will find yourself in an IPython interactive session with all the variables of your code. You are then free to play around with the variables, modify them and do pretty much whatever you want. I cannot emphasize enough how useful this is.
  • ChoiceField vs ModelChoiceField: When you write a form with newforms, if you want a ChoiceField or MultipleChoiceField that has its choices from the database, use ModelChoiceField and ModelMultipleChoiceField instead. When you give the choice argument to ChoiceField, the data is loaded at import time. This has two consequences: (1) when you add new data, it won’t appear in your form until you restart the server, and (2) in your tests, your production data will be loaded into your form instead of your test data.
  • Add your custom form validations: The current documentation for newforms does not cover how to do custom form validation, but it’s hidden somewhere in the tests. It’s really simple to do, you write a clean_XXX(self) method where XXX is the field that you want to test. The method should raise django.newforms.ValidationError if the input is incorrect and return the data if it is. Here’s a simple example:
        def clean_number(self):
            if 1 < self.clean_data['number'] < 10:
                raise ValidationError(u'Please choose a number between 1 and 10')
            return self.clean_data['number']
        

    Note: I’m not quite sure, but I think the property name for the clean data has been changed from clean_data in 0.96 to cleaned_data in the development version.

  • Use the {% url %} tag: The {% url %} tag takes a view name and its parameters and does a reverse lookup to return the wanted URL. This means you can make modifications to your urls.py file and your links will keep on working. The documentation for this tag is here
  • Learn to write template tags and filters: There is a very strong chance that you will need to perform a computation in your template at some point. You should definitely learn to write tags and filters for that reason. Here’s an example, you have a generic view to display a list of objects and the list is paginated. You want to add links to every page number at the bottom of the list. You will need to loop from 1 to the template variable pages. A small filter such as {{ pages|range1 }} (range1 to show that it’s from 1 to pages inclusively) will help tremendously.
  • Generic views are really useful: Although I don’t use them all, the object_list generic view will surely become your best friend. It gives you free pagination! One less thing to worry about.
  • Make your own context processor: If you need to pass an information to every template, don’t do it by hand, create a context processor and add it to TEMPLATE_CONTEXT_PROCESSORS in your settings.py file. From then on, all templates that use the RequestContext class will have that information available.
  • Write tests: Not really a Django-specific item, but it’s always a good idea to have a complete test suite. Learn how to use django.test.TestCase with fixtures. Also learn how to use django.test.client.Client to be able to test your views.

Brood War: Protoss and Terran campaigns’ complete

June 3, 2007

My friend Étienne and I are currently revisiting the Brood War universe: we are completing the single player campaigns. We finished the Protoss campaign a couple of weeks ago. Étienne did the last mission and ended it with a really awesome (and incredibly cheesy) army of Photon Cannons: more than 200 around the Xel’Naga temple and some Corsairs to counter the long-ranged guardians.

Last night, we finished the Terran (UED) campaign. I did the last mission, and it wasn’t nearly as hard as I thought it was going to be: I quickly ran up the tech tree to Battle Cruisers and from there on it was pretty easy to destroy the three cerebrates and brings the medics to the Overmind.

If you still got your copies of StarCraft and Brood War around, I highly recommend that you play them again, it’s a whole lot of fun!