Monthly Archives: October 2013

Local and Session Storage

With the advent of HTML5, data storage on a user’s local machine is possible without the need for cookies. The localStorage object allows us to store persistent data, in a key-value pair fashion. There is also a sessionStorage object in which we can store key-value pairs, the data for which is cleared when the user closes their browser window.

Continue reading

Share Button

Coding Conventions in MySQL

I like to follow quite strict rules when it comes to coding MySQL queries. Unfortunately, a lot of developers seem to disregard the need for readable and maintainable SQL code which becomes a problem when working with joins across multiple tables and nasties like sub querying.

Continue reading

Share Button

Sublime Text 3

I’ve been using the awesome Sublime Text for my day-to-day coding for a couple of years now and a few months ago I upgraded to ST 3. I’ve used a number of editors and IDE’s in the past but nothing has come close to winning me over as much as Sublime.

Some of the features I love:

  • Multuple cursors/selections. The first time I Ctrl+clicked my way around a document my mind was blown. It makes batch updates to files and data an absolute breeze. I couldn’t do without them, now.
  • Fuzzy searching. Being able to open /admin/css/print/myprintstyle.css by entering “adcsspricss” is great, in a lazy way.
  • “Goto Anything”. From one keyboard short cut you can jump to a different file, a line in a file and/or a symbol/function definition. Oh and you get the same fuzzy searching throughout.
  • The command palette. Find functionality, apply settings, insert code snippets and access options and preferences from a single text-driven box. Great when you don’t want to search through unfamiliar menus!
  • Customisation.¬†Key bindings, menus, code snippets, command macros, and text completions are all open and editable from JSON-formatted configuration files.
  • Plugin API. Sublime Text has a wealth of 3rd party plugins including (among my favourites):
  • User-definable snippets.
Share Button

Ternary Operators and Other Less-Used Control Structures

I like my code to take up as little space as possible. I believe that compact code is more readable and more importantly navigation is easier.

I tend to write my if statements on a single line when there is only one statement in the block:

if($var) echo "Hooray";

Another thing I love to use are ternary operators:

$win = ($validation) ? "Yes" : "No";

The only problem with ternary operators seems to be that a certain proportion of developers refuse to use them. Most often than not, it’s fear of doing things ‘differently’; they’re used to if-else control structures and anything else is new and scary.

I’ve often heard the excuse that developers don’t want to write code which others won’t be able to pick up. In my opinion, that’s a total and utter cop-out! If a developer were to pick up my code and see something they don’t recognise, there’s a chance that they’ll look into it and learn something new, even if only to understand the code they are working with. Surely, that’s a good thing.

Developers need to be adaptable and open to new coding practices otherwise they stagnate and they stop learning. What’s the point in having 10 years of experience if the code you’re writing hasn’t evolved?

I look back at the code I wrote when I got my first commercial web development job and it’s vastly different to the code I write now. I’m also constantly reviewing the way I structure the PHP, SQL and Javascript code which I churn out. For instance, I recently had the revelation that rather than doing something like:

$win = ($validation && $winning == 'yes') ? TRUE : FALSE;
//Or:
if($validation && $winning == 'yes') $win = TRUE;
else $win = FALSE;

I can just do this:

$win = ($validation && $winning == 'yes');

Any expression (so I believe; comment if I’m wrong) within the parentheses will assign a boolean value. How clean is that?

Share Button

Tables Are Bad, Mmmkay?

A couple of weeks ago I got into a heated “discussion” with a fellow dev about the use of tables in HTML. His argument went something like:

“Tables for layout are bad!”

Which I countered with:

“Agreed, but using tables for tabular data is both correct and DDA compliant.”

Which was met with:

“Tables are BAD!

How’s about that for nerd snobbery? I fancied debating the guy on how IE is improving with regards to standards compliance but I fear he would’ve hit meltdown.

Share Button