Pre - Pro JavaScript Design Patterns

March 10, 2008 at 6:38 pm (Coding, JavaScript) (, )

I just bought Pro JavaScript Design Patterns from Amazon for my birthday and it arrived today. Recently I have really got into JavaScript and decided on getting this book mostly because I enjoyed reading Dustin Diaz’s blog so much. The blog itself hasn’t had much recent content but it delivers a lot of very educational and enjoyable reads that are as validate as the day they were posted. I have never purchased a book about coding or even computing really, so this is a prototype to evaluate there benefits. I figured I would land some JavaScript design patterns that I like to use, very personally, so that after I finish the book I can compare how I code then. For the most part the techniques will be very basic but hopefully they benefit some web wonders.

Defining Functions

Usually you learn to define functions like so…
function doSomething() {
(…)
}

However, I like to define functions like so…
var doSomething = function() {
(…)
}

Semantically they are identical, but it’s just taken a preference recently. It keeps my code looking more consistent with the way I define objects (see literals below).

Literals

Sometimes it just seems ridiculous to define a new object (I don’t like using new to much (when you can use a literal)) …
var myString = new String('foo')
var myArray = new Array(’foo’,'bar’)
var myObject = new Object()

You probably already use some literals…
var myString = 'foo' //creates a string literal value
I like to additionally use these literals…
var myArray = ['foo','bar'] //creates an array literal, with values
var myObject = {} //creates an object literal

Object Initialisers

More commonly referred to as JavaScript Object Notation (JSON). Nearly all the JavaScript I write uses JSON. It’s a much better way of writing your code. It is a collection of name/value pairs usually stored in objects and arrays.
var ourObject = {
doSomething: function() {
(…)
},
‘myString’: ‘foo’,
myArray: ['foo','bar'],
myObject: {}
}

I have stored similar objects as before, however, even though I have stored them with the same names they will not over write the previous objects because they are stored with ourObject. They will render exactly the same results but will be called like so…
ourObject.doSomething()
ourObject.myArray(0)
// etc …

Using this type of coding is much more efficient, and it’s considerably easier to read and understand because code is grouped into objects. It also allows you to use the same names for functions, for example ‘init’, which I use a lot.

Permalink 2 Comments

XNA na na

March 8, 2008 at 10:52 am (C#, Coding, Development, Gaming, Life) (, , )

Today I broadened my programming schema with a games development workshop. The workshop was very brief, only lasting one short day, or long day when there’s travelling involved. I had an extremely early start at 5.50 (sense when was there a 5.50 in the morning? It was news to me too.). After struggling out of the house I headed down to the train station where I met Aaron and eventually Ciaran and Mike. We started are adventure at 6.46.

We confusedly got some trains to Coleraine University stop and even more obtrusively wondered the campus in search of the “South Building”. After a short pit stop in the students union we ended up circumnavigating the South Building until managing to find the actual entrance and an unlocked door. By this stage we had been drenched in an over exuberant volume of Coleraine’s weather. However, with Aaron about it’s hard to put any dampers on group morale.

In spite of the self indulged perplexity that our group of dysfunctionate individuals had inadvertently induced, we arrived with roughly two hours to burn. We twiddling our fingers for a bit, but ended up helping Darryl set up for the rest of the people coming and chatting to David & James.

Everyone was situated in the labs for around 10.30 and the workshop kicked off. There was an introduction to XNA Game Studio and simplistic only-what-you-need-to-know guide to general programming aimed at C# with XNA. We moved swiftly into some exercises designed to get us used to general workings of the Visual Studio extension. They really helped but could get frustrating if you couldn’t find a solution to your problem.

After a lunch break we started into the real tender meat of the day. There was small shooter games (think asteroids and space invaders) that we needed to modify. I started making modifications to the way the ships guns would fire, limiting the amount of bullets that could be used at once. The amount would increase when you destroyed more ships. Aaron, beside me, started into the tasks of taking the single player game and making it co-op. Semantically, it was a very ugly process involving copying most of the existing code and running it again for difference instances, but the output was brilliant.

As there was going to be a prize (a copy of Halo 3) for the best modification, and I thought Aaron’s was more outstanding that mine, I took what code I had already done and added it into his project. We both worked on his and together made it fairly decent. It got top three so they had a match on one of the games a student had made. Aaron won - somehow. That’s a copy of Halo 3 for the dude without an Xbox 360 (Oh dear).

That pretty much wrapped up the day. It was really educational, but more importantly the people holding the workshop were great fun to be around. I have to greatly extend my gratitude to the persons involved in the day.

Permalink 2 Comments

Birthday 2007

March 4, 2008 at 6:51 pm (Life) (, )

Woop! It’s my birthday. Got a coincidental half day which was nice. Didn’t really do anything eventful. Cake - num num num…

Permalink No Comments