php namespace seperator

October 28th, 2008

For those of you who are keeping up with the latest developments of the PHP language, you will know tha they finally decided on the serperator used to seperate namespaces, and to almost everyone’s horror they went with…

\

…that’s right, a bloody backslash. I think Paul Jones summed up what we are all thinking in his i.m. transcript.

Ok, so pretty much everyone agrees, that a backslash, blows…but what could be used as an alternative, I know a few people have suggested a single dot (foo.bar()) but this will make it ambiguous withs string concatenation. The original contenders were as follow (assuming the namespace foo):

  • foo\bar()
  • foo**bar()
  • foo^^bar()
  • foo%%bar()
  • foo:>bar()
  • foo:)bar()
  • foo:::bar()

I think the triple colon would have been my shout, sure it’s an extra character, but it fits in well with what we know so far. Would love to hear other people’s opinion on what they would prefer, if their not happy with the backslash.

mysql order by numeric difference

October 1st, 2008

Whilst recently implementing a discount system for an e-commerce site I needed to apply the biggest discount should there be a product which was eligible for more than one discount. As the system allowd x for y type discounts I needed to order my results with x-y so I could find out which of the x for y discounts was the best value for money.

In order to do this I used something like the following SQL:

SELECT discount FROM discounts WHERE ABS(x_value - y_value) DESC

With thanks to:
http://www.bradino.com/mysql/order-by-numeric-difference/

first 5 steps to maintainable code

September 10th, 2008

Bad, untidy, ugly, messy code is one of my pet hates. This is by no means a definitive post, but a few guidelines on how to instantly make code less ugly and more maintainable. Please don’t think that by applying these 5 steps that your code will instantly be maintainable, however it would be a good place to start!

1. indent your code

If I open up some code and its not properly indented, I have to wade through the code and indent it correctly. It honestly makes it soo much easier to read and make changes to, if everything is indented properly. Sometimes I wish PHP was like Python and absolutely required code to be indented properly.

I cannot stress this enough, indent your code!

2. name variables meaningfully (but sensibly)

Another one of my pet hates. Variable names like the following…

$mv = 'blah';

I would much rather have to spend an extra few seconds writing a longer variable name which makes sense, so in our example above, something like…

$my_variable = 'blah';

Now, this also requires some common sense. Yes longer variable names are good if it makes them more meaningful, but that doesn’t mean you should go overboard, for example…

$this_is_my_variable_name_right_here_long_and_meaningful = 'blah';

…is just taking it a bit too far.

Meaningful variable names should also be used in the database. For example TABLENAME_id is far better than simply id.

3. comment your code

There is no such thing as ’self documenting’ code. Sure you can write some code which is really obvious what it does, but if there’s any doubt what so ever, even a smidgin…write a frickin comment.

It will take only a few seconds extra and could save the next developer hours.

There is also no such thing as over-commenting, although quality should always trump quantity.

Also worth mentioning is docblocks, these are excellent and means you can use a documentation tool such as phpDocumentor to create documentation for your code.

4. use a coding standard

Coding standards make life so much easier, especially when developing in a team. They set the development rules. Without rules, anarchy ensues, and the same goes for code. I hate to have to open some code and see something like the following where different developers have added or edited the code…

require_once 'blah.php';
include("foobar.php");

if ($foo == $bar) {
    $random_array[] = 'hello';
}

foreach ($random_array as $word)
{
    echo "hello";
}

Using a coding standard you would not get the mixture of bracket placement, the single and double quotation mash-up and you wouldn’t get the combo of require_once and include().

There are a few coding standards out there, but the one I would go for is the PEAR standard.

5. separate code and html

I’m not going to get into a spiel about MVC (I’ll save that for another post) but what you should definately have is a clear separation between your code and your html. Even if this means just having your code at the top of the page and the html at the bottom, it’s a start. However, ideally you should be putting your html is a separate file, bonus marks for using a templating engine.

obligatory dev tools post

September 9th, 2008

I’ve been blogging now for a few months (albeit quite sparsely!) and I think it’s about time I write the obligatory dev tools post. These are tools that I use day in, day out and couldn’t imagine developing without. If you have any which you would like to add, please do so in the comments below…

editor/ide

Texpad - love its flexibility and simplicity. Does everything I need, never crashes and has loads of settings.

ftp

Smart FTP - ok, so you have to pay for it, but it really is the best ftp client out there.

honourable mention

FileZilla - a nice, simple, free solution. Seems to update all the time, which is a bit annoying, but other than that a very nice alternative to smart FTP.

database management

PHPMyAdmin - Was torn between this and Navicat for the main spot, but went for this because it’s free.

honourable mention

