Javascript Challenge: Results

So it’s almost been a week since I posted my Javascript challenge and I’m not surprised by the response it received or lack thereof however I’d like to thank Lenny for taking part and teaching me a few things about Javascript along the way. Below is his implementation, followed by my original version at the end.

What was learnt

  • All properties of an object can be accessed via []
  • By default, Javascripts join method uses commas for it’s delimiters.
  • A more elegant solution for my problem.

After seeing this solution and how ugly it made mine look, I finally made time to start reading ‘Javascript: The Good Parts’ by Douglas Crockford and soon came across the section called ‘The Bad Parts’. The third point to this chapter was entitled ‘eval’; I had a feeling this would apear here and below it described a similar situation to my own.

After finding that a solution to my problem was sitting right there infront of me the whole time I started to become intrigued about similar scenarios to this and how they were eventually solved. So if something like this has recently happened to you, let me know by posting a comment below or find me over at twitter.

Javascript Challenge

Whilst working on a piece of code today I had to join the results of a method belonging to an object that was part of an array. Perhaps needing to do so was a flaw in my design but it got me thinking.

I couldn’t find a solution whilst searching the net(maybe I lacked the correct vocabulary?) so I whipped up something quickly. It’s not elegant or pretty however it does work and it gave me the idea to try a javascript challenge.

The Challenge?
Fork the gist below and improve on it to create a nicer solution (this shouldn’t be too difficult, my javascript isn’t the best!). The required usage can be found here.

The Prize?
Bragging rights? Sadly, there is no prize other than the satisfaction of knowing you’ve helped changed a developers life(haha.)

And then?
Leave a comment below or contact me on twitter -@EddieBluh and I’ll include your solution in a roundup post. Allons-y!

Randomarks: Chrome Extension

When I find something interesting but am feeling too lazy to look into it properly, I drag it into my bookmarks bar to look into at a later date. After a while, it gets pretty full (any relation to how lazy I am?) and they need sorting out. I thought it would be cool to have an extension that would act like stumble upon however only stumbling through my bookmarks, so being a developer I made one. The Chrome API docs had everything there for me so I dived in and created it in about an hour.

You can find the extension here:
Randomarks

And I’ve put the source up on Github:
https://github.com/EddieDev/Randomark

Overall it worked out pretty well, feel free to join in and send a pull request. Any suggestions or improvements mention me on twitter or leave a comment below.

Don’t Hate PHP

I’ve been developing with PHP on and off for the past few years, and one thing I’ve learnt is that there are haters. Including myself. Each day there are new posts on why you shouldn’t use PHP and that it doesn’t necessarily encourage you to use the ‘best’ techniques within your code. To an extent, I agree with this but as we all know PHP is staying right where it is for reasons I wont go into.

At my work, I write PHP daily for two reasons; It’s the web based programming lanuage I have the most experience with,and all our legacy code is written in it. Recently I have started to use Rails for side projects and being able to use the power of Ruby on the web is just awesome, and when I have to go back to PHP a sigh usually follows. I think at times we can under-appreciate great projects and frameworks because of the language it is written in, and as the saying goes ‘the grass is always greener on the other side’.

jQuery Validator Plugin and AJAX Requests

Whilst making some changes to a registration form on a current project, I needed to check if an email address was already in the database and use this as a rule within the validator plugin. After trying to make an AJAX request within validators addMethod, the problem was staring me right in the face and I should of noticed it sooner.

The problem was the Asynchronous part of AJAX. The validation method was running at the same time as the request, therefor the data was not available on completion and returning false. A quick search through the docs and I had found my answer. The async option:

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: “jsonp” requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

It worked, and I had my email exists validation working in no time. If anyone knows of a more refined way to replicate this method, leave a comment below or find me on twitter.

jQuery.validator.addMethod('email_exists',function(val,element){
 
  var available = true;
 
  var options = {
    type:'GET',
    url:'user/email_exists/'+val,
    async:false,
    dataType:'json',
    success:function(data){
      available = data.exists;
    }
  };
 
  $.ajax(options);
  if(available =='true'){
    return true;
  } 
  return false;
 
},'This email address has already been used.');

My Take On Technical Debt

Coming from a freelance background where I set my own deadlines, it took some getting used to having to rush my unfinished,half-tested code. I recently purchased the book ’97 Things Every Programmer Should Know’, and the first piece of wisdom was entitled ‘Act With Prudence‘ written by Martin Fowler. To summarize, it’s an article on “doing it right“, or “doing it quick” in respect to project development.

This is something that is constantly on my mind and was comforting to finally have a name for the situation. Ideally, every programmer would love an infinite amount of time to spend on a project making it tidy,clean,fast and bullet proof. However, unfortunately this isn’t how money is made and a high percentage of the time we have to do what we can and try to create some leeway.

Find the right/quick balance
Unless your working on a personal project or a project with no deadline, it’s more than likely your not going to be able to stick to “just doing it the right way”. Occasionally your going to have to get into some debt with your code and deal with it in time in the correct way.

A big part of increasing the “doing it right” proportion was learning how to say “No”. Constantly wanting to hit impossible(at times) deadlines I found my self saying ‘Yes it can be done’, when knowing it would take three of me to complete the task cleanly in the time given. Occasionally saying no gave me more time to write decent solid code and helped put my mind at rest when starting a new iteration of development.

Final Note
Of course there are times where deadlines cannot be dodged and we have to deal with the code debt, and that means overtime. I often find myself spending a couple of hours after work trying to keep the code clean and tidy. Doing so will increase productivity in the long run and reduce stress. There is nothing worse than hack and slash code in a long term, solo project.

Python – CSV to XML

When working on a recent project I had to convert a load of CSV files to XML. There were many tools on the net, but none of them fit the specs of our required output.  After revisiting Python on a previous script, I decided to have another go and write a CSV to XML converter using it. It’s not perfect but does the job, CSV setup was borrowed from code on the internet but I can’t find the link ( Sorry! )

Continue reading