Categories
agile dev-process programming web 2.0

PHP: Looking for some Best Practices

PHP is weak, crappy, and encourages people to write terrible code. But … if you know what you’re doing, all of those might actually be really really good things.

(by the way, if you haven’t already, you should read Eric Ries thoughts on PHP…)

“Best Practices” are one of the best guides you can get for sailing between the Scylla and Charybdis of “crap code” and “over-engineering”.

So I’d like some, please.

Perfection

Anyone with zero computer science education can write crap code; that’s easy. At the other end of the spectrum we have the people who write Perfect code.

Anyone who writes perfect code is either:

  1. a liar
  2. too rich to work for money
  3. works in an industry that is immune to marketing

…because marketing is always a cheaper and more effective way of making more money, compared to writing “perfect” code.

(disclaimer: in the past, I’ve been the first, and lusted after the third, but never quite managed the second)

The skill that marks out the difference between a competent programmer and a good (or great) one is knowing which of the language’s strengths to play up, and which to play down. Sometimes, the fact that a language allows extremely sloppy code is a very good thing. e.g. most scripting languages – especially any that run on an “intelligent” runtime like the truly Awesome JavaVM – where being grossly inefficient is not only “OK” but in fact “doesn’t happen”, because once it notices what stupid thing you did, the runtime silently recompiles everything into decent C code while you’re not looking.

So … how the heck do you win?

You look for Best Practices, language-specific (and domain-specific, but that’s obviously of more niche value).

I recently did two moderate sized PHP projects. I figured: OK, so PHP is very easy to hack code together knowing very little about the platform (and I’d been using my Perl experience most of the time when writing PHP) – but Perl showed us that Best Practices are damn useful and not too hard to stick to. There must be lots that the PHP community has worked out by now!

(as Quake3 would say, in a bass rumbling voice:) Denied!

Um, no. Apparently not. Google – with *considerable massaging* – threw out a few dozen attempts from different people.

There was some interesting stuff in there. I found some obscure bits of standard and near-standard extensions I hadn’t noticed previously when reading the manuals.

But, to be honest, nearly all of it was a waste of time – it simply wasn’t PHP specific. Telling people that they should “document function headers” is meaningless as a “PHP Best Practice”. It’s not a PHP Best Practice, it’s a general programming practice. You can find it in *any* textbook (and if you don’t know stuff like that, I suggest you go read a few key books like Code Complete, for starters).

There’s also – and this quite upset me – a lot of BAD practice out there being passed on from PHP programmer to PHP programmer and hailed as if it were “a good thing”, when often it’s about as wise and valuable as staring down the barrel of a loaded gun so you can see better while you clean the inside.

What is PHP good for? Why?

I have recently realised that the majority of PHP programmers cannot answer these questions, and that many of the rest aren’t great at answering them either. If you can’t answer them, you can probably never be a great PHP programmer, and it’s debatable exactly how “good” you are. (/me ducks and runs for cover)

I’ve not done enough PHP to know what it does from the inside.

But I’ve done tonnes of code in “languages that are not PHP”, and I have a really really good idea what it does from the outside. And most of the Best Practices I saw for PHP were stolen from other languages (especially C derivatives) and are totally inappropriate for PHP. Because PHP is not C, and PHP programmers will never be decent C programmers, no matter how big their inferiority complex, and no matter how hard they slave to try and write “high quality code”.