Navicat - This has some really nice features and is a lot more enterprisey than PHPMyAdmin, but then you have to pay for it :(

browser

Firefox - at the time of writing Google Chrome has just been released and so far it looks good, however firefox is still number 1. Everything about it just feels right, and the add ons take it to a different level.

honourable mention

Google Chrome - just released, but looks by far the next best thing after firefox, and who knows, could even surpass firefox one day (Tangent: isn’t google developing a browser a bit of a conflict of interests, considering the money they plough into firefox?!)

firefox extensions

Web Developer - the mother of all firefox webdev extensions. If you’re a web developer and you don’t have this plugin, you may as well go home.

Firebug - Another plugin I couldn’t live without. The inspect function is awesome, the javascript console and debugging is incredible, again this is a must have.

HTML Validator - Validates pages as they load and has some nice features like tidying up the html.

Color Zilla - Lets you instantly get any colour from any web page with the handy colour picker.

Screengrab - Allows you to easily take a screengrab of the page, visible portion or just a selection.

YSlow - A plugin for firebug this allows you to see how fast your page is and where you could improve.

Delicious Bookmarks - Allows me to easily bookmark pages and also gives me access to my bookmarks at the click of a mouse.

Bloglines Notifier - A bit naughty this one, but when you love bloglines as much as me it makes sense.

version control

Tortoise SVN - If you’re using subversion then you need to have this gui tool (if working in windows).

regex

Regex Builder - There are loads of online ones, but I like this one for its simplicity and quickness. Shame it requires the .net framework though.

misc

Stickies - Very useful when having to log time. These handy sticky notes sit on your desktop so you can easily record what your working on and between what hours.

Ok, so that’s my list. If you would like to add to it, or feel there’s a tool out there which you can’t believe I don’t use then please point me toward it.

useful function of the day

August 12th, 2008

An equivalent javascript function to php’s urlencode() is escape()

to vi or not to vi

July 30th, 2008

In the process of setting up a home development server. Basically want a debian box with all the necessary stuff loaded on to it, so any personal projects or freelance stuff I can do on a decent box, rather than develope with a wamp setup.

Anyways, during my setting up of the server, I managed to mess things up, so I couldn’t boot up into it. I knew exactly what the problem was and knew the exact file I needed to change. Luckily, I manged to use the installation disk to boot me into a command line (one which isn’t just readable!).

The command line was limited to only the basics, naturally. However, little did I know that the only editor I could use (or so it seemed) was vi. Now I have never used vi before and can safely say, they I do not plan on using it every again.

it’s frickin crazy!

I know there are lots of vi fans out there and they would probably berate me (if anybody every manged to find and read this blog!), but it’s the most retarded editor I’ve ever come across. It took me the best part of an hour to remove just two words from a file, an hour!

I’m sure their are perfectly good reasons why it does things in such a crazy way, but it confused the shit outta me. Things I learned from using vi:

  1. to delete a character it is not backspace or delete, oh no, its the letter x
  2. to move the cursor from left to right and up and down its the keys h,j,k and l
  3. You need to be in command mode to navigate around the file, and interactive mode to edit
  4. i will do all i can to ensure I never have to use vi again!

Give me nano, pico, emacs, joe anyday.

redesign season

July 30th, 2008

Not sure why, but it seems to be the season for a re-design. Two of the sites I visit most have just gone or under a major re-design. The sites in question are last.fm and facebook.

last.fm

last.fm beta
I must say, when I intially saw the beta of last.fm, I was horrified. I feared the worse and it seemed that they had created a really bad version of the facebook design.

Fortunately, the feedback they received must have sunk in, and they changed the design into what greets us today, and I must say that the new design is a cracker.last.fm new design

I love the simplicity, elegance and originality of it. Bravo to the last.fm team. I’m sure I will now be spending even more time on there, when I should be doing work!

facebook

I never really had an opinion on the old facebook design, as it wasn’t much of a design as such, but then isn’t that the beauty of it? The site isn’t really about the design but about the functionality, so it is a good thing if I don’t notice the design. They say in a football match that if you don’t notice the referee then he is having a good game, well I think it’s true for the design for sites like facebook too.

The new facebook continues this trend and tidies some things up quite nicely. I’m still unsure of the home page, as some of the story feeds on the homepage seem a bit lost amongst all the white space. However, I think the other parts of the site really excel under the new design. No longer do I need to go to a profile page, only to be smacked in the face with annoying applications telling me to write on their funwall or become a zombie.

The sectioning of all this information and lumping all the apps together on one page is a welcome and much needed addition. The fact that the feed take prominence on the profile page is good, although I do miss the wall as in just a wall of comments.new facebook design

All in all I think it’s a worthy redesign, but will take some getting used to.

conclusion

I’m sure the owners are facebook and last.fm are glad that my opinion is now out there, how they slept at night not knowing my thoughts on the new design I do not know. However, to sum up, I think that the designs are neat, tidy and clean, which is fine by me. Another site not mentioned in this article (now it has been! :P) which has recently gone under the design knife, is the bbc website. Despite the web 2.0 gimmickery on the homepage, they also went for a clean, big and easy to read format, and I must say that I’m all for it. I tend to flick through the web at a really fast rate, so anything which makes my experience fast and easy is ok by me.

truncate text snippet

July 17th, 2008

Just a quick snippet to truncate a string to a certain length (in this case 200 chars) and append … at the end of it.

<?=(strlen($my_string > 200) ? substr($my_string,0,200).'...' : $my_string?>

If you are using smarty you can use the somewhat simpler truncate modifier

{$my_string|truncate:200}

rounding money

July 14th, 2008

What a pain in the ass this turned out to be. When rounding money, how should it be rounded?

Rounding as I know it will round up at .5 and over and down on .4 and under. This works great for most thing, but not so great when dealing with something like cash money.

The problem is that if you are a penny out for one quantity of a product then this can get multiplied depending on multiple quantities. For example:

£75.75 net price is £89.00625 gross price.

This will get rounded to 89.01 pence, then when this gets multiplied for quantities greater than 1, the one pence will get multiplied.

I’m open for best ways to deal with this, what I did was store the gross price and net price for each product, so we have the same gross and net no matter what, and then use this, so if there is any rounding issues it happens only once and does not get multiplied. There may however, be better ways of doing this, I am aware that there is more than one way of rounding, and maybe another method will solve these problems when dealing with money.t

dodgy looking source code

July 4th, 2008

Due to an update to the SyntaxHighlighter Plus plugin, the source code is going to overlap the sidebar for a few days, as I won’t have chance to fix it at all this weekend :\