This article was originally published at /home/siddhi. It has been republished here with the permission of the author.


You get better at programming just like how you get better at anything else – by practicing. The question is: how to practice in an effective way?

There are two ways to practice any skill. One is practicing individual techniques, the second way is to practice putting it all together. (PS: practicing on the job doesn't count as practice)

Consider a sportsperson, lets take football as an example. In practice sessions they might practice passing, and then they just do passing drills over and over for a few hours. This is an example of practicing individual techniques.

You also want to practice putting it all together. A sportsperson might do that by playing practice matches against another team.

What are the equivalents for programming?

Projects

Let us start with how you practice putting it all together because the answer is more straight forward. You do this by writing side projects. These side projects can be tiny or big, it does not matter. Doing a side project helps you learn how to put together different concepts into a solution.

Many students learn a language from the building blocks – variables, data types, conditionals, looping, I/O and so on. But at the end of it, they are still unsure on how to put all these pieces together in order to actually solve a problem.

Writing small projects helps to develop this skill of putting it all together.

Katas

Now let us discuss the question of how to practice individual techniques. For this we do coding katas. What is a kata? It is a term borrowed from martial arts, where students repeatedly follow a sequence of moves in order to improve their form and technique.

In a similar style, there are a number of coding katas. Here is a site and another one each of which has a number of such katas. These katas are generally simple problems. The point of a kata is not to solve the problem, but to use it as a canvas to practice technique.

Robot Kata

The robot kata is one of my favourite ones which I use in a lot of my python trainings. Here is the problem statement:

  • Imagine a robot that is at position (0, 0) and facing North.
  • It can take 3 commands: Move forward, turn left or turn right.
  • Turning left or right changes the orientation of the robot by 90 degrees without changing the position. For example, turn left will make the robot remain at (0, 0) but it will face West now
  • Moving forward will move the robot 1 step in the direction faced, so if it is at (0, 0) and facing West, after moving forwards it will be at (-1, 0) and still facing West

Given a sequence of commands (eg: FLFFLFFFRRRF) then calculate the final position and orientation of the robot.

Robot kata

Practicing with Katas

So what are the kinds of practice you can do with this kata? Obviously you can simply solve the problem, and that is a lot of fun, but you can also add some constraints to practice specific things. Here are some example constraints, you can choose any one and try the kata again with that constraint:

  • Use object oriented code to solve it (if you want to practice OO skills)
  • Use only functions, without creating any classes
  • No function body more than 2 lines long (practice writing small functions)
  • Do not use if/then/else conditionals anywhere (when you want to practise using polymorphism)
  • Use function composition only (to practice functional programming style)
  • Write it along with unit tests (test driven development style)
  • Write it so that no function should returns a value (all void / empty return functions)
  • Use a design pattern to solve the problem (practice design patterns)

Each time you try with a different constraint, the code will turn out in a different way. Not every result will be beautiful, but that is not the point. The idea is the practice, not the final solution. Then leave the kata for a few months and then come back to it and practice it again (repetition is a key component of practice).

Did you like this article?

If you liked this article, consider subscribing to this site. Subscribing is free.

Why subscribe? Here are three reasons:

  1. You will get every new article as an email in your inbox, so you never miss an article
  2. You will be able to comment on all the posts, ask questions, etc
  3. Once in a while, I will be posting conference talk slides, longer form articles (such as this one), and other content as subscriber-only

Tagged in: