Archive for the ‘Programming’ Category

Learn to Program with Poker / Code with Cards

Saturday, June 11th, 2011

P1020341 I love learning new programming languages. I’ve been reading Bruce Tate’s “Seven Languages In Seven Weeks” with the hope of getting a flavour of all the different paradigms on offer. It is impossible to learn a new languages by simply reading – you have to get stuck in with the code to gain a true understanding. Mr Tate gives us the opporutunity to dive into some practice examples he has thoughtfully prepared for us. They are a good start, but i like something more involved to move onto.

And that’s where poker comes in. Texas Hold’em may or may not be your cup of tea, but it is a game to perfectly test your understanding of the languages you are writing in.

Just look at the different elements that come into play:

  • Scoring hands. This is a non-trivial task that is on the surface, quite straightforward with OO. But scoring hands is perfect for functional languages. For extra difficulty, remember – Ace is both high and low!
  • Who wins what? With varying players in the game, and varying amounts in the pot, this could get tricky!
  • Performance. Ok, so you’ve worked hard on your perfect card-scoring functions. Now we can really test how well you’ve done by asking “how many hands can we score per second?”.
  • DSLs. Perhaps the language you are using is good for developing DSLs. Could you write a DSL that mimics a hand of poker? One round? Or even an entire poker game? Something such as the following would be a start (I’m pretty sure Scala’s implicit type conversions will perform this miracle):

“AC QH 10S 2D 4C 8D AD” vs “3S 10C 3C 5H QD QS 7S”

  • AI. If you’ve played poker, you know it can be an infinitely complex game of deep strategy. But even a simple A.I. bot can give a player a good run for his money. Start with the basics (hand strength, pot odds, outs) and go from there.
  • Immutability. Can you complete your program whilst keeping it completely immutable? I don’t mean scores, chips, etc. I mean, can you avoid mutable data structures, state-ful functions, etc.

Once I’ve written a basic poker engine once, it becomes easier to use it as a comparison between languages. I have a firmer understanding of which languages perform better in which circumstances. Oh, and it’s definitely more fun than writing business applications!

Get Better At: IntelliJ IDEA

Monday, December 13th, 2010

IntelliJ IDEA is a wonderful piece of software.

And that’s all I’m going to say; I won’t run comparisons against other popular IDEs, because that’s a subject for another day. Today, I want to talk about sharpening your skills with IDEA.

Shortcuts

We all know that shortcuts improve our productivity. Here are the ones I find most valuable:

  • CTRL-W and CTRL-SHIFT-W: Select progressively greater / lesser levels of code
  • CTRL-ALT-V: Introduce Variable. See also Introduce Field / Constant
  • type “iter” TAB: Java 5 style for loop Live Template. See sections below for more info on this
  • ALT-LEFT and ALT-RIGHT: View previous / next tab.
  • CTRL-ALT-LEFT and CTRL-ALT-RIGHT: Back / Forward Tab (in history).
  • CTRL-N and CTRL-SHIFT-N: Find Class and Find File.
  • ALT-F12: Show class outline (press again to also show Inherited members).
  • CTRL-D: Duplicate Line.
  • F2 and SHIFT-F2: Go to next / previous error (or warning).
  • ALT-INSERT: Create new file popup.
  • CTRL-SHIFT-F: Find in path.
  • CTRL-ALT-T: Show list of Live Templates.
  • ALT-ENTER: Show Intentions (see below for more info)

Live Templates

Live Templates are snippets of code you can insert in and around pieces of code. The most common one would probably be “Surround with If”, which takes the highlighted code and surrounds it with an If statement. What’s more, it formats the code correctly so you needn’t worry about tab spacing.

Another good Live Template is the for loop generator. Type “iter” and then hit TAB. IDEA intelligently selects a list of collections you might wish to iterate over:

To create you’re own templates, I would recommend you take a look at this Jetbrains blog post describing how to create a null-check template.

Quick Lists
You can create a shortcut to open a popup of commonly used items of your choosing. Within Preferences, go to Quick Lists to define a new list, and then Key Intentions to create a shortcut for that list. As an example:

