ColdFusion: StructCopy & Magic Structs

Quick post about a Coldfusion oddity I came across this week, whilst attempting to use CF's built in StructCopy function.

Coldfusion has two primary mechanisms to clone a Struct (that's a map to Java folk): StructCopy() and Duplicate().  StructCopy is a shallow copy, where as Duplicate is a full blown deep copy - so if you are attempting to clone a complex nested struct then duplicate is probably the function for you (although, beware, as you may expect it comes with some performance penalty!).


What I actually wanted to do was clone the URL scope (which is for all intents and purposes is just a Struct of key/value pairs of query string params in the URL).  I just wanted to clone the current URL scope struct of query params so I could alter the struct (add additional params etc) without actually affecting the URL scope (normal FP type stuff). As the URL scope is always just going to be a struct of String key/value pairs (Strings being immutable), I figured the shallow copy StructCopy function would do (the struct would always be a simple single level struct, and all key/values would be Strings - so any changes to them would not affect the original URL scope).


Oh no. It doesn't work.

To be fair to CF, the URL scope isn't a straight forward Struct - it is actually a Coldfusion URLScope object - but just masquerades as a Struct most of the time, writeDump()'ing it labels it as a Struct, passing it to a function that requires a Struct argument - no probs.

Here is some example code:

In the above scenario, after the structDelete - both Structs output as the same thing. The key "rob" has been removed from both Structs. Well actually, just the one. But actually the cloned struct isn't cloned at all, it's just the original URL scope again.


To me, that sucks. Really.

Like I said, I get that URL is not really a true struct, so I don't blame CF for not wanting to play nicely (although duplicate( url ) will work), but returning the URL structure? not cool.

There are a few options that I see can happen if CF doesn't want to StructCopy:
  • Throw an exception. To me, this is the best option. Everyone knows where we are, and really it is an exception - if we are saying truly, that StructCopy cannot copy a URLScope object, then its an exception.
  • Return an empty struct - not ideal, but again, forces handling of this potential - kind of StructCopy saying, look guys, I tried to copy but failed, so here's an empty struct.

In no circumstance is it cool to just silently return the URLScope object.  Appearing to be working correctly (you get a what looks like a struct back, with all the same keys/values as your original struct, so all good right?) but actually providing the exact opposite functionality you actually want. Dangerous.

I was lucky that the code I was writing flagged up the errors immediately in an obvious fashion - but if you are cloning just to avoid an unusual edge-case, you may be in for an unpleasant suprise.

Year of Code: Can it be that it was all so simple

I'm not sure if we should be pleased with the UK Governments Year of Code or not. Sure, it's a train wreck, it has been a shambles in term of organisation and PR - sometimes in a spectacular fashion, like when the well connected but not so well informed "Director of Coding" appeared on Newsnight - and undoubtedly from what we have seen, it will be poorly executed.


But on the plus the initiative is there. The government have recognised the importance of the tech industry to the country and its economy, which has got to be a good thing?

Every now and then the Government suprises us, with positive, forward thinking iniatives such as David Cameron embracing and pushing Silicon Roundabout (London's answer to Silicon valley) and spending time and energy on pushing the tech agenda for the city and country, or their design principle guidelines. And I think this opportunity has a lot of potential.

Did Lottie Dexter (the founder/director of Year of Code) make an ass of herself on Newsnight? Sure. But that is really just about her being poorly prepared/thought through. Were her comments about learning to code in a day worrying and possibly betraying a deeper lack of understanding of the task at hand? Sure. But all she needs is people around her who do understand these things - I have no problem at all with Lottie Dexter not knowing how to code - it's a classic pattern of non-tech CEO and tech co-founders that has been common place in Silicon Valley for years. It's great for her saying she wants to learn to code, and its undoubtedly good PR for the initiative, but that should be all it is - PR. She is not the CTO or Head of Engineering or anything else vaguely tech related. And the good news is she has got a lot of smart tech folk on board, who will have a deep understanding of tech and code, take a look at some of the board:


The list goes on. A lot of smart tech folks.

Really though, I would have liked to have seen more. I would have liked to seen the government take a leaf out of Obama's book and just hand it over to the startup ecosystem altogether - Obama completely handed over his election campaign tech to a bunch of great tech startup guys and left them to it, and it turned out pretty great (that's a long read, but well worth it). Really incentive-ise and provide the means to startup folks to really be creative with some ideas. If there is an industry that is just waiting to be disrupted it's got to be education - and whilst the government won't be opening up the whole education system for innovation and disruption any time soon, these changes to the tech curriculum are a real opportunity to start testing the waters with alternative and innovative approaches to education and inspiring children to become life-long learners.


On the whole, it's a positive step. The government has recognised that the tech curriculum is important and have an initiative with lots of smart folks on board.  The main concern I have is the tech curriculum and what happens with it - with all this emphasis on learning to code, and curiculums that get kids making e-cards. I have my doubts that coding or low level computing should be taught at primary school level. The best would probably be something like starting actual programming at comprehensive/secondary school level.  If you want a good grounding in tech, teaching kids at primary school to create e-cards is probably not the way to do it. Teaching basic logic and reasoning would, in my oppinion, be a much better grounding. And I'm not talking Formal Logic, I mean just thinking through puzzles, thinking rationally to work things out. It may sound odd, but I would love to see us start teaching kids from a young age about logic and problem solving through play.  Lego, Brio, etc. are great tools for learning to think logically, whilst also being creative

As a thought experiment, how about we give a bunch of primary school classes a Brio train set. Get kids in to teams, give them each a bunch of track and set the following objectives:
  • Make a train track that uses all the pieces (give them lots of pieces, including the various track switching pieces and merging/splitting pieces)
  • Design a track whereby every piece of track can be driven down in either direction without ever taking a train off the track (this is actually quite hard - I quite often find tracks I build with my son to end up with segments whereby once you take a particular junction you are destined to follow the same track route for ever)
  • Move the teams around and get the kids to try and find flaws in the design as per the point above.

One exercise doesn't make a curriculum, but it's an approach - it takes rational thinking, team working, problem solving and hopefully, is engaging and fun. Getting children enjoying learning and problem solving is a good grounding for going on into tech - after all, being able to design train tacks to a particular specification and being able to identify flaws in them is not that much different from designing code - understanding the use of track switches and considering the different implications/paths of multiple switches/junctions is exactly the same as being able to understand and think through conditional routing through code but without the syntax. I might be missing something on this "design an e-card" curriculum, but Brio is already sounding a whole lot better.

The re-think of the tech curriculum offers an ideal opportunity to shake up the way we think about education and take a step away from traditional test-focused, memorising of information approach and a step towards a more engaging approach that also helps teach children the joys of learning.




Observations on the WhatsApp Acquisition


Yesterday's acquisition of WhatsApp is a great example of a few trends throughout the industry:

1) The importance of product

