Coding Ninja

In words lies immortality

Export osTicket to Atlassian Jira Service Desk


Our old ticket system (osTicket) has served us well. However the time had come to move them onto Atlassian’s JIRA service desk. While JIRA seems to have many different import mechanism it didn’t have an option to import from osTicket.

Our version of osTicket didn’t have an export option (or at least I could not find it). JIRA does have the option to import via JSON format.

So I’ve decided to put this out there in the wild just in case that some else might need such a thing.

import MySQLdb as sql
import sys
import json

def get_comments(con, ticket_Id):
cur = con.cursor()
select * from
concat(title,' : ', note) as body
, as author
,DATE_FORMAT(n.created, '%%Y-%%m-%%dT%%T.000+0000') as created
ost_ticket_note n left outer join
ost_staff st on st.staff_id = n.staff_id
n.ticket_id = %s
union all
r.response as body
, as author
,DATE_FORMAT(r.created, '%%Y-%%m-%%dT%%T.000+0000') as created
ost_ticket_response r left outer join
ost_staff st on st.staff_id = r.staff_id
r.ticket_id = %s
) as sub
order by
sub.created asc
""", (ticket_Id, ticket_Id))

rows = cur.fetchall()
results = ''

for row in rows:
results += '{ "body" : ' + json.dumps(row[0].decode('latin1')) + ','
results += '"author" : "' + str(row[1]) + '",'
results += '"created" : "' + str(row[2]) + '"},'

return results[:-1]

def get_issues(con):
cur = con.cursor()
t.status as status
,p.priority as priority
,t.subject as summary
,m.message as description
, as reporter
,t.ticketID as externalId
, as assignee
,DATE_FORMAT(t.created, '%Y-%m-%dT%T.000+0000') as created
ost_ticket t left outer join
ost_ticket_priority p on p.priority_id = t.priority_id left outer join
ost_ticket_message m on m.ticket_id = t.ticket_id left outer join
ost_staff st on st.staff_id = t.staff_id
order by
t.created asc;""")

rows = cur.fetchall()
result = ""

for row in rows:
result += '{"status" : "' + row[0] + '",'
result += '"priority" : "' + row[1] + '",'
result += '"summary" : ' + json.dumps(row[2].decode('latin1')) + ','
result += '"description" : ' + json.dumps(row[3].decode('latin1')) + ','
result += '"reporter" : "' + row[4] + '",'
result += '"externalId" : "' + str(row[5]) + '",'
result += '"labels" : ["osTicket"],'
result += '"assignee" : "' + str(row[6]) + '",'
result += '"created" : "' + row[7] + '",'
result += '"comments" : [' + get_comments(con, row[5]) + ']},'

return result[:-1]

def start_json():
return """{ "projects": [{
"name": "Sample data",
"key": "SAM",
"issues": ["""

def export():
con = sql.connect("YOUR_DB_SERVER_IP", "DB_USER", "DB_PASSWORD", "DB_NAME")

with open('os_export.json','a') as f:
f.write(start_json() + get_issues(con) + """]}]}""")
print "fin!"
except sql.Error, e:
print "Oops I did it again.." + e.args[1]
if con:



Why Did Google Get Android Development So Wrong?


It is 2014 and the mobile is the new frontier, we are probably past the transition point of going from mainstream web to mainstream mobile in terms of usage and its a hopelessly messy affair for developers. If web development appeared untamed and wild and ugly back in the 90’s then mobile development is now a whole new level of ugliness.

Like the albatross that was Internet Explorer, Android today are the developer’s new albatross. Google has used its muscle to push its way to the top spot and now can claim to be King of the Hill. If you want to develop for the mobile market, it would be commercial suicide to ignore the Android market.

Android Development Sucks

Android Development Sucks

Android development to put it mildly is a pain in the rear side. Programmers do not usually develop for Android because they want too, like Internet Explorer it is because they HAVE too.

There are numerous blog’s already on the internet written by many a burned developers that have had to endure the punishment that is Android development. I don’t need to add to that hall of anguish.

Everything from installation, environment setup, stupid and over engineered SDK, device fragmentation, lack of consistent and current documentation has already been covered by many before me. I don’t want to cover those points in this blog post, instead I want to talk about a different way. What if I told you that Android development didn’t have to suck? what if I told you there was another way?

Another way, the Python way

What if you could just issue a few simple commands and have an entire environment set up for you?

sudo add-apt-repository ppa:kivy-team/kivy
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install python-kivy

What if instead of installing a mammoth IDE and configuring it you could just open a text editor and simply program it (in a beautiful language)?

import kivy
from kivy.uix.widget import Widget
from import App

class HomeScreen(Widget):

	def button_clicked(self):
		self.ids.home.counter += 1
		self.ids.label1.text = "you clicked " + str(self.ids.home.counter) + " times!"

class MyApp(App):

	def build(self):
		return HomeScreen()

if __name__ == '__main__':

