Tuesday, 30 August 2011

Open Saucy

One day, I shall finish my procrastinating and finally write and release something into the open source world. And when that glorious day finally arrives, I may just have to release it under the WTFPL because it is pretty damn awesome:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
Version 2, December 2004 

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> 

Everyone is permitted to copy and distribute verbatim or modified 
copies of this license document, and changing it is allowed as long 
as the name is changed. 

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 

0. You just DO WHAT THE FUCK YOU WANT TO.

/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */

Marvellous :)

Monday, 22 August 2011

Properlog

So, now we're doing it old school as I set off into chapter 1 of Prolog properly. It is sparking a few memories of the old computer science degree, but only vaguely, so it still feels pretty new to me.

Whilst I enjoyed a bunch of the fun examples in previous chapters, here I felt that I had to change some americanisms, velveeta, jolt, soda, twinkie & desert became cheddar, coke, drink, cake & pudding. Plus, flavour and colouring have a 'u' in them. I can't help it, I'm a bit of a grammar and spelling nazi sometimes :)

Anyways, on to the tech stuff, it has some impressiveness, but so far seems less like a programming language and more like a logic calculator as you have to use it within the boundaries of it's runtime interface. I dunno if we'll find out ways to do other work around crafting a UI, or to harness it as a logic engine within a different language for some polygottism, but I currently can't see how I could fit it into anything that I do.

One of the samples was to write a map colouring tool that could give combinations of colours to use so that adjacent states were different colours. The code had rules different(Mississippi, Alabama) and different(Alabama, Mississippi) but all other pairing are only listed once. The rule different was declared in both directions, so I think that this was a bit of a typo, although not really a bug as it is just duplicating a rule. I also found that the definition of the colouring method was overly verbose with the definition having the full state names and the calling code having the exact same. The rest of the rules about what has to be different were tied intimately to the given selection of states though, so a more generic method wouldn't really be right, it just felt like it wasn't really very DRY.

As usual, the chapter finished with a bit of homework, a few quick googling tasks so that we could get a few resources, and a couple of simple apps to write. (Is apps the right word for Prolog code? I'm not sure, knowledge systems maybe...) These were nice and simple to churn out without needing to resort to leaning on any of the resources that we needed to google for. First up was a list of books and their authors with a query to find all books by a single author. Following that was a list of musicians, their instruments, and the genres that they play. We were asked to write a query so we could find all guitarists, and that was it. The genre part was ignored completely, so I made my query a little more complicated so that I could find all musos based on their instrument, or all people playing a certain genre, or a combination thereof. And due to the way that Prolog works you could also find all instruments and genres that a musician plays. As we all know, Mr Eddie Van Halen is both the world's greatest guitarist and a fine keyboard player too ;)

As mentioned, from what I've seen so far, I can't imagine how Prolog can be used anywhere other than in Prolog, which feels limiting. But the code came nice and easy without all the road blocks that Io put in my way, so I'm quite enjoying it so far and looking forward to seeing if it will be more useful than I can currently grok. I found the writing of this chapter less inspiring than others though. Bruce Tate's writing kept me interested through the dark days of Io, but the language is holding together the Prolog for me. Hopefully that is just me being racist to a chapter that has more Americanised examples than the earlier ones, and not a sign that a little way into the book he started to get bored or complacent and let the quality drop. Lets see what awaits in day 2...

Thursday, 11 August 2011

Replace Conditional With Polymorphism Refactoring–A Wake-up Call

One of the aspects of good object oriented design that I keep seeing mentioned is to replace complex conditional statements with polymorphism the best known source of this probably being Martin Fowler's Refactoring. This is usually called out when seeing large case statements or stacks of ifs. I’ve always had a problem understanding how polymorphic behaviour would help here, but I think that is because the only time I’ve used much in the way of conditional stuff like that has been in places like just behind the UI layer when processing user input, so there isn’t really the chance to do anything polymorphic at that stage as things are just coming in to the system in the first place.

However, I recently inherited some complex business logic code that is chock full of ifs and cases. All of a sudden I can see where the potential for polymorphising it comes in. This code is absolutely mission critical, arguably the core of our business, and we currently have no unit test coverage whatsoever, and only minimal automated integration tests that certainly won’t be covering all of the edge cases. Add to that our legacy code issues of tightly coupled code and static methods, the facts that it does actually work and is suitably performant, plus medium term business plans to do a complete overhaul of the way that it all works, and I can’t see myself being in a position to do any major refactoring any time soon. But at last I have come across a great example of where that refactoring would apply, which also makes me feel more comfortable about the places that I’ve been tending to pile conditionals as being a valid use of them.

Friday, 5 August 2011

Prologue

Next up in the 7 Languages book is Prolog, and the author points us towards the GNU gprolog package. Fortunately this time there is a prebuilt installer. Hooray, that’s a promising start.

Screeeech. As soon as I try to start the Prolog app I get a file not found type of error.

I am sadly lacking in unixy skills but was able to figure out that the path wasn’t set. Navigating to the correct folder and typing gprolog still gave command not found error but ./gprolog however worked fine. I added a symlink to gprolog in /usr/local/bin alongside the now defunct Io and was then able to launch it. The other option that I tried on my laptop was editing the .bash file to add to the Path environment variable. Both work, but I don't know which would be considered the best approach.

I have only done the first couple of simple examples to check that it is working properly, so I won't yet document my experiences with it, but I will say that after all these semi-colons for end of statements in C based languages, using a full stop feels very civilised :)