WhatsApp currently has 450 million active users, double that of Twitter, and is still growing at an insane rate (which probably hurts even more given Twitter's recent growth struggles), why is that? Sure, in no small part it is because they are the first major company to really disrupt and take on the SMS/MMS monopoly - Why ever pay for text messages through your provider if you can send pictures/chats to anyone in the world for (essentially) nothing? - but their success in becoming that major player is their single focus on the core of their product, they wanted to provide mobile messaging - and thats what they focused on. As users grew they could have spun out with gaming/video chat/desktop clients/etc but they stayed focused on their core product and did it well.

It is telling that founder Jan had this note stuck on his desk:
No ads!
No games!
No gimmicks!

Also, choosing to monetise by annual subscription rather than in-app adverts showed dedication to the statements above - having used pricing as a way to throttle growth to a rate they could manage, they grew confident that the model would work and didn't need to detract from their product with  gimmicks or ads.

Whats more, by focusing on the simplicity and usability of their core product, it also meant they could focus on the technical stability of their platform -  I doubt if they had spun out they would have been able to maintain their impressive tech stats - 32 engineers, 450million active users, 99.9% uptime.  Twitter's "Fail Whale" was infamous during their period of growth as their platform couldn't scale to handle it, but with a more accelerated growth WhatsApp's simplicity has allowed them to maintain 99.9% uptime - failing to keep platform stability could have easily seen them lose their position in the market and not be signing the deal they have.


2) The importance of mobile

Mobile is the platform of growth for the future. We are, and will continue to see more and more mobile-first (if not mobile only) startups. Instagram, Snapchat, WhatsApp - the three biggest recent startup exits - all mobile focused.

And should we be suprised? Let's have a look:
  • On average, people use their phones 150 times a day
  • There are currently ~1 billion people using smart phones
  • But, there are still ~6 billion using feature phones - so huge room fro growth

Smartphone usage has exploded, other mobile computing is growing as traditional computing devices decline.  There is a huge market opportunity for new mobile users, and for many parts of the world, mobile is providing first experience of the internet.

What's more, the mobile eco-system is offering a new opportunity for startups to take on social incumbents - For a long time, Facebook has had the crown of social, and could not be tumbled by others efforts, primarily because it's where people are. It's where people's photos are. It's where people's social graphs are. Sure Facebook's design is kinda clunky now, and kinda ugly, with terrible ads all over the place, but who is going to try and convince their entire social graph to move?  Exactly.  But the mobile eco-system offers something different - your mobile device contains your contacts/address book, your photos, your videos - more and more, your mobile device is becoming your social platform that really holds your social graph.


Mobile is the future.

Raspberry Pi: Let's light up the (hello) world

There's a light, that shines, special for you, and me..


Soldering complete and Occidentalis installed, it is time to start learning how all this stuff works. Plus check that my soldering isn't so bad that it has destroyed my Cobbler.

From a quick bit of googling I started with the HelloWorld project:

Hello World: Python

So there are a tonne of hello world pythons tutorials and examples, so I won't dwell on this.
  1. from the shell (I setup wifi connection & then ssh on to the Pi so I can work from my normal desktop - I also installed vim - you can either install vim or use your preferred editor) run: 

        vim helloworld.py

    In the editor enter the following:
        print("Hello World")

    Exit and save the file (:wq)
  2. From the command line run
        sudo python helloworld.py