What if when you wanted to build a GUI you could just use a beautiful DSL that makes sense and is clean?

		orientation: 'vertical'
		size: root.width, root.height
		id: home
		counter: 0
			id: label1
			text: 'Hello World!'

			id: button1
			text: 'click me!'
			on_press: root.button_clicked()

What if when you wanted to run the application you just ran it without needing some emulator?


What if when you wanted to deploy it you just connected your mobile and issued a few simple commands?

sudo apt-get install python-pip
sudo pip install buildozer
buildozer init
buildozer -v android debug deploy

What if for kicks you wanted to deploy to iOS?

buildozer -v ios deploy run

Well you don’t have to imagine, with Kivy and Python you can do this today. Doing things like accessing GPS is also just as effortless thanks to an high level API that does what all good API’s should do which is abstract away the pain.


Kivy is an amazing project for cross platform multi-touch GUI programming, Kivy is what I wanted mobile development to be like, simple straight forward and painless development and deployment. Although you can build some great apps this way and it may even be enough for cross platform development, it is still a hack.

So why did Google get mobile development so wrong? this comes to mind:

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.(Albert Einstein)

One day mobile development tools may be like Kivy but native and without all the hacks, and when that happens it will be the new King of Hill and not because of muscle, it will be because developers will flock to it.

7 Productive Tricks That Every Developer Should Know

Software development is really hard and wrestling with complexity is mentally taxing. In business time is money and deadlines add to the stress. Here are some productive tricks that every developer should know to give themselves a boost:

Developer Productivity Tricks

Productivity Tricks

#1 Testing:

When doing any testing, always do it in the live production environment! if it works which it will then you save a ton of time and hassle. Don’t worry about complaints from your end users, use these handy responses:

  • “It’s a user error, you are doing it all wrong”
  • “It works fine on my computer, the problem is clearly your machine”
  • “This is a known bug in your browser”
  • “It’s an extremely rare race condition, a mute German walrus has a better chance at telling a joke then this happening again”

#2 Database Tuning

Speed issues? its because you have too much data, stop being greedy and stop data hoarding. You don’t really need all this data! and besides you add new records every day! so use this handly one liner


voila! speed up is now OVER 9000% , don’t worry about your boss shaking his clenched fists in the air, that’s just a double fist pump for victory!

#3 Passwords

Security is hard, the weakest point of failure are always passwords. But coming up with and remembering passwords are a pain right?

Don’t worry, everyones knows that the most obvious password is “password123″. So here is a clever trick, use a double bluff by setting your password to “password123″, because everyones knows no one would
be stupid enough to use THAT password so you win!

#4 Source Control

Why use SVN or complicated tools like GIT? source control are for fools! all you really need is good ‘ol email. Its amazing and super secure and in the cloud (use trick #3 for setting your password to make it really secure)
Need to share your codebase? hit forward and BAM!, need to revert to an older version? check your sent folder and BOOYA!

#5 Code Reuse

We have all heard about the DRY (Don’t Repeat Yourself) principle, why waste your time retyping the same old same old huh? copypasta is your friend, “Ctrl+C, Ctrl+V” these are the wizards darkest spell. The more you use it the more code is reused! plus you can fine tune each one independently, Polymorphism baby!

#6 Backups

They say that if you don’t have 3 copies of your data then it does not exist. This is very sage advice indeed so take heed and make sure you store all 3 of them on the same machine, because when the time comes and you need your data the
last thing anyone wants to do is run around looking for backups duh! Your DevOps people will laugh and cry at you manically because of your genius forethought next time the database server blows up.

#7 Scaling

Donald Knuth said it best “Premature optimisation is the root of all evil” so don’t optimise that’s for idiots and losers besides life’s too short to be a human compiler. We all know that hardware gets faster every time you blink, why not let the hardware scale for you! not running fast enough? no problemo remember “mo’ memory, mo’ scale”. Instead of wasting time optimising use this time to play golf with your boss on his Yacht as they say “I don’t optimise, I socialse”.


So now that you know these amazing super productive tricks, do you have any more of your own? let the world know..

If Programming Languages Were Superheros


Programming languages all have their own distinctive style and odd character. Not surprisingly these unique set of traits tend to attract deviants that sometimes form a community who then hold conferences to talk about their deviant ways :)

It was a Friday afternoon and I started wondering if programming languages were superheroes who would they be?

Assembly (Hulk)

Assembly fights “close to the metal”, moving and shifting data around like no ones business. Like the Hulk its powers really has no limits. But it comes at a heavy cost, you have to do everything yourself. Power it seems is tied directly to its emotional state. There are no safety nets, the world is not made of rainbows and kittens. Assembly is a lone gun and will carry that heavy burden all day long. But be very careful and don’t make assembly angry, because when assembly gets angry it will scream “ASSEMBLY CRAAASH” and destroy your computer.

Python (Batman)

