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

Script Wrappers in PHP

I’m guessing that most web developers with a few years of experience have at least one nightmare client. I’m currently struggling with a total arse of a project. This one in particular was originally built some years ago by a predecessor of mine who, to be fair, was fairly lazy in a lot of respects.

Part of the work I’m currently doing on the site involves moving the whole she-bang over to one of our newer, faster, better severs. The initial move went surprisingly well, (I LOVE WHM’s account transfer features), but I soon hit upon a large stumbling block.

Continue reading

Share Button

Client Quote of the Day

During a meeting with one of my clients, she noticed that the mouse and keyboard we were using were wireless. I guess she’d never seen a wireless mouse before:

“Oooh, this mouse has no ropes!”

I honestly have no idea how I kept a straight face.

Share Button

The Importance of Indexes

One of my clients has a rather large shop, with in excess of 40,000 products over a couple of thousand multi-tiered categories. Recently the company’s website started to run slowly, seemingly at random times of the day. The reason was surprising.

After some frustrating investigation, the client realised that after each period of high server load he’d often immediately receive an email confirmation of an order being placed on the website.

The order confirmation script, (in this case a HSBC ePDQ callback), queries the database for an order matching a UID passed from HSBC. The query also checks that the order is still marked as un-paid, as added protection against duplicate orders.

SELECT *
FROM `orders`
WHERE `uid` = '$orderIdFromHSBC'
AND `paid` = 0
LIMIT 1

There was no index on `uid` or on `paid` and there were heaps of orders in the table… this customer in particular doesn’t like to archive his old orders.

The addition of a single index on both columns, in order of reference, (it’s more efficient, don’chaknow), fixed the problem immediately.

Share Button

Client Quote of the Day

“I want the logo changing of course… I want it to… Umm”, (looks blankly for a few seconds), “umm… web two point zero”.

Thank God it’s Friday.

Share Button

Developer Says ‘No’

I’ve just had an interesting discussion with one of my accounts managers which raised the question: when do you say ‘no’ to a client’s request?

The client in question was asking if their main navigation could stay anchored to the top of the browser window when a user scrolls down the page. I said that, it could but the effect might not work in older, browsers. It was then mentioned that the client had an alternative solution: An internal scroll-bar, (iFrame style), on the content. I pointed out that that was stupid because anyone on a low resolution, (or anyone who resizes the browser window, for that matter), would probably see two scroll bars – one for the page in the browser and one for the content on the page. That’s just dumb.

I could see the manager trying to think of a way out of the situation, without saying ‘yeah, but it won’t work for everyone’ but it begs the question: Why not just tell the client that keeping the menu at the top of the page and not having crazy scroll bars everywhere is pretty much the norm. We are after all employed by the client to advise as well as acquiesce to their wants.

Sure, we’re likely to keep a client sweet if we do everything they ask for, but if what they ask for is stupid and potentially detrimental, isn’t it up to us to draw the line?

I’ve also seen the same situation with client-led ‘design tweaks’. This cartoon illustrates the dilemma, perfectly.

Share Button

MySQLHell

I’ve just had a nightmare of a development problem. In a nut-shell, I’ve had to import data, (specifically content articles), from one server to another.

This is normally not a problem, but in this instance the source database had each article’s content split into multiple ‘page’ rows. Also, the source database was developed and hosted by another company. They refused to install PHPMyAdmin, (for “security” reasons), and would not give me FTP access. All I had to work with was a terrible database admin system which they’d cobbled together.

Continue reading

Share Button

Ubuntu Vs. Broadcom

I have a Compaq Presario F500 laptop, which comes with a built-in Broadcom wireless network adapter. Vista Home Premium came pre-installed, but the system only has 1Gb of RAM, which is the it’s maximum.

Vista is so power-hungry that things were never going to go well. The straw which broke the camel’s back came one Sunday afternoon when I was surfing the web while listening to music on WMP. I navigated to a site with an embedded flash video and the system froze, completely. I rebooted and tried again but got the same problem, the system was totally maxing out. It’s worth mentioning that I already had Aero and all the fancy, (read sparkly; pointless), effects turned off. I came to the conclusion that Vista had to go.

The system is distinctly mid-range and I only use it for surfing the web, chatting online and email so I decided that Linux in anUbuntu flavour was the way to go. The software and feature list which comes with this distribution is pretty good and installing was a cinch so I was really happy… for a few minutes, at least!

As it turns out, Linux hates the Broadcom wireless adapter in my laptop. It took me hours of Google searching, driver installations, plenty of swearing and a complete re-install to get it working. Even now, it’s buggy to say the least. I really think it’s issues such as this which put people off using Linux over other operating systems, which is a shame.

Anyway, the solution to this too-common problem can be found here.

Share Button

Array Looping in PHP

I remember back when I as learning PHP I had to write a directory “spider” function to list the contents of a directory. (Including sub-directories).

The approach I took involved reading the initial contents of the base directory into an array. The plan was then to loop through the array adding any sub-directories, (and their files), recursively.

In my naivety, I assumed that I could do something similar to:

foreach($results as $result){
	if(is_dir($result)){
		//Add directory's contents...
	}
}

But, I quickly learned that the foreach construct works on a copy of the array, so adding to it in a recursive fashion is not possible. Bum.
There’s a fairly simple solution to the problem though:

for($i=0; ; $i++){ //Infinite loop
	if($i < count($results)){
		$result = $results[$i];
		if(is_dir($result)){
			//Add directory's contents...
		}
	}
	else break; //Nothing left to process, so break out of the loop
}
Share Button