That's it. You will see the Hello World output in the shell. Yay.


Hello World with Lights On

So, the above was trivially easy and kinda pointless - but I did actually go through that, really just to make sure python was behaving and so I had a working baseline to go from.

So next I wanted to do a Hello World equivalent with hardware - the simplest thing to do seemed like a script that instead of printed hello world, just switched on a light briefly.

This assumes you have followed the same path as me and have opted for Occidentalis, and have soldered your Pi Cobbler (the basics are still really the same even if not using a cobbler, and are just breaking out your pi using a GPIO ribbon cable).



  1. If you opted not to install the Occidentalis OS (mod'd Wheezy OS packaged with stuff if you are planning on doing hardware stuff), then you will need to install some extra bits and pieces (you may already have them installed anyway, but running this won't do any harm - will just print the message saying its already there):
        sudo apt-get install python-dev
        sudo apt-get install python-pip
       
    sudo pip install RPi.GPIO

  2. Connect your Pi to the cobbler using the provided ribbon cable - plug the cobbler into your breadboard so it straddles the break down the middle (See photo below of the final setup of my breadboard for this experiment)
  3. Using a standard jumper lead, connect the ground on the cobbler to the -ve power rail on the breadboard (in this case mine is the purple-ish rail as you can see in the photo, the ground on the cobbler will be labelled GND). This is always best practice.
  4. Connect a resistor from the Pi Cobbler plug 23 to one of the free holes not linked to the cobbler (the resistor is needed to reduce the power - without one of these you will just blow your LEDs)
  5. Connect the +ve leg of the LED (the longer of the two legs) to the -ve power rail that we plugged the GND into, and the shorter LED leg into a hole in line with the resistor we plugged in (again, see photo below for final setup - although ignore the surplus resistor - that's not doing anything, I juts forgot to remove for the photo)



Now that the hardware is all connected, we need to update our simple helloworld script to control the light.  So again, run vim hellolightworld.py and enter the following script:


Now save and exit that script, then go to the shell and run python hellolightworld.py  and watch that little sucker flicker on briefly!









Raspberry Pi & Adafruit Cobbler: Getting Started

It's yours, the world in the palm of your hand..


As mentioned last time, I had to get my hands dirty and do some soldering. Having not really done soldering since my early teens, and even then , I can't really remember what or why I was soldering. I just remember having a consciousness of what soldering was and the experience.  Anyway, needless to say, I was pretty useless at it - not helped by the fact that I was using a soldering iron from the 70's that was undoubtedly designed to fix washing machines, and solder itself to match. The pins on the Cobbler were actually closer together than the diameter of the solder i started with, which made things pretty hard for precision soldering.

To give you an idea of the size of the cobbler, here is a photo pre-assembly:


I bought the Adafruit Pi Cobbler to make prototyping easier, and a lot of the tutorials/articles around the web suggested they were a good idea, and being an electronics rookie I dutifully did what I was told.
I also noticed when I was in Maplins recently that there is now a slightly cheaper version of the cobbler available, I don't know about its quality but in shape/size/design it looks pretty similar to the Adafruit cobbler. The advantage of this is you can just stroll into your local maplin and grab one for not much more than a fiver (that is five english pounds). The red one above is the non-branded one, the blue one is the Adafruit version.

After almost doing irreparable damage to the cobbler with my antique soldering iron and solder, I also resorted to a cheap hobbyist soldering iron and solder from Maplin. It cost less than a tenner (£10) and made a huge difference. Not drinking whisky whilst soldering on the second run possibly also made a difference as well, but that theory would need to be further tested.


All in all, it took me about two-three hours, over two evenings to get the thing soldered (there are 52 pins that need soldering), and considering I haven't soldered for probably 15-20 years (and that was as a young boy, so probably just soldering wires ends with my dad or something) it went reasonably well.


Lessons Learned

1. Don't use solder/soldering irons from the 70's. Washing machine repair evidently requires less precision that modern electronics
2. Don't drink whisky whilst soldering (not proven, but I definitely saw a decline in soldering quality on the first night).
3. Soldering irons get hot - touching them to test if they have cooled down/heated up is not advised.

Raspberry Pi: My Shopping List for getting started

As mentioned, I am completely new to electronics stuff really so I have been trying to get everything sorted and ready to get started. Coming from a Java/JVM dev background, I'm going to try and write up my experiences and things I think are different/difficult etc.

One of the first thing I did, having read a fair few tutorials/blogs on the topic, was work out all the components I needed/wanted to buy. Most of the components are really cheap, and I really didn't want to be caught short mid-development finding out I had to go out and buy something (a luxury that never happens with my usual development process - the most I have to do is go on the web and look for something).

Before I get started, here are the things I bought, which might be useful if planning on following the other posts.

They are all amazon affiliates links - which means if you buy via these links amazon gives me some cash, but won't cost you anything.