Saving a City from criminals is not easy, solving crimes created by evil geniuses requires a touch of elegance and sophistication. With a vast library just an import away, its not so much programming as merely expressing your will. Let python worry about the details for you. But Python has a dark past too, its real power can never be know, it’s character can never be made public. It hides itself as “just another scripting language, running on an VM interpreter hybrid wasting expensive CPU cycles..”

PHP (Joker)

Some minds are so twisted, so mangled they were never meant to be understood. Peering into PHP code, is looking directly into the abyss. But once you stare into the darkness that is PHP, the darkness stares back at you. Some say mixing all concerns (view, logic, mode) all in one place is madness, but maybe PHP is just ahead of the curve?

C++ (Robocop)

C in its original form, was a normal hardworking decent language. But a fateful accident with OOP and desire to make it faster, stronger, and harder resulted in C++. Yes C++ is super shiny but it also created things like protected abstract virtual base. If you can master this shiny machine then amazing power is yours to be commanded. However most just use the hardworking C side of C++.

Ruby (Ironman)

Ruby is advanced, its neurokinetic nanoparticle morphological language is were its power lies. Some say the original Ruby had humble beginnings, made in a cave by single man called Matz with nothing but simple tools. While it combines the best ideas from other languages it ends up just monkey patching it all together. Lately people say Ruby has become shallow and just a big “front”, this is sad because the new generation of kids conflate Ruby with the framework “Ruby On Rails”. The real question on everyones mind is can Ruby stand on its own without its web framework?

Java (Magneto)

An old language, Java was born out from an age of suffering during the jurassic era of C and C++. Its a very verbose and heavy language, but the over engineering is by design (a design that only a committee of bowel movement architects could appreciate). If you want to lift heavy metal, steel or a suspension bridge Java will not let you down. Of course the downside is if you want to lift small light plastic things, Java will be of no use. Java believes that native languages like C and C++ are a disease and that managed languages are the next evolution. Java believes in a final war between native and managed languages as an inevitable clash.

Lisp (Professor Xavier)
Professor Xavier

Is code data or is data code? some say “it’s all in your RAM”. And as for language, do you really need syntax? When you look deep enough you will find that all languages are connected and can be expressed by an AST. Lisp’s simplicity and metacircular evaluator is nothing short of pure genius that is perhaps only comprehensible on another metaphysical plane of existence. Lisp wishes to promote the peaceful message that all data and code can co-exist, it stands in a neutral place where it believes it can create harmony by virtue of homoiconicity. Sadly not everyone understands or appreciates Lisp’s high virtues and instead run away in fear.

So what do you think? if your programming language was a superhero which character would it be?

The Joys Of Migrating From SQL Server To MariaDB


In my professional line of work I live within the full Microsoft stack. While there is nothing inherently wrong with this, the world is full of other stacks. I have to admit that Microsoft’s development tooling is top notch, its easy to get spoilt in that environment. However when it comes to pricing, freedom and platform independence then Microsoft is perhaps not the best choice.

Outside of work I do freelance development for a few customers. Freelancing give me the opportunities to use whatever stack I want. I’ve used a variety of languages on different jobs, whatever best suited the problem, everything from python to php.

Data Migration

One particular system I built was full on Microsoft stack, C# WinForm front end with your basic SQL Server Express backend store. Over the years the application has evolved, and recently the decision was made to convert it into a web application.

I didn’t think twice and started work on a front end, gutting out the old application and creating light weight web services.

The requirements had shifted for the application from providing for a single company, to potentially multiple companies. In terms of hosting, licensing and data growth etc staying with Microsoft was going to be expensive in the long term.

Part way through I made the decision that I needed to switch stacks. After doing a lot of research, I finally settled on using old school JSP and Java Servlets for the middle, and MariaDB for the back end store.

The front end was trivial to move over because it was just html css and js, the middle logic was just a matter of porting C# to Java. The only real challenge was going to be the database because of my heavy use of Stored Procedures.

I love stored procedures, I’m a die hard fan of them. However when it comes to migration this love can soon turn into hatred. I knew this part would be problematic ahead of time, so I decided that while porting I’d move the logic into the application layer away from the datastore so that in future the datastore can be vendor agnostic.

Stored procedures aside, the schema and data migration was in theory supposed to be the “simple” part.

Oh how wrong I was! I used Oracles Workbench data migration wizard to do the heavy lifting. Everything would have been a complete breeze, if if wasn’t for one small annoying problem: GUIDs!

Yes along with stored procedures, I also love GUIDS. In SQL Server GUIDs when used correctly work amazingly well. MariaDB doesn’t support them natively, so SQL Workbench simply imports these columns as VARCHAR(64).

In SQL Server if you use GUIDS as primary keys you can use the “NEWSEQUENTIALID()” default which creates sequential GUIDS which dramatically reduces index fragmentation. This is somewhat similar to UUID Version 1 in the MariaDB world.

