-
Tags
amazon web services audio aws Barcelona bluetooth bom cambrils cloud configuration costa daurada debian domi.co.uk ec2 editor ethernet farmhouse Food Gadgets git headphones interweb ipod linux mac macbook masia misc music news osx pix programming projects repo sony sport ssd tarragona tech unix version control vim web biz webdev web development
iPhone SDK 3.0 beta 3
Beta 3 is out and the new OS fixes the non-working Youtube from the previous beta.
[Update: and also breaks syncing, which now hangs on "Syncing contacts..."]
[Update2: Seems like the contacts syncing hang was a temporary issue...]
Development Standards: PHP
We might as well start this series off at the bottom and work our way up. So, let’s start with PHP – it’s not the base of our stack, but it’s the first element that heavily requires some coding standards in place.
As I said in the intro, this series will be about how I do things, although of course it’s all heavily based on what I’ve seen and used elsewhere, and what I’ve known to work. Given this, I’ll also talk about the toolkits and frameworks I use: Code Igniter on the server side and jQuery on the client side. I also use YUI Reset and Fonts at the top of most or all CSS.
Although it’s just as important I won’t talk about sticking to some semblance of good MVC practice when using Code Igniter. There’s plenty of that out there.
Why have development standards?
Possibly the main reason is that one person working on the code can understand what another has done. If I look at anyone else’s code and it’s formatted in a way that obfuscates its meaning, I’m possibly going to miss something key.
Looking forward, if something isn’t correctly formatted, fixing a bug is going to be that much easier if it’s easily readable. This applies whether it was written by a different person or not.
PHP tags
In view files, the short opening tag <? as opposed to <?php is encouraged, as is its logical next step “. Reducing clutter in view files, where front-end developers or even designers may work, is worthwhile.
Indentation
All code – HTML, CSS, Javascript, PHP – should be indented correctly. All indents should be 1 single tab. I prefer to view that in my editor – whether it’s TextMate or Vim – as four columns. How you view it in your editor is a personal preference, as long as the file itself uses 1 tab.
Line endings
All line endings should be a single \n – i.e. Unix-style. In addition, add no trailing spaces to lines, and remove any that sneak in due to slack editing.
Control Structures
Including if, for, while, switch, etc:
INCORRECT in various ways
if(condition1){
//action1
}
elseif(condition2){
//action2
}else{
//action3
}
CORRECT
if (condition1) {
//action1
} elseif (condition2) {
//action2
} else {
//action3
}
Another example:
for ($i = 0; $i 123,
'key2' => 'foo',
'key3' => getValue('bar'),
);
Notice the extra comma after the last element – this means we can rearrange the elements or add a new element with worrying about commas between elements. (This only works with PHP.)
Comments
Single line comments go like this:
// Check the value of foo.
$bar = isset($foo) ? $foo : '';
Multi-line, “C-style” comments:
/*
Check the value of foo.
This is to prevent errors.
*/
$bar = isset($foo) ? $foo : '';
Notice that the comment characters are on their own lines.
Includes
For unconditional inclusion, use require_once 'includefile.php';
For conditional inclusion, use include_once 'includefile.php';
Note: require_once and include_once are statements (or language constructs) not functions, so no parenthesis is required, or desired.
Metadata
Every applicable file should use an Id keyword:
<?php
// $Id$
which gets expanded by Subversion to
<?php
// $Id: codingstandards 758 2008-05-15 01:35:58Z dom $
Naming conventions
Functions and Methods
All names should start lowercase and use interCaps/camelCase
function getFooBar() {
...
}
No underscores in the middle of function names, please:
INCORRECT
function get_foo_bar() {
...
}
Private methods should start with an underscore:
function _getFooBar() {
...
}
Classes
Classes follow the same method as methods ;) except start with uppercase.
class FooBar {
function FooBar() {
...
}
}
Constants
Constants should be all UPPERCASE, except internally-provided ones like true, false, null;
File Format
All files should be unix format – I do not want to see any \r\n…
Development Standards: Intro
I’ve decided to write an occasional series of articles on coding and development standards. It will be how I see them – the standards I myself follow – so it might not be for everyone. What it will be is a guide to what to expect if I’m working for you, and what to expect if you’re working for me.
I’ll write separate pieces for CSS, HTML, PHP and possibly for server administration-type stuff too – Apache configuration, and so on.
Expect the first instalment in the near future…
MacBook hard drive upgrade
I just got a new drive for my MacBook (MacBook2,1 – black) and am quite pleased with the simplicity of the process. First up, I didn’t need to add a jumper to throttle the SATA II drive down to SATA I speed. I put it in, it was recognised OK. The drive is a Seagate ST9320421ASG.
Secondly, the restore from Time Machine was pretty damn simple and effective. It’s always the same question with backups – if you don’t test them, do you know they’ll work? I’ve grabbed individual files from Time Machine and that seemed fine, but I had no way of knowing, without having a new drive with which to test, if a system restore would work. It did. Just boot with the DVD (or a copy of it on an external drive, if your DVD drive doesn’t work, like mine) and choose to restore from Time Machine. Just pick which backup to use, pick where to restore it to and wait. I suppose it took about an hour.
Once that’s done, reboot with the new system.
The only things I noticed awry were that my hosts file wasn’t there and that my Downloads directory wasn’t either! Seems a bit odd… Anyway, copy old hosts file to new location, copy old Downloads directory to home dir and sorted. I need to look into the missing directory issue, but all in all, not too bad an experience.
[Update: small, possibly even unrelated error: the firewall seemed to have Ruby blocked for some reason, even though it was listed as allowed - just switched it to block, then back to allowed and it worked.]
[Update2: the Downloads directory wasn't there because I, er, set it to be excluded... ahem. I'd forgotten about that.]
Screencast as development tool
I’m in the middle of the first phase of building a new webapp and like most web developers (I would bet) when I show someone what I’ve done, it’s not apparent what marvels of engineering I’ve pulled off! So, in order to actually give a quick run-down of where we’re at with the application, I thought about making a screencast of the login, profile creation, module, video upl– well, I’ve said too much already ;)
After thinking about that for a bit, I wondered if any methodologies, agile or otherwise, use screencasts specifically as a tool. I could just Google it, I know. In any case I think it’s a nice way of showing progress on the development – or bugs in the app.
Webshop launched
As the first example of my Tienda.co.uk project, we’ve just launched slant1996.com which sells slightly mad t-shirt designs!
ACCEPT LANGUAGE, DAMMIT!
This is probably an oldie-but-goodie to a lot of web devs, but as search indexing bots don’t always send HTTP_ACCEPT_LANGUAGE to the server with the request, it will cause an error if you depend on its presence, which can and will be reflected in the search results, even though not in “any” browser.