Some things PHP is good at: (in no particular order!)

  1. It’s a Scripting Language (like JavaScript, and ActionScript, and Perl. But not like C, and not like C++, and not like Java)
    • It’s intended to be trivial to read and write code; the core language is “easy for a non-programmer to grasp”
    • There’s no feature in the language that requires thinking to understand what’s going on, by design (unfortunately, some of the libraries broke this. c.f. the great Register Globals disaster too)
    • idiots can code effectively…
    • …which means that highly intelligent individuals who were never trained as programmers can ALSO code effectively, with minimal pain and suffering
    • …and that changing (modifying, fixing, extending) other people’s PHP code is trivially easy
    • There’s no compiler. No cached builds. What you see is what you executed, literally.
  2. It’s very easy to grasp two of the three most important structures of a web application (the third one is “data flow” – PHP sucks at that one). The two it does great are “program logic” and “program inputs + outputs”; together these sum to “execution structure”
    • One page == one source file : you read an output (FORM or A link) to an HREF? Or a page is crashing in the browser? You know exactly which file to find it in
    • One source file == one page : all the logic (PHP tags) and the input/output parts of the user-interface (HTML tags) are in one file. So you can’t make assumptions and the resulting mistakes
  3. It’s a context-free language. This is a by-product of being so tightly integrated with HTTP/HTML. It is one of the things that “real” programmers sneer at PHP for – how can you have a context-free language that isn’t Functional and expect it to be any good? That’s some kind of mutant offspring of Fn and Imp that ought to be strangled at birth. Or … maybe … just maybe … it’s one of PHP’s greatest strengths (although admittedly it is a mutant offspring too)
    • When a single source file is executing *there cannot be any other context* (except for the Session, sadly – but this is a strange piece of context-that-is-not-context because of all the rules about what can and cannot be in a session)
    • Oh, OK: there cannot be any context from other threads, other users, other simultaneously executing code. This is one of the hardest, nastiest parts of all systems languages, the multi-threading stuff. And PHP completely wipes it out as a problem. Nice.
    • All source files – hence all user-interaction points – are (semi-)atomic by definition: either they execute, or they don’t (by default, if they start, they dont stop, not even if a crash-level event happens; more on that later)
  4. Code is mingled with presentation, but is “together but apart” because it’s guarded (in the computer science meaning of that word, sort-of) by enclosing magic PHP tags
    • You can copy/paste move around code without the risk of it becoming part of the presentation code (this problem happens A LOT (and causes some awful security holes and application crashes) in some languages, especially in templating languages, even though it seems like such a simple thing. I kid you not.)
    • Any decent IDE (/wave at Eclipse (download PDT to add PHP to it)) can do a lot better at second-guessing what the programmer wants to type next, thanks to the explicit context of being either “in code” or “in presentation”. Saves a lot of time, in very small pieces many times a day.
  5. It’s a mainstream scripting language: very very fast to write code, code is very very fast to read + understand (it CANNOT DO complex things like STL or Operator Overloading), and you can “make it up as you go along” when writing a program – it doesn’t expect you to plan ANYTHING in advance
    • No declarations : you don’t have to plan your variables in advance
    • No declarations = faster “flow of mental programming” : you can aribtrarily change your mind “mid sentence” when writing code, and not have to move backwards in the source file to fix a declaration as a result
    • No memory management : you don’t have to plan the LIFECYCLES of your variables in advance
  6. Writing basic imperative code. Fast. (Imperative == “not Functional”; if you don’t know what that means, look it up – Fn programming is awesome)
  7. Templates for pages.
    • If you’re going to use a template method (or, god forbid, “library”) in PHP, make sure that its source code looks *exactly like PHP*. Because PHP is an excellent templating language.
    • This begs the question of “why aren’t you just using PHP in the first place, fool?”. The answer to which is usually “I didn’t read Adam’s Best Practices on this website and wrote long waffling PHP source that no-one could understand”, or “I have some truly stupid accomplices who will overwrite the PHP tags whenever they see them, just for fun” (anyone using DreamWeaver has almost certainly done this at some point, without even realising it; in that case, it’s not the human’s fault, it’s the fault of their editing software – DW – but the PHP programmer can’t really tell the difference, it’s the same net effect)

Some things PHP is bad at: (in no particular order!)

  1. NOTE: just because PHP is “bad” at something does not imply that this is a bad thing about PHP!
  2. OOP. PHP is poor at OOP. It’s not as bad as Objective C (which is terrible) nor Perl (which is horrifically awful at OOP), but it’s not part of the core language, and it shows.
    • Actually, I don’t even know what to say here. Go on a long rant about how it should look like C++? Hmm. Not really helpful. I’ll just leave it at that. If you *really* need to ask why PHP’s OOP is bad, it’s going to take more than a couple of bullet points to explain it to you.
  3. It’s an “old fashioned” scripting language; it lacks some so-called modern features (many of them are older than the microchip but took a few decades to make it into mainstream programming)
    • Closures? Where are my closures? : without this, it’s much harder to write self-adapting code, and much harder to write code “for other programmers to extend and alter without needing to change my source code”
    • No OOP scripting : without this, OOP is a pain in the ass, taking a lot more typing, and standing out like a sore thumb inside any PHP file
    • No modern Array derefence syntax : (you have to do: “${arr[‘var’]}” instead of: “$arr.var”) this makes getting data out of arrays so hard that people go out of their way not to use arrays in PHP.
  4. Data flow (the third prong of three mentioned in the Good bits section)
    • SQL in PHP is not fun : it’s all manual SQL statements, with practically zero language-level support (check out Perl::DBI for an example of how funktastic language support for SQL can get; I didn’t say “good”, I said “funktastic”. There’s a difference)
  5. Distribution
    • There isn’t any. PEAR doesn’t count – and if it did, well … PEAR has one of the most appalling distribution systems I’ve seen in about 10 years. The fact that I had to hack PEAR’s installer – in 2008 – just to “not crash” let alone to get it to “start installing” says it all, really.
    • No package management. No bundling. No metadata. No repository (PEAR or CPAN? take your pick. I find both fine as manual systems, and frequent failures as automated systems).
    • (if you still don’t believe me, find yourself a Debian sysadmin and ask them to show you Aptitude, a worn-out decade-old text-based front-end to apt. And marvel at how fantastic it is despite all that!)
  6. Register Globals fiasco : the maintainers killed some core features of the language in an attempt to fix a security hole, instead of just fixing the security hole
    • Sadly, the core libraries now force you to use Arrays for evertying, because the language maintainers panicked over Register Globals and destroyed the whole input-sampling system.
    • …which is bad because of the lack of Array-de-ref syntax in the language (see above)