After some digging around on the various forums and stackoverflow, the best choice for storing GUIDs in MySQL seemed to be BINARY(16). The only caveat was a bit of hexing and unhexing that was needed between storing and retrieving them.

I poked around SQL Workbench’s wizard to see if I could create a custom mapping, but alas it only allowed very simple data type changes. This was turning out to be an exercise in frustration :(

Python to the rescue!

It become clear that some manual intervention would be required to fix these GUID columns. Of course I could have knuckled down and gone through each column and converted them by hand. This naturally is not the way of the programmer! why do something manual when it can be automated?

The idea was dead simple use python to get the database schema, based on this meta generate the sql to do the actual transform. A little bit of debugging later I had a working sql script generator. Running the script against the newly migrated database ran within seconds and fixed up all the columns into BINARY(16) and created the primary keys when needed.

If you have find yourself in need pf cleaning up MSSQL GUIDs after a migration to MySQL or MariaDB then I’ve put my script up on Github:

import MySQLdb
# license
MIT = """
-- The MIT License (MIT)
-- Copyright (c) [YEAR] [COPYRIGHT_HOLDER]
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
-- THE SOFTWARE.""".replace("[YEAR]","2014").replace("[COPYRIGHT_HOLDER]","")
# configuration
DB_HOST = "your_host"
DB_USER = "your_user"
DB_PASS = "your_password"
DB_NAME = "your_db_name"
# define a row class
class rx(object):
	def __init__(self, table, col, isKey):
		self.table 	= table
		self.col 	= col
		self.isKey 	= isKey
# schema meta
def get_meta_data():
	db = MySQLdb.connect(host  	= DB_HOST
						,user 	= DB_USER
						,passwd	= DB_PASS
						,db 	= DB_NAME)
	cur = db.cursor()
	cur.execute("select table_name, column_name, column_key from information_schema.columns where table_schema = '"+DB_NAME+"' and data_type = 'varchar' and character_maximum_length= 64")
	meta = []
	for row in cur.fetchall():
		meta.append(rx(row[0],row[1], row[2]))
	return meta
# sql script generation
data  = get_meta_data()

dsql  = "\n"
dsql += "-- ;===============================================\n"
dsql += "-- ; auto transform mssql guid to mysql bin(16)	\n"
dsql += "-- ;===============================================\n"
dsql += MIT

dsql += "\nUSE "+DB_NAME+";\n"

dsql += "\n"
dsql += "-- ;===============================================\n"
dsql += "-- ; mirror old columns							\n"
dsql += "-- ;===============================================\n"

for item in data:
	if item.isKey == "PRI":
		dsql += "ALTER TABLE `[TABLE]` ADD COLUMN `[COL]_x` BINARY(16) FIRST;\n".replace("[TABLE]", item.table).replace("[COL]", item.col)
		dsql += "ALTER TABLE `[TABLE]` ADD COLUMN `[COL]_x` BINARY(16) AFTER `[COL]`;\n".replace("[TABLE]", item.table).replace("[COL]", item.col)	

dsql += "\n"
dsql += "-- ;===============================================\n"
dsql += "-- ; transfer data across							\n"
dsql += "-- ;===============================================\n"

idx  = 0;
current_table = ""
move_col = "`[COL]_x` = UNHEX(REPLACE(`[COL]`,'-',''))"

for item in data:
	if current_table == item.table:
		dsql+= ","+move_col.replace("[COL]", item.col)
		current_table = item.table
		dsql+= (";" if idx > 1 else "")+"\nUPDATE `[TABLE]` SET ".replace("[TABLE]", item.table)+move_col.replace("[COL]", item.col)

dsql +=";"

dsql += "\n"
dsql += "-- ;===============================================\n"
dsql += "-- ; kill old columns								\n"
dsql += "-- ;===============================================\n"

for item in data:
	dsql += "ALTER TABLE `[TABLE]` DROP `[COL]`;\n".replace("[TABLE]", item.table).replace("[COL]", item.col)

dsql += "\n"
dsql += "-- ;===============================================\n"
dsql += "-- ; reset to orginal column name					\n"
dsql += "-- ;===============================================\n"

for item in data:
	dsql += "ALTER TABLE `[TABLE]` CHANGE `[COL]_x` `[COL]` BINARY(16);\n".replace("[TABLE]", item.table).replace("[COL]", item.col)

dsql += "\n"
dsql += "-- ;===============================================\n"
dsql += "-- ; reset new primary keys						\n"
dsql += "-- ;===============================================\n"

for item in data:
	if item.isKey == "PRI":
		dsql += "ALTER TABLE `[TABLE]` ADD PRIMARY KEY(`[COL]`);\n".replace("[TABLE]", item.table).replace("[COL]", item.col)

print dsql


Migrating databases in most cases will involve some amount of pain, automation tools are your friend but don’t believe the marketing hype. The tools will only get you most of the way. But to finish the job use the force of a scripting language to slay any nasty database dragons!

If vs Switch Performance, To Switch Or Not To Switch


While most people would generally consider spending vast amounts of energy arguing over minute details bizarre; for programmers this kind of weirdness with a taint of madness comes with the territory.

If vs Switch Performance

Recently a nerd squabble came up and it was centered around the use of an “if” statement versus a “switch” statement. I found myself intrigued and curious enough that I decided to take a more scientific approach and put real numbers instead of just nerd speculation.

The Pattern

A common pattern in programming is to return a value when presented with another associated value. In this particular case an additional condition was to return a default value when no match existed.

Clearly given the above description the text book answer most probably would be to use a switch statement. Normally I would also use a switch statement given the above pattern. However in our particular case there was only three associated values (with the first one being the default as well).

Me being the coding rebel, the white space bureaucrat and line break conservative I opted in this situation to use two simple inlined “if” statements. The two coding styles are shown below:

Classical Switch

string department;

switch (departmentCode)
	case "FD":
		department = "Finance Department";
	case "HR":
		department = "Human Resource";
	case "IO":
		department = "Information Office";

Rebellious If

string department = "Information Office";
if (departmentCode == "FD") { department = "Finance Department";   }
if (departmentCode == "HR") { department = "Human Resource";       }

Switch Visual “Flow”

switch visual flow

If Visual “Flow”

if visual flow

Such rebellious coding style did not go down well with the fellow programmers and predictably began the squabble. I prefered my use of inlined “if” statements for no other reason than aesthetics, I assumed that given the context the performance differences would be entirely negligible.

A Scientific Approach

Later in the evening I started wondering about the two cases and what the actual performance between these where. Looking at some stack overflow questions it appeared that the general view seemed to be that switch statement yielded better performances. However as with all things it really depended on context. In some cases the “if” statement was more performant. This got me even more curious. With compiler optimization added to the mix the question became even less clear cut.

I decided to do some actual benchmarks and see what the numbers looked like. At least this would be more objective than just outright speculation about what “might” happen.

Reading about the “if” versus “switch” performances, I found that after a certain point the compiler would optimize a switch statement by using a “jump table”. So I decided to also test an expanded case from our own specific case.

Results 1 (Short Case)

A simple benchmark program was created and the two styles run for 10 million cycles for each code path. The entire test was then repeated for a total of 7 trials. All tests where compiled with the “optimized” checkbox turned on. The final averaged results are shown below:

Results 1

The disassembled IL code produced by the “if” style is shown below:

IL_0000: ldstr "Information Office"
IL_0005: stloc.0 // department
IL_0006: ldarg.1
IL_0007: ldstr "FD"
IL_000C: call System.String.op_Equality
IL_0011: brfalse.s IL_0019
IL_0013: ldstr "Finance Department"
IL_0018: stloc.0 // department
IL_0019: ldarg.1
IL_001A: ldstr "HR"
IL_001F: call System.String.op_Equality
IL_0024: brfalse.s IL_002C
IL_0026: ldstr "Human Resource"
IL_002B: stloc.0 // department
IL_002C: ldloc.0 // department
IL_002D: call System.Console.WriteLine
IL_0032: ret

The disassembled IL code produced by the “switch” style is shown below:

IL_0000: ldarg.1
IL_0001: dup
IL_0002: stloc.1 // CS$0$0000
IL_0003: brfalse.s IL_003E
IL_0005: ldloc.1 // CS$0$0000
IL_0006: ldstr "FD"
IL_000B: call System.String.op_Equality
IL_0010: brtrue.s IL_002E
IL_0012: ldloc.1 // CS$0$0000
IL_0013: ldstr "HR"
IL_0018: call System.String.op_Equality
IL_001D: brtrue.s IL_0036
IL_001F: ldloc.1 // CS$0$0000
IL_0020: ldstr "IO"
IL_0025: call System.String.op_Equality
IL_002A: brtrue.s IL_003E
IL_002C: br.s IL_003E
IL_002E: ldstr "Finance Department"
IL_0033: stloc.0 // department
IL_0034: br.s IL_0044
IL_0036: ldstr "Human Resource"
IL_003B: stloc.0 // department
IL_003C: br.s IL_0044
IL_003E: ldstr "Information Office"
IL_0043: stloc.0 // department
IL_0044: ldloc.0 // department
IL_0045: call System.Console.WriteLine
IL_004A: ret

The switch produces a slightly longer IL code.

Results 2 (Extended Case)

The simple benchmark program was extended and the two new styles run for 10 million cycles for each code path.

Results 2

The disassembled IL code produced by the “if” style is shown below:

IL_0000: ldstr "Information Office"
IL_0005: stloc.0 // department
IL_0006: ldarg.1
IL_0007: ldstr "FD"
IL_000C: call System.String.op_Equality
IL_0011: brfalse.s IL_0019
IL_0013: ldstr "Finance Department"
IL_0018: stloc.0 // department
IL_0019: ldarg.1
IL_001A: ldstr "HR"
IL_001F: call System.String.op_Equality
IL_0024: brfalse.s IL_002C
IL_0026: ldstr "Human Resource"
IL_002B: stloc.0 // department
IL_002C: ldarg.1
IL_002D: ldstr "MI"
IL_0032: call System.String.op_Equality
IL_0037: brfalse.s IL_003F
IL_0039: ldstr "Mangement Information"
IL_003E: stloc.0 // department
IL_003F: ldarg.1
IL_0040: ldstr "SO"
IL_0045: call System.String.op_Equality
IL_004A: brfalse.s IL_0052
IL_004C: ldstr "Security Officer"
IL_0051: stloc.0 // department
IL_0052: ldarg.1
IL_0053: ldstr "HO"
IL_0058: call System.String.op_Equality
IL_005D: brfalse.s IL_0065
IL_005F: ldstr "Head Office"
IL_0064: stloc.0 // department
IL_0065: ldloc.0 // department
IL_0066: call System.Console.WriteLine
IL_006B: ret

The IL code produced is an extenstion of the shorter version.

The disassembled IL code produced by the “switch” style is shown below:

IL_0000: ldarg.1
IL_0001: dup
IL_0002: stloc.1 // CS$0$0000
IL_0003: brfalse IL_00BF
IL_0008: volatile.
IL_000A: ldsfld {6D2970D2-35A8-4EC1-AA32-6B4B8F1B6F55}.$$method0x6000002-1
IL_000F: brtrue.s IL_0066
IL_0011: ldc.i4.6
IL_0012: newobj System.Collections.Generic.Dictionary..ctor
IL_0017: dup
IL_0018: ldstr "FD"
IL_001D: ldc.i4.0
IL_001E: call System.Collections.Generic.Dictionary.Add
IL_0023: dup
IL_0024: ldstr "HR"
IL_0029: ldc.i4.1
IL_002A: call System.Collections.Generic.Dictionary.Add
IL_002F: dup
IL_0030: ldstr "MI"
IL_0035: ldc.i4.2
IL_0036: call System.Collections.Generic.Dictionary.Add
IL_003B: dup
IL_003C: ldstr "SO"
IL_0041: ldc.i4.3
IL_0042: call System.Collections.Generic.Dictionary.Add
IL_0047: dup
IL_0048: ldstr "HO"
IL_004D: ldc.i4.4
IL_004E: call System.Collections.Generic.Dictionary.Add
IL_0053: dup
IL_0054: ldstr "IO"
IL_0059: ldc.i4.5
IL_005A: call System.Collections.Generic.Dictionary.Add
IL_005F: volatile.
IL_0061: stsfld {6D2970D2-35A8-4EC1-AA32-6B4B8F1B6F55}.$$method0x6000002-1
IL_0066: volatile.
IL_0068: ldsfld {6D2970D2-35A8-4EC1-AA32-6B4B8F1B6F55}.$$method0x6000002-1
IL_006D: ldloc.1 // CS$0$0000
IL_006E: ldloca.s 02 // CS$0$0001
IL_0070: call System.Collections.Generic.Dictionary.TryGetValue
IL_0075: brfalse.s IL_00BF
IL_0077: ldloc.2 // CS$0$0001
IL_0078: switch (IL_0097, IL_009F, IL_00A7, IL_00AF, IL_00B7, IL_00BF)
IL_0095: br.s IL_00BF
IL_0097: ldstr "Finance Department"
IL_009C: stloc.0 // department
IL_009D: br.s IL_00C5
IL_009F: ldstr "Human Resource"
IL_00A4: stloc.0 // department
IL_00A5: br.s IL_00C5
IL_00A7: ldstr "Mangement Information"
IL_00AC: stloc.0 // department
IL_00AD: br.s IL_00C5
IL_00AF: ldstr "Security Officer"
IL_00B4: stloc.0 // department
IL_00B5: br.s IL_00C5
IL_00B7: ldstr "Head Office"
IL_00BC: stloc.0 // department
IL_00BD: br.s IL_00C5
IL_00BF: ldstr "Information Office"
IL_00C4: stloc.0 // department
IL_00C5: ldloc.0 // department
IL_00C6: call System.Console.WriteLine
IL_00CB: ret

This longer swith case produces much more IL code, the compiler optimization is done using a .NET dictionary<string, int>.


Ok so what can we conclude from the raw results? we can certainly say that for this particular pattern the data indicates that the “if” statement is indeed faster and produces smaller IL code.

Does this mean it is conclusive? No, because in most practical real world cases such extreme number of cycles will not typically be used. The performance difference will always be practically zero.

Some may argue that in the above case that the “if” suffers from “bad practice” because it instantiates a string variable and in all but the default case overrides its straight away and thus “wasteful”. This line of argument much like the performance red herring in reality makes no difference because a very large number of objects would have to be created for any real performance degradation.

What it ultimately boils down to is programmer preference, arguments based on performance are irrelevant since they are a massive premature optimization that for 99% of the time do not even apply.

“Beautiful code, is in the eye of the beholder”

Until next time beautiful coding!

Quick and Dirty Eval for C#


“Eval is evil” is a well known phrase and with good reason. Allowing arbitrary code execution opens the doors for [hacker stereotype] to pwn your beautiful application.

Quick and Dirty Eval for C#

Evil Programmers

In many situations the need to reach out to eval is not even needed, its mostly a lazy way out. The price of eval is very high, you pay in terms of speed and security as well as increased debugging complexity.

“eval is Evil: The eval function is the most misused feature of JavaScript. Avoid it”

 Douglas Crockford in JavaScript: The Good Parts

Of course JavaScript is not the only language to have eval, PHP and many other languages have the ability to evaluate code from strings. The evil uses aside, the ability to evaluate code on the “fly” does open up some cool possibilities such as meta programing.

When it comes to C# however the eval function doesn’t exist (probably a good thing). However I recently needed to visit the dark side, most examples of C# eval are quite verbose, but I managed to put together a fat free version. Its quick and its dirty and you probably shouldn’t use it but in times of desperation…

Quick and Dirty C# eval in 8 lines:

    private static object Eval(string __code)
        var csc =   new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        var p   =   new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, null, true);
        p.GenerateInMemory = true; p.GenerateExecutable = false;
        CompilerResults r = csc.CompileAssemblyFromSource(p, "using System; class p {public static object c(){"+__code+"}}");
        if (r.Errors.Count > 0) { r.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText)); return null; }
        System.Reflection.Assembly  a = r.CompiledAssembly;
        MethodInfo                  o = a.CreateInstance("p").GetType().GetMethod("c");
        return                      o.Invoke(o, null);

