Jasmine!

Posted on

image

Do you know how hard it is to find non xxx fan fic pictures of Princess Jasmine on Google Image Search?

I'm converting some of my C++ library code into CoffeeScript. Like everyone else I've a set of 3d maths bits n pieces in the form of helper classes so I thought'd I'd get the across first.

Now I mentioned in the previous post was begining to dawn on me how useful test suites would be for dynamic languages but it was something I hadn't had much exposure to. Well that's changed over the last few days and after a bit of research I've had a go with Jasmine that has a node based implementation.

And yeah, it's been really good. The tests are kind of easy to implement and it exposed quite a few minor things I'd missed. Minor things that could have easily propogated out to be major things a long way away from the actually code if the project I'm toiling on got much bigger.

Here's one of the tests for Vec3 that makes sure if you create a Vec3 with no args it returns a zero vector.

  
# Vec3 Tests
describe 'Vec3', ->

    it 'can make a Vec3, no args, be 0,0,0', ->
        got = new Vec3().getArray()
        expected = [0,0,0]

        for v,i in expected
            expect( v ).toBeCloseTo got[i], 10

This one used to fail and in the game code it did so silently.

And now it's all wrapped up nicely in a build task that gets run if any project file changes. Growl even moans at me when it fails.

Test Driven Development seems to be a pretty hot topic recently and I can see how it'd be a pretty cool approach. Start with your tests and then fill out the code until it all passes. I'd be interested in doing something like this for my C++. Anyone got any reccomendations? The hard thing about taking a new approach usually isn't the work to do it but the dead ends you have to go down finding a good path. I've heard googletest is supposed to be pretty good. Any opinions?

References

previous post jasmine TDD googletest

So what did I learn today?

Posted on

Arbroath is further away than it looks on the map

Managed to clock > 50km in a day cycling (Ride 1, Ride 2 and Ride 3 ) for the first time today. A small 5km in the morning coupled with a 45km round trip cycle to Arbroath. The intention had been to to cycle there, coffee shop code for a bit and then come back. That'd have worked fine if I wasn't the sort of idiot who leaves the keys for their bike lock at home. So after 5 minutes of cursing I just cycled straight back.

Must admit I'm still enjoying this, only started in earnest 4 weeks ago but I've now built to a pretty solid with daily commute round trip of 20km per day coupled with a longer run at the weekend. I can feel my legs getting and times creeping up. The ride today ended with the longish climb up Grange Road and the big surprise? It was my quickest time so yet :) A jump to sixth in the segment leader board. In your face Vicki Maclean! :D

Cross Domain AJAX Calls - Empty Calorie Learning

I know about this sort of thing now and kind of wish I didn't. After quick change in how I'm organising my home project I hit a bit of a brick wall with this nonsense. Basically if your page is served to your browser from machine A then your web client can't make an AJAX request to a different domain.

