Archive for the ‘programming’ Category

Why jQuery it?

Sunday, November 13th, 2011

It took me a while to get into using the jQuery library (http://jquery.com/). I found the slight differences between complex Javascript and PHP sytax just enough to confuse me, and having never really been one for event driven languages, so adding event listeners and managing triggers was baffling.

Now though, I cannot understand how I was programming any Javascript, let alone interactive interfaces without jQuery. For a start, being able to do
$(“#element_id”)
rather than the extremely long-winded and sometimes unreliable
document.getElementById(“element_id”)
is a Godsend.

My main experience in using jQuery came from the need to develop a complex interface to simulate Excel type features. These included highlighting cells in a table and calculating totals, averages and counts of the cell’s contents, hiding/showing/adding/removing columns and rows and advanced validation.

At the time, I loathed the project, but as with most projects like it, you end up learning so much more than on the projects you love. I will never forget that project – for its bad times and for the new love it gave me for jQuery!

MVC is not a file structure

Tuesday, September 13th, 2011

Recently I’ve been seeing new projects laid out with a file structure attempting to dictate an MVC architecture, with model, view and controller folders. For many web applications though, this is much too limiting. It’s a structure that suggests that elements can be reused – could any element in the model folder use any element in the view folder? I suspect not. Implementations I’ve seen attempt the following structure (usually under model and view folders):
/model/
home.php
login.php
users.php
/view
home.php
login.php
users.php

Shared elements are suited to this structure, for example classes that output standard pages, or standard model functions, but I prefer file structures that allow for use cases per file. Generally I create each as a separate class, but I’d argue that a file as simple as this can be considered as MVC architecture.

function load($mode){
/////////////////////
// CONTROLLER
switch($mode){
case “printHash”: printMenu($_POST['toHash']); break;
}
}

function printHash($toHash){
/////////////////////
// MODEL
$toHash = stripslashes($toHash);
$hash = sha1($toHash);

/////////////////////
// VIEW
echo $toHash.”=”.$hash;
}

Whilst at University I was taught too much about what MVC is rather than why and when it should be considered, and I think that other developers are suffering from the same tuition. The aim of an MVC architecture is to encourage independent development and testing and to present clean and logical code that is easy to maintain. If you can use MVC to achieve this then it does not matter how you structure your files.