No We Are Not Dull Weirdos But Willard Foxton is a Troll

After reading Willard Foxton’s Blog piece entitled “The Government wants to teach all children how to code. Here’s why it’s a stupid idea” I had to stop myself and think rationally; naturally the post was full of factual inaccuracies and a negative portrait (conjured up entirely in the imaginations of Willard’s mind) of a large section of the developer community.

Ironically I agree with the title. This is not a new debate and it’s one that I have posted about before as a response to Jeff Attwood “Please Don’t Learn To Code But Give it a Try”.  Sadly after the title, there is very little to agree with. With all the ad-hominem grenades tossed at developers the instinctive reaction would be to hurl abuse right back at him. However that would not be a dignified response. Instead after calm reflection I concluded that a more subtle and straight headed response would be more suited.

Troll Dad

Troll Dad

While everyone is entitled to an opinion, that doesn’t entirely mean it has any real value. When you speak a view in public to a large audience, those words do have an impact and so a certain level of responsibility and liability has to be taken on if you choose to speak on a public platform.

Willard’s words are inflammatory, derogatory and demeaning. Judging from the comments he has certainly got a reaction. I don’t need to reiterate and correct the many inaccuracies as this has been covered in the comments. Naturally some comments have been just as negative towards the author, but that’s hardly surprising. If you go around looking for a fight don’t be amazed when you get hit.