Only it can through the horrid bodge that is JSONP which I got working. But then you find out all requests have to be PUT which breaks the idea of using http verbs to implement a [RESTful interface][REST. So I binned that and have ended up using OSXs built in install of Apache as a proxy to the game that all IO is done through.

And this is great as I'm not getting a better level of separation between my game app and the browser editor client with game only have to handle the REST api. Only the other thing I learned is Apple removed easy access to the Apache server they bundled and in turn had to learn a load of rubbishy rubbish on re-enabling it again and then how to set it up as a proxy.

Also I learnt some stuff about manipulating headers in Apache to allow CORS and also setting Chrome up so it won't moan about trying to do this kind of thing.

And did that move my home project along? No. Gnnnng. It's a shame in some ways, I had a fair bit of energy this weekend, used it up and don't have a lot to show for it development wise.

But On The Plus Side

There's always days like this I suppose and I did learn some a few awesome things. Had a brief look at various bits of JS middleware out there. This thing? it's an excellent thing! I want to eventually have some live shader editing and this will be perfect for it. KickJS already does that kind of thing and it works really well.

As the JS code I'm writing gets beyond a few hundred lines it's apparent some structure and organisation that the language doesn't naturally support. The way different bits of Javascript can know about each other is by polluting the global namespace with a a load of mess. There's a few formalised approaches to packaging things up which I found after playing about with my own solution. It was interesting reading about these, I'll likely go for CommonJS or RequireJS not sure which yet. In turn lead me to an introduction of the upcoming Harmony and tangentially about the imminent Coffeescript 2 release and its exciting Kickstarter journey.

I learnt a bit more about nice development environments for Coffescript and Javascript. After experimenting with have Guard auto compile things for me last week I'm now using Cake (yes, every language seems to have their own build environment, there was make, Ruby's rake so there's also cake of course for Coffeescript) after wholesale ripping off the really pleasant setup in this lovely demo app of Coffee Physics. It's so cool all of this stuff is easily available as source and so easy to learn from. So much great stuff out, just found a port of bullet to JS!

Actually when you write it down, on top of the cross domain nonsense I learnt a lot of good stuff. Not so bad then.

I Learnt I Don't Like Old Naked Men Commenting On The Fancyness of My Goggles

Went for a bit of swimming with Suzey and the boy at the local gym. Which was ace. Charlie's really come along and close to swimming completely solo. Right now he's just got a float to help him along but he's kicking away in the water and putting his head under with a boat load more confidence and verve that the last time we went.

In the changing room after a naked old man looked at my googles and said they were 'fancy'. And I really didn't like it.

References

Strava ride 1 Strava ride 2 Strava ride 3 Up Grange Road leaderboard JSONP Cross Domain AJAX Module Approaches Common Js Require JS Harmony Cake make rake Coffeescript 2 Coffee Physics KickJS Ace Code Editor Coffeescript2 Kickstater JSBullet CORS Chrome CORS Lion Apache REST

Enjoyablility

Posted on

Enjoyablility

It's important to me that I enjoy working on my home dev project. If you're doing stuff for fun well it should be fun. I know that sounds kind of obvious but it's something I missed in the past at times and kind of have a good handle on now. FIrst of all you have to know what you like, right now for me (and I am pretty phase) I really enjoy:

  • Quick turn around of development
  • Learning new stuff
  • Not having to repetitious work

Last night wasn't really like that, it took bit of slog but I think I have my dev env set up nicely now to deliver on fun factor a bit more :)

Javascript / Coffeescript

I've been enjoying playing with Javascript the last week or so. I wrote a little server in node.js and have now moved onto writing an editor for my game in it. And its been a lot of fun. Very fast to develop in, lots of great resources on the web to help you learn and for a dynamic language it's pretty nippy too.

The immediacy though, that's what I like best. Save your file, flip to the browser, refresh and your code is running. I love that, so quick and no horrendous compile / link times. There's even a debugger. Well there's lots of them but the one in Safari has given me break points and variable inspection. That's nice for a dynamic language.

There are some niggles though. Syntactically it's a bit shit. Being able to inline embed anonymous functions often leads to a lot of indentation. You can get stuff like this pretty quickly

$('#tilda').tilda(function(_line, _console) {  
  quakeConsole(_line).success( function(_response) {
     _response.response.forEach( function(str) {
      _console.echo(str);
    });
  });
});
Ooof, look at that nasty trailing brace and bracket storm! The code ties the input from a [JQuery][jquery] based quake style [console in the browser][jqueryterminal] to my game back end (yeah I know, how cool is that?). It uses jQuery to get the node with the console in, sets a callback for any input. Once it gets input quakeConsole sends it to the game and also talks a callback for any response and that callback sends it back to console object. You get a lot of that sort of ping ponging in this sort of work. It really hurts the eyes. It's not fun to type and maintain either. I kept getting it wrong. Some sort of syntactic sugaring for Javascript that got rid of that would be nice wouldn't it? Turns out I'm not the only moany, fussy idiot in the world who's brace averse which is probably why [Coffeescript][coffeescript] exists. So the above now looks like this:
$('#tilda').tilda ( _line, _console ) ->  
    quakeConsole(_line).success (_response ) ->
        _response.response.forEach (str) ->
            _console.echo str
A lot easier on the eyes, quicker to type and easier to maintain. Scope is defined by indention rather than bracing and have never really done Python this is my first time for that sort of thing. A bit intimidating but I'm used to that now. Some help in the [editor][sublime] has made that process a lot easier. [SublimeLinter][sublimelinter] has been really handy which after a bit of fiddling has real time linting support for a lot of languages. Very handy seeing schoolboy errors fussily being pointed out as I type :) There's a bunch of other things Coffeescript brings to the party. As well as Python Ruby seems to have been a big influence too which has made me feel nicely at home. It also makes doing anything a bit class like a lot less fiddly and prone to error. JS can do class based OO but it means a lot of pointless typing. Not any more. There's a few issues, the code you debug is different from the code you right and being new to all of this means I'm debugging quite a bit. That's not been too bad though as the mapping is fairly light and surprisingly easy to map in your head. It's made writing JS more fun for me and I also go to learn something new so result. It does mean a compile stage though and I didn't want to lose any of the immediacy I'd been enjoying so much. # Compiling! Yuck So I wanted to preserve my tight little dev cycle of save in the editor, tab to the browser and immediately try things out. My home project has a nice build environment based around the excellent [Rake][rake] where any data that needs transforming to another format for use in the game is processed there. It only builds files that have changed is pretty quick but even adding that one extra step of invoking a build before trying out my new code rankled a bit. Turns out something called [Guard][guard] is a great solution for this. It's written in Ruby and easily installed as a Gem. Basically it'll sit in the background watching your files and as soon as any of them changes will do some background task for you. Best of all it's really popular and people have written a shed load of different plug ins that support all manner of different actions. And there's one for Rake. With Guard and the rake plug in installed all it needed was a Guardfile that listing out what files to watch and some options for rake to do its magic:
# A sample Guardfile

guard 'rake', :task =>; 'html' do  
  watch(%r{^media/html/.*$})
end  

Simples. A regex saying look at the html src dir and if anything changes? Builld the 'html' target with Rake. It works really really well. I have an immediacy of development that's an absolute joy while being able to use a pre-processed version of Javascript that I'm finding a lot less laborious to write. I've used the same setup to let me edit my CSS in sass as well.

Summing Up

It was a bit sloggy last night but it all feels really good now I have this all nicely in place. Something that's been great using technologies that have come from the web is there's a wealth of well maintained and incredibly useful tools.

Generally if there's something that niggles or your dev cycle seems a bit tortuous then someone has had a crack at solving that problem already. As with most things you need to put a bit of back into researching the issue and finding the right way forward. Chances are there's actually 10 solutions and half of them are shit but put in the effort, embrace the fear of the new and you'll likely find the solution you're looking for.

References

jquery jqueryterminal coffeescript sublimelinter sublime guard rake guardrake sass