The Best Feature of Go

I’ve been programming since the late 90’s and I’ve done quite a bit of coding in C, C++, a lot of it in PHP and some in Python as well. On the front-end I’ve done some JavaScript and I’ve also had the misfortune of programming in Java 😉

I started programming in Go in 2012 and since then I haven’t wanted to program in any other language. I’ve had a handful of large Go implementations across two companies and by now I have my own short list of favourite features.

One of those features is not mentioned very often but it has changed things significantly for me, and that’s what I’m going to discuss here.

Continue reading “The Best Feature of Go”

Inheritance Semantics in Go

Contemporary application design discipline is deeply rooted in Object Oriented Analysis and Design and inheritance is a key concept in OOAD. Go does not support classes and inheritance in their classic OOP sense but since many of us are trained in OOP, the loss of an important design concept sometimes feels restrictive.

Even though I knew about embedding and interfaces, their connection with classic inheritance wasn’t quite obvious. I set out to understand how I could emulate the coarse inheritance semantics in Go, without going into fine nuances. That in turn has helped me understand embedding and interfaces in a deeper way and I hope it would help me better design Go types and methods for extensibility.

Continue reading “Inheritance Semantics in Go”

The Compiler as a Refactoring Aid

Recently, I sat down to refactor a Go application with a high-level design objective in place. The application had two conceptually separate entities implemented in different files but mashed into a single package. I needed to separate them out into their own packages. I wasn’t using an IDE — just Emacs with basic formatting and non-contextual auto-complete aids.

I started out by creating a new directory for the package to be split out and moved the files that contained most of the relevant code into that directory, without thinking of the consequences. I could just invoke the compiler and let it guide me through the process of fitting the pieces of the puzzle together. One of the nice features of modern compilers is that they don’t continue dumping out errors beyond a limit. This allows fixing a program in small steps, going by the changes in errors produced by the compilers.
Continue reading “The Compiler as a Refactoring Aid”

Go Workshop

Last week, I conducted a 2 day Go Workshop at my workplace. It was fun.

I started day 1 with the excellent Go at Google presentation by Rob Pike, followed by my own presentation of Go’s key features. The rest of Day 1 was spent taking the Go Tour.

The coolest thing I did was on day 2. I mirrored my laptop on the projector and went through a fresh install of Go from source on my [newly allocated] dev box. Everyone else had identical dev environments so they could follow what I was doing on-screen. At the end of it everyone had a functional Go workspace!

What’s even better, I showed them how super easy it is to create packages and use them in a program. From conducting the installation to demonstrating package creation, it only took 1 hour. This included 10 minutes fiddling with Vim settings for syntax highlighting — the most difficult part of the entire session 😉

Presenting slide decks is one thing. It’s a totally different experience when you do a live demo with no prior preparation except the confidence that what you’re about to demo will work without much fuss.

Why I Program in Go

Go is a fresh new programming language, that has come out of Google and is primarily targeted towards server development. It is developed by some very accomplished computer scientists, like Ken Thompson and Rob Pike. I recently launched a significant new product built with Go at work, and it has proved itself out very well in terms of developer productivity and performance. So much so that many other teams are also giving it a go (oh, how punny this language’s name is).

Think about it. How often do you come across a programming innovation that improves productivity and computational performance? Continue reading “Why I Program in Go”

Find and Edit File

While working on a huge code-base with several thousand source files, it becomes difficult to remember where each file is. If you use conventions like one file per class, you can at least figure out the file name. E.g. the definition of class Foo would be found in Foo.class.xx or something like that.

Thankfully, ack makes it easy to find the location files in a project. Just say, ack -g Foo.class and voila! it tells you to dig in modules/frob/model/include/Foo.class.xx or whatever abominable directory hierarchy it may be embedded in. So when you decide to kill that error at line 324 of class Foo, all you need to do is:
vim `ack -g Foo.class` +324

Unfortunately, typing this out is a bit tedious as well, so we bring in a little shell script (I call it vis, for vim on searched filename):


# Search and edit files. Use it like:
# $ vis [...]

if [ $# -lt 1 ]; then
exit 255;

vim `ack -g \\\b${fname}[^\/]*\$ | head -1` $@

This will invoke vim with the first filename that ack returns for the filename pattern (yes, it can be a Perl regex), and you can pass all other vim arguments as usual after the file name. Nifty, eh? The regex above will match file names starting with the given search pattern (with some caveats).

PS: It’s not too hard to make the script choose the editor dynamically, but people usually love to use one editor consistently. I use both Emacs and Vim, so I’m planning to make this script take the editor as an argument and have two convenient aliases, one each for vim and emacsclient which will handle passing the editor as an argument.

Beating the TripAdvisor Badge’s SEO Tactic

The Problem

Recently, I got a reference to this article from my Product Manager, regarding TripAdvisor badges and how they boost SEO. Their secret is a simple <a> link that links deep into TripAdvisor website and makes your site a donor of link love to TripAdvisor. This is somewhat bad for hotel owners who might find that TripAdvisor trumps the search ranking for their brand name.
Continue reading “Beating the TripAdvisor Badge’s SEO Tactic”

Simple PHP Timer

I was just trying to profile an HTML page for performance bottle-necks. I’m trying to follow a top-down approach, wherein I start from the entry script, and find the block of code that takes the biggest chunk of time before digging deeper into that chunk.

At this stage, it’s not feasible to drop in a full-blown profiling tool like xdebug because of the set-up overhead and amount of data it generates. So, I wrote a simple timing function that you can call at various points in your program to provide incremental and cumulative timing info in milliseconds. Continue reading “Simple PHP Timer”

PHP vs. Python for Web Apps

These days, more and more people ask the Python vs. PHP question when they start out with a web application from scratch. I’ve developed PHP applications for 5 years but for the last couple of years I’ve been doing Python. This post is meant to note some of my observations. If you don’t want to read the whole of it, my opinion — opinion — is to stick with PHP for dishing out your *ML. Use Python in the back-end, if you must. Continue reading “PHP vs. Python for Web Apps”

MySQLdb Leaks Memory

Whenever people search for a Python library for MySQL, they get directed to MySQL for Python. However, there are some nasties hidden in it. Searching for “mysql python memory leaks” results in a few links which suggest that using Unicode causes memory leaks with the library.

Today, however, I found another cause for MySQLdb memory leaks, while debugging a leaky Python daemon at work — database errors.

Use this script:

import MySQLdb
options =   {
   "user": "user",
   "passwd": "p455",
   "db": "somedb",
   "connect_timeout": 1

while True:
    except MySQLdb.Error:

Make sure MySQL is up and it’s possible to connect to it without failures using the above credentials. Then run this script and watch its memory usage. It’ll be rock steady for as long as you care to run it.

Now stop mysqld or do anything that would cause errors during connection and rerun the script. Watch it gobble up meg after meg as the errors continue. Any MySQL error, like unreachable server, missing database or tables, malformed query, etc. would trigger the leak.

I need to check if the recently released (13 days ago) MySQLdb 1.2.3 does better in this regard. MySQLdb 1.2.3 does fix the memory leak in this case.