It’s a terrible shame that at the heart of the post and the central point has been completely side lined. Was it really necessary to write such a loaded and baited post? Was the traffic really worth it?


It’s really sad to see someone would exploit a genuine debate for purely selfish interests. In a strange way I can’t fully blame Willard, I assume if he didn’t do this someone else would have. But these kinds of post don’t help the overall conversation, as they are a distraction away from the main discussions.

Ultimately the best thing to do would be to ignore him. He is after all a troll as demonstrated by his post.

“Don’t feed the trolls” -anon

Using Your Smartphone as a Desktop Replacement

After smashing my smartphone screen (a Sony Xperia S), I got a quote for a repair because foolishly I hadn’t insured it. After hearing the quote I politely said no and hung up.

An alternative option would have been to try and repair the screen myself, however I didn’t want to take the risk of further damage, besides life is too short for that kind of hassle.

Since the phone was still functional, bar the cracked screen I decided to carry on using it until I a replacement smartphone was available.

After a long review I settled on a new smartphone which I plan on getting in the next month. This means however I will have an extra functional yet damaged phone (yes I know, 1st world problems). I started thinking about extra uses for this spare phone as it would be a shame to waste good working tech.

A few months back one of my old PC’s blew up (a dead motherboard), and I have not bothered to replace it. It was mainly used for surfing. With access to lots of web enabled devices in our house the priority for replacing it was low. With the whole “Ubuntu Edge” buzz going around I thought it would be interesting to see just how much a current smartphone could replace a desktop.

