Coding Range

On the Inner Heckler

October 21st, 2013

One of the only three podcasts I subscribe to is Writing Excuses. In summary, it’s a 15-20 minute weekly podcast all about about writing books. In episode 8.42 they discussed the ‘Inner Heckler’ versus the ‘Inner Editor.’

The ‘Inner Heckler’ is the little voice inside all of us that trashes the work we do and says it’s crap. It’s entirely destructive. The ‘Inner Editor’ is the little voice that picks apart what’s wrong and why, in order that we can improve it. It picks out the bad, but in a constructive manner.

The more I think about this, however, it seems as though children don’t have an inner heckler at all. Instead, they have an inner explorer, an adventurer that wants to know everything from “What’s around the corner?” to “Is this flame hot?” and “What happens if I put the kitten in the dishwasher?”. I’ve never heard a child negatively criticize their own work - it’s always the best work, regardless of actual quality, and will typically get stuck on the refrigerator door by the proud parents.

Given this, where does this heckler come from? At what point of growing up does a little destructive voice come in to play? And if the heckler is a foreign visitor, so to speak, is there any way to evict it?

Invoking Cygwin from Batch/Command Scripts

October 6th, 2013

Writing blog posts - or really anything requiring only basic text formatting - is really nice in Markdown. This blog (at least right now) is running on Octopress, which requires Ruby. Unfortunately, I couldn’t really get it working properly using the Windows build of Ruby.

On top of that, I deploy to a Linux host using Rsync, which doesn’t seem to have any equivalent or port in the Windows side, apart from Cygwin.

I couldn’t figure out how to get shell scripts (.sh files) associated with Cygwin properly and running in the correct folder, so the next-best solution I’ve come up with involves invoking Bash from a .cmd script:

@echo off
set CYGDIR=D:\tools\cygwin
set PATH=%PATH%;%CYGDIR%\bin

FOR /F "tokens=* delims=" %%A IN ('cygpath %~dp0') DO SET CURCYGDIR=%%A
bash --login -c "cd %CURCYGDIR% && do magic bash stuff here"

Almost surprisingly, it works. Also, the DOS/CMD equivalent of Bash’s MYVAR=$(run command here) is awfully ugly.

In Google We Trust 

September 10th, 2013

Great episode on (the lack of) digital privacy by ABC’s Four Corners.

Emit all the things!

September 8th, 2013

IL generation is something I thought was kinda cool, but like assembly language was too low-level to be able to read, never mind write. IL in particular seemed beyond the realms of readable, but after having a poke around a JIT branch of Steam4NET, it seemed like there wasn’t really much to it.

The best way to learn is by doing, in my experience, so today I took a few hours to try build a Steam Web API client with a hardcoded interface and a (mostly) dynamically generated implementation. The results of that can be found here.

If you have a basic grasp of reflection in .NET, the rest is actually quite simple. Reflection, or introspection, is the ability to ask the runtime information about what’s going on, and sometimes to change it. Things like:

  • What’s the name of the class I’m in?
  • Is this class a subclass of that class?
  • Is this unknown object a string?
  • Does MyFooClass have a method named “BarMethod”?
  • Set the instance variable (field in .NET) named “myvar” on this object to the value “avalue”.

And plenty more. Fortunately, creating the structure of methods in fairly easy as they use the same objects that .NET’s reflection APIs use. Calling methods from methods is also fairly easy, as calling a method requires a MethodInfo - another object used by ‘normal’ reflection. Below is halfway between a blog post and journal notes.

That said, it’s still pretty low-level. For example, in order to call a method one has to set up all the method arguments in the correct order, and then call the method. For example, the following constructor

public MyClass(string thing)
    : base(thing)
{
}

is about as simple as a constructor of a subclass can get. When dynamically generating it, that becomes:

TypeBuilder typeBuilder; // get this from somewhere
var constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.HasThis, new[] { typeof(string) });
var baseConstructor = typeof(BaseClass).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, new ParameterModifier[] { });
var il = constructorBuilder.GetILGenerator();

il.Emit(OpCodes.Ldarg_0); // this
il.Emit(OpCodes.Ldarg_1); // thing
il.Emit(OpCodes.Call, baseConstructor);
il.Emit(OpCodes.Ret);

So here the ‘OpCode’ ldarg loads an argument onto the evaluation stack. The first argument (zero-indexed) for an instance method has to be this based on the calling convention. Then the first argument is ldarg 1, the second is ldarg 2 and so on.

Once the evaluation stack contains all the arguments we need, we can call a method using the call OpCode. After the method is called, the return value of that method will be on the top of the evaluation stack to pass on to another method, or in this case, to return it and end the function.

Regarding local variables, there’s a list that has to be maintained by the person writing the opcodes. For the dictionary of API parameters, for example, there’s one local variable (the dictionary) so we store it in position 0 using the stloc opcode. Later, when passing the variable to another method, we can retrieve it using the ldloc opcode.