A quick retrospective

If you’re a PHP programmer, I hope you’ve already noticed the implications of some of the good/bad points I hilighted.

There are things that a lot of the PHP community loves with a lack of reasoning that sometimes borders on obsession/fetishism, but which – from the above analysis – are inappropriate for PHP. I’ve tried some of these, and confirmed: whoever thought this was a good idea was either smoking crack or didn’t understand how to build web applications.

Some quick examples:

  • Page Caching. You gotta be kidding me. There’s are plenty of reasons that all other languages push this OUT of the language, and OUT of the application, and most of them apply equally to PHP. Use Memcached instead, and learn to use it properly – that is, externally, not internally.
  • Separating Code from Data. As a gross generalization, if you do this, you should stop writing PHP, because you just threw away most of the benefits of PHP, and you’d be better off writing J2EE from this point on – you’d get the benefits of J2EE and of the Java language AND of the Java standard libs (which kick the living daylights out of PHP’s weak set of “Extensions”) – and you’d hardly lose anything in the transfer.
  • M/V/C architectures. If you do it the way we do it in other languages, then it’s equally stupid as the above point. It *can* be done, conceptually, in ways that fit with PHP – but the majority of PHP systems I’ve poked inside didn’t get that clever, they just tried to do it the traditional ways.

By The Way: I’m probably wrong on some of this. I haven’t spent much time thinking about it. Unfortunately, I’m not sure at this point which points are where I’m Totally Right, Dude, and which ones are where I’m Smoking Something Funky.

Sorry.

Too many words! Argh

I keep being told off (nicely) for writing blog posts that are too long.

In petty revenge, after making you read through the rant-tastic introductory bits (I have my Asbestos suit at the ready…), I’m splitting the rest into a separate post. I almost feel bad about doing this. Almost.

So, on to … PART 2: Best Practice for PHP Programming!

Categories
bitching iphone

Apple & a study in frustration

Half way through the insane 1.75 gigabyte download of iPhone SDK 2.2.1 (which, lets be honest, has precisely 200 megabytes of content, and 1,500 megabytes of STUFF I ALREADY HAVE; Apple’s engineers apparently have never heard of the 30-year-old concept of a “patch file”), Apple refuses the download.

I “no longer have read permssions to that URL” according to Safari. This is despite being logged-in to Apple’s developer.apple.com website. The same website that FORCE logs out everyone *at least* once an hour (because Apple’s web developer team have miserable lives, and want to share their pain with everyone else).

The same website that has given me “error: this page requires you to be logged in”-type errors trying to load … the login page (I’m not kidding, it’s 100% reproducible in Safari and in Firefox, quite easy to trigger. Seriously weak web-server code going on there).

So, let’s start that 1.75Gb download *again*. Apple: I detest you. Especially because you know the problems you cause, and build it into your marketing campaigns; it’s not cool, it’s not funny, it’s pathetic. Why can’t you employ some competent web developers? Why, when you like to call yourselves paragons of UI design, do you make the iPhone App Store have a GUI that would look “weak”, “poor”, “stupid” and “ugly” in 1999, let alone 2009? What happened?

Sigh.

Categories
amusing computer games design games design

Tabula Rasa: A Plot Summary

Ironically enough, from the LotRO forums:

“You know the only analogue I can come up for this is to imagine a WWII FPS where the opening cinematic tells you that the Nazis have destroyed Great Britain with a giant laser and you’re one of the few English to escape via a magic portal to Russia at Stone Henge which was planted by ancient mystics from China. However the rest of the game takes place in relatively normal WWII FPS style while the Nazis throw paltry attacks into the steppes, and yet most of the people you meet are also British and no one seems to care that Jolly Ol’ England is smouldering black glass. Occasionally you stumble across more ancient Stone Henges to learn Mandarin to gain super powers. The Nazi country-destorying laser is never brought up.”

ROFLMAO. And … excellent plot-summary there.

(remembering that I did actually *like* TR. But the OP has a point)

Categories
games industry

I need your vote (for IGDA elections. Starting now.)

(IGDA == International Game Developers Association)