I’ve used a laptop setup as my main working environment for a while in the past, its harder to go back to a big bulky desktop setup after a while. The thought of using something even more space saving was very nice.

The idea was to use an OTG (on the go) cable to a USB hub and then connect my keyboard and mouse that way. The old PC had a standard VGA monitor, so this meant I needed to get a HDMI to VGA adapter.

Sony xperia s. using OTG cable and USB hub to connect Keyboard and mouse

Sony xperia s. using OTG cable and USB hub to connect Keyboard and mouse

I ordered the relevant cables and adapters from eBay. A few days later when they arrived the problems started. The first issue I ran into was that the phone could not be charged and use USB host at the same time. It seems that the standard practice on handsets are to combine USB host and charging into one.

The second problem I ran into was getting the HDMI to VGA to work. I proved that the converter worked when using two other Android devices (Tablet and an Android Gamepad). However for some reason it simply did not work with my handset. I managed to find a hack to a hidden service menu which allowed me to change the HDMI resolution and frequency (dial *#*#7378423#*#*), but none of the options worked. In the end the adapter fell apart in my hand due to the low quality part of that cheap product.

I decided to go ahead and try and operate the device without the benefit of an external screen. At least I had a working keyboard and mouse. The next series of issues were really related more towards software rather than hardware limitations.

Getting android to recognise a UK English keyboard was tricky. I used an app which seemed to work (except the “@” and ‘“’ keys were mixed around).

Android has three hardware keys (back, home, menu) although the “back” and “menu” mapped to the “escape” and “right menu” key. I never found any key that mapped to the “home” key!

I decided to carry on, tweeting was a breeze. Sending text messages was really nice. Doing basic surfing was cool. I found that even the “alt + tab” function worked as expected. Then I decided to try do some work and write this blog post.

I tried using Google Docs, but Google Docs would not give me the “desktop version” and forced me to use a mobile version, which frankly is useless. I tried using the “request desktop” feature but that didn’t work either.

Frustrated I went ahead and installed “Kingsoft Office” which is an amazing alternative to MS Office for the desktop (cross platform and free). The Android version however after using it, while workable was slow and didn’t support basic keyboard short-cuts. At one point it crashed my smartphone.

In the end I caved in having only written 50% of the post and I had to go back to my desktop top to finish this post.

I can still see the potential however at this point until a full-blown desktop OS is implemented inside a phone (Ubuntu Edge Style) this setup is not yet ready for prime-time.

I think the following tweet summed up the entire experience perfectly:

The tweet pretty much sums it up

The tweet pretty much sums it up


There is great potential behind the convergent concept, I genuinely believe we are moving towards it. Hardware is “good enough” for most tasks. What is lacking is the software. Ubuntu Edge really has the opportunity to change this. If wireless charging, video, keyboard and mouse could be implemented this would be my dream setup. Until then it is still just a dream. For now I’m running and screaming back to my laptop, there is still life in that old dog yet!

Until next time, keep it lean mean and green.

Time To Junk Your Junk Code

With summer in full swing it becomes harder to sit crammed behind a computer wresting with code for the best part of the day. Having some time off from work has given me the opportunity to start on my growing list of “things to do”.

I’m a bit of a tech junkie, and as such have lots of bits and bobs lying around. One item on my “to do” list was to sort this stuff out. I’ve never considered myself a hoarder, however as I pulled out those dusty old boxes of treasures I started reminiscing, ah an old school stick of ram, an old AOL dial up installer, my old embarrassing VB6 book, OK ignore the last one I’m going to burn it.

Looking at most of this junk, I have no idea why one earth I’ve kept onto them this long, I know most of the time its probably because it was out of sight (and so out of “mind”). Sometimes it was out of nostalgia for example a decade old CD photo slideshow that I had made and was so proud of. I hoarded some things like my ps2 to usb adapter because of that feeling that I “might just need it”.

A pile of junk

A pile of junk

The reality of course is that it is just junk, it has no real value and it is of no real use anymore. It just sits there taking up space, valuable space that I could use for other things.

I’ve thrown away three old PC’s, and planning on turning my current smartphone as a desktop replacement for one of the PC that blew up. It was only ever used for surfing anyway. The thought of using such a light weight and space saving approach makes me feel that my current workstation is such a dinosaur. I mean 2013 and we are still using wires and cables?

After a day of of cleaning up junk from my house, I got this amazing feeling of freedom. Like I just removed this massive ball and chain from my foot which I had been dragging along all this time. Things are much more cleaner and organised, only that which was actually useful has been kept (only a few items).

Going back to programming I happened to look at some old corners of one of my codebases. When I read through it, I got the exact same feeling as I did with my real world junk, a lot of it was useless, but remained because of the failed “I might just need it” reasoning. Other than nostalgia it was just dead code, very little of it was working code.

I sprang into action, killing the code with fire like a zombie slayer, taking delight at watching text disappear into the ether forever, I did a few rounds of refactoring and boy did that feel good! It was like finally scratching that annoying itch.


If you get some down time, go looking through your codebase especially the corners you have not visited in a long time, if you see any junk code, well just junk it because it feels damn good :)

Until next time, kill some junk code with fire!