Some opcodes have seemingly shorthand codes. For example, il.Emit(OpCodes.Ldarg, 0) and il.Emit(OpCodes.Ldarg_0) seem to be identical.

When calling a method, scalar types such as integers have to be boxed. This can be done through il.Emit(OpCodes.Box, typeOfVariableHere) immediately after loading the variable onto the stack.

Constants can be loaded onto the stack using other ld* opcodes. For example, ldstr will load a string (e.g. il.Emit(OpCodes.Ldstr, "String Theory")).

The newobj opcode is the equivalent of the new keyword. Give it a constructor and it will create an object for you.

I’ve probably only just scratched the surface, but it’s good to be able to finally have some understanding of what IL opcodes mean - and it’s always cool to learn what’s going on at a much lower level than the happy world of abstraction.

Shame on you ALP

July 22nd, 2013

The Australian Government announced earlier this week a new shocking policy for asylum seekers who arrive by boat - the Government will ship asylum seekers off to Papua New Guinea instead of actually living up to their duty and responsibility to process these so-called “boat people” properly.

The reason asylum seekers is an election issue is that people are afraid of them. And the reason people are afraid of them is that both sides of politics have told us to be. And the reason they tell us to be afraid is so they can make it an election issue. Now, statistics would suggest that the amount of refugees coming in is such a tiny percentage we’ve got nothing to be afraid of, but most aussies couldn’t give two hoots about statistics. We’re more interested in the footy. - Adam Hills

They seem to have forgotten that foreigners coming by boat is a big enough part of Australian culture that it’s even made it’s way into our national anthem - “For those who’ve come across the sea / We’ve boundless plains to share”.

It’s in our history, too. Captain Cook, Captain Arthur Phillip, and the entire First Fleet arrived by boat without a visa.

Shame on you, ALP, for compromising our national identity for the sake of politics. Shame on you.

Why mobile web apps are slow 

July 10th, 2013

The part that the transcript doesn’t tell you is that the audience broke out into applause upon hearing this statement [deprecating Garbage Collection]. Okay, now this is really freaking weird. You mean to tell me that there’s a room full of developers applauding the return to the pre-garbage collection chaos? Just imagine the pin drop if Matz announced the deprecation of GC at RubyConf. And these guys are happy about it? Weirdos.

On iWork for iCloud

July 2nd, 2013

In The Menu Bar Episode 12, Andrew, Mark and Zac discused some of the significance of iWork for iCloud. It definitely deserves a lot more attention than it’s getting, but people seem to be more obsessed with colourful icons than productivity software.

However, the discussion was mostly about Apple vs Office (likely Office 365) and Apple vs Google Drive (formerly Docs). Regarding that, I think a big part is going to be higher education.

Students using Pages, Numbers and Keynote (mostly Keynote) need a Mac or iOS device on them if they want to do any editing, viewing or presenting. If you’re at a high-school or university library, or logged in to a Windows PC connected up to a projector, it can be a hassle to edit on your own device instead of the provided machines - especially if you’re trying to do research and there’s no WiFi.

With iWork for iCloud, I can log on to any computer with an internet connection and edit my iWork documents with a full-blown web-based editor which quite frankly puts Google Drive’s editor to shame. The one moment that I was amazed at during the iWork for iCloud reveal at the WWDC 2013 keynote was the fact that Keynote could do all it’s fancy presenations and transitions in a web browser. I no longer need to carry around my Macbook and a mini-DisplayPort (or if I had a newer Mac, Thunderbolt) to VGA converter, or buy a VGA converter for my iPhone or iPad, since I no longer need it to present anything from a personal device.

What Apple’s longer-term goals are for iWork for iCloud, I wouldn’t know. Being able to edit productivity documents from any internet-enabled computer in the world with a full-blown editor, however, is definitely a plus. In regards to Keynote especially, this blows both Skydrive/Office 365 and Google Drive out of the water.

The only thing iWork is now missing to be even with both Office and Google Drive in the consumer market is collaboration.

Creativity in programming 

June 20th, 2013

I think developers are more creative anyday if you’re doing these things with code.

Much more creative than coming up with funny hexadecimal codes that haven’t already been overused.

Alternate Reality Game puzzle design 

June 19th, 2013

Another person found what he thought was Valve’s current office address, and turned up at the front door hoping to track down that elusive phone line. But Valve had, just days earlier, moved to a new location - presenting him with nothing but a huge, empty void. Mind blown, he reported back to the internet that the entire company had disappeared, just like that…

CityRail are learning from Google

June 17th, 2013

Somebody at SMH several months ago wrote a rather ridiculous article on what would happen if, hypothetically, Google ran Opal, NSW’s new ticketing system (in beta) for public transport.

Now that it’s actually launched, I can see that took one idea from Google.

When I try use their other, more established services, they won’t shut up about the new one.