I’m standing for the IGDA Elections. Right now. If you’re a member, you should have received a vote-by-email slip today. Please vote – you don’t even have to login, it takes just a few seconds.

I want to be elected to the IGDA board because I want to make the organization less reactive and more proactive. Today, we under-sell/under-use the good we do. We can make it more effective, and at the same time make it reach more people.

Categories
amusing bitching games industry recruiting

Open Letter to Recruitment Agencies (video games industry)

Hi!

My name is “Adam” (first name) “Martin” (surname); you might need to check the spelling. You might want to check which is the first name, which the surname – funny how many recruiters get it wrong!

You’ve probably cold-emailed me because you got my email address somewhere – maybe as much as 10 years ago – and yet, bizarrely, I haven’t been coming to you looking for jobs. You’re probably really hoping I’ll write back with a CV/Resume that you can send out.

Instead, I suggest you save us both some time: have a look at my LinkedIn profile, and see what I’ve done – http://www.linkedin.com/in/adammartin – it’s shorter and clearer than a CV/Resume, too.

Hey, if you’ve got a few minutes, why not have a look right now? Take your time – I’ll wait! You can learn a bit about me, find out what I might be interested in (hint: it’s there, in several paragraphs of text, right at the top of the page).

Now, maybe you think you’ve got a perfect job for me. But hold on, my friend! Don’t hit that “Send” button yet! There’s some things you should know before you email me a second time…

You see, each time you email me, blind, cold-calling, un-solicited … it’s not just you. All your competitors are doing it. Even some of your colleagues (it’s funny how many agencies accidentally compete with themselves). And a whole bunch of your clients, the companies you recruit for, are doing it too. And each one of those emails takes me time to read.

My time is precious, I’ve got a lot to give, and I usually go well beyond what’s asked; if it weren’t, there’d be fewer companies that wanted to hire me, and willing to pay the salaries I’ve been paid. And hence willing to give YOU that big, fat, commission you’re hoping for…

“What’s there to lose?”, you may be thinking to yourself, “if you don’t like it, we’re cool, I’m friendly, we’ve got a bit of a relationship going here – I emailed you, you emailed me, it could be the start of a great partnership, propelling your future career gradually up the corporate ladder!”

Well, here’s the thing: I’m a technology guy. I have a degree in Computer Science from one of the world’s top Universities. I’ve been trained and employed as a SysAdmin. I’ve been an entrepreneur, and built my company’s computers myself, to save money. Although I don’t program for money any more, I’m still fluent in many programming languages. And, you know what, I’m a bit of an expert at all that “mailserver stuff”.

So … if you piss me off; if you waste my time with meaningless, unsolicited drivel; if you nag me with “this is an amazing opportunity you will love” when we both know it isn’t vaguely true … I’m going to nuke your ass (figuratively speaking): I will never see an email from you again, they’ll die before they reach me.

And when I say “you”, I don’t just mean “you, at the company you currently work for”. Nope. You really piss me off, and I won’t be seeing an email from you no matter which agency you move on to. I hope you grok the seriousness of that? (this may suprise you, but those of us in the industry DO actually notice when you guys change roles, change agencies, etc)

I simply do not have time for time-wasting muppets who are too damn lazy to bother even doing a simple LinkedIn/Google/Gamasutra/etc search on their “targets” to find out who and what these people are.

Oh, and by the way – I’ve done recruitment, many times, myself. I’ve had to get creative with reaching people, trying to tempt them out of their jobs and into working for my own employers. So I know how hard the hard stuff can be. But I also know how little – how VERY little – time it takes to do the easy stuff. And when you DON’T do even that, it tells me a lot about you. It tells me a lot about the crap you’re sending to your clients. It tells me a lot about how (un)impressed they’re going to be with the drivel you send them. Above all, it tells me that if I *do*, somehow, find the role interesting, then it’s worth my time using my own contacts to get a direct invitation from the company, and bilking you out of your commission.

Actually, I could bilk you anyway, whoever it is. The industry is *that* incestuous that everyone above Junior level “knows someone” (who knows someone, who knows someone else … until you hit the Hiring Manager). So, your whole business is based on the assumption that you make it so much easier for me to work with you that I don’t bother to test my extended network. You’re living on borrowed time from the moment your email hits my inbox. Humour me.

But on the other hand, if you take a genuine interest, and make the effort to find stuff that would actually interest me, you could save me a lot of time and hassle. And then I’d love to work with you on finding and evaluating roles. And (modulo all the above) I’m a pretty forgiving guy, if you give me just a little bit of mutual respect. So you CAN send me random crap that you think might tickle my interest, and I won’t hate you for screwing up. You can even get it wrong every time – so long as it’s clear you are, in fact, *trying*.

So, you know … take the time. It’s for your own good. Really.

HAVE A NICE DAY!