Intentions
Intentions can be used to perform common actions, or solve problems with your code. Hitting ALT-ENTER over a valid piece of code will give you common actions. Doing the same over an error will give some suggestions such as:

  • Migrate Type
  • Cast expression
  • Import unknown symbol

Below is an example of both forms of intention:

The Developer’s Affliction

Saturday, November 21st, 2009

Lately I’ve been spending a lot of time working on little projects of mine (one of which being this blog). What seems to make us developers unique is the way our job and our hobby is essentially the same thing – writing software. It’s what gives us the advantage over many of our peers; when going to our first job interview we may have a hard time listing any previous commercial experience, but the line “Yes, but I’ve been writing software of some variety since I was 13 years old” is often likely to get at least a foot in the door.

I’ve read many articles and comments from around the net which encourage hiring managers to look for people with a real passion for technology. It helps us strive for self-improvement, and aim for a higher standard of work.

However, after spending many evenings and weekends trying out the latest web framework or refactoring my neural-net application for the hundredth time (I’m never pleased with it), I wonder if I can / should / want to keep developing in my spare time.

Yes, I really want to give Grails a spin, but I also want enjoy my time away from work.

Wouldn’t it be great to finish that killer app you’ve been writing, an app that will make you an irresistible hire for Google or give you a base to start a company?

You know CoverFlow right? That was someone’s pet project before Apple bought it and put it on everyone’s iPod and iTunes. Even if I didn’t get any cash from that I would die a happy developer. Facebook, Apple, Google, Microsoft – these are companies that were all spawned from pet-projects at one time or another, and made it big.

For the victor, the spoils are huge. But for the rest of us, there’s a small chance that we’ve just let the rest of our lives pass by and those lines of code we wrote at 1am were a bit of a waste.

Know Thy Tools – The Programmer’s Checklist

Thursday, November 12th, 2009

I was hit by a quite sudden realisation about 6 months ago – the amount I don’t know about the basic tools I use to earn a living as a software developer. Up until that point, I had developed with a view to being “good enough”, instead of striving for perfection.

Within those 6 months I went about trying to rediscover the craft of software development, so that I would hope to eventually improve and become a better programmer.

I drew up a quick Programmer’s Checklist, in no particular order:

Keyboard
Switching between keyboard and mouse incurs a productivity penalty. Learn to stop using the mouse for these basic activities:

  • Page-Up and Page-Down
  • Jumping to the beginning and end of lines using Home and End
  • Moving left and right, one word a time using CTRL-LEFT and CTRL-RIGHT
  • Selecting words using SHIFT-CTRL-LEFT and SHIFT-CTRL-RIGHT
  • Using DELETE as well as BACKSPACE

IDE
Believe it or not, I was using only about 20% of the features of Eclipse for Java development.

  • Use refactoring tools as much as physically possible. Ultimately you may find some of them overkill, but there are some real gems there. In particular, Introduce Variable and Rename have become rather invaluable for the work that I do.
  • Learn keyboard shortcuts. I find I can only learn one or two new shortcuts at time, but over the course of a month I might have most of the IDE mapped out in my head.
  • Understand the tools designed to help you navigate through a large project. I’m talking about Jump to Declaration, the in’s and out’s of the search tool, etc.
  • Code manipulation tools such as being able to comment out an entire block of code with one keystroke, duplicating lines, and moving lines around without using the mouse.

General Productivity

  • Batch and Automation. For me, there are things I do over and over, such as restarting my JavaEE container, rebuilding a project, running unit tests, executing DB scripts, etc. These should all be a few keystrokes away. I use Launchy on Windows with to execute Batch files I’ve written.
  • Scripting language. There are so many times I’ve needed to get things done programmatically, and I haven’t wanted to use Java. I would recommend that all developers know one commercial language (Java, C#, C++ etc) and one scripting language (Perl, Python, Ruby, Groovy).
  • Regular expressions. This will help you everywhere. Start learning how to search text with regular expressions, and soon you’ll find yourself using them in places you never expected.

That’ll do for now. Hopefully this list is helpful in your path to becoming a better developer – it has for me!