Tips for developing WebApplications

So it been awhile since I sort of promised some tips on developing web applications fast. This post contains my five generic tips, which may apply to you (but then again – your mileage may vary). I’ve tried to abstract the advice and not stick to PHP development here, though it’s by far the world I know best.

1. Spend time on the data model

Too often a data model is driven by code, not careful thinking, and this often causes problems has the web application develops and expand over time. When developing a site, which needs data stored in a database (as most web applications tend to do), do try to spend some time on the data model – which tables do you need, what fields should the tables have, are the naming logical and consistent, what constraints and limitations does the data model offer.

It’s often far easier to refactor code and functionality than it is changing the database tables and fields, and it can be a tremendous help while maintaining an application if you clearly understands the data its working with.

2. Get the basics working first and fast

It often takes a while to get a complete web application finished. Help motivating yourself by getting the core working first and then add features and frosting once it’s working. By having a working skeleton, I usually find it much easier guess how much work needs to finish the various features than I do guessing the work needed to complete a section (ie. admin interface) on the application.

3. Think performance and security from the beginning

Remember to think of performance and security from the very beginning. Sometimes performance – or security – may cause you to layout the data model in a certain way or structure the application a certain way. While you may be able to add performance and security during refactoring, it’s often much harder than it is keeping both in mind from the very beginning.

4. Don’t fix the future; focus on the current needs

I don’t care about the next version. I really don’t want an infrastructure in place, which can help me build the future versions of the application. I want the features and infrastructure in place for the current version I’m working on – and frankly the features in the next version has a tendency to change before I’m ready to develop them.
Too often future-proofing an application clutters the code and too often the clutter doesn’t ever come to any good use – it’s just noise once we get to the real future features of the application.

5. Naming matters

Do think of what names you use on functions, classes, variables and database tables through out the application – and choose something meaningful. A variable named $x doesn’t hint what it’s used for. Help yourself (and other looking at the code) by hinting the uses of various things by choosing meaningful names.

Your mileage may vary

It’s always fun to read articles with tips and tricks by other developers and see how they figure “best practices” are handled. Most developers do seem to thing they observations and practices are easily adopted by anyone, and should be accepted without any argumentation or reasoning behind the advice.

One of the nice examples of this, was a story called “5 tips and tools to develop php applications fast”, and while it may apply to the web applications developed by the author, it’s one of those where I question who is supposed to be the audience. All 5 tips are often found in many textbooks and other stories on the net, and in my experience they are all wrong (more or less naturally).

1) Use a MVC framework – it’s an industry accepted standard.
MVC as an idea may be accepted, but there are many ways of implementing the MVC – even with the various PHP frameworks. If you know the MVC pattern it may be an idea to look at the frameworks supporting it, but don’t get carried away.

  • If all your old code is non-MVC code and you don’t have any plans to MVC-refactor it, it may cause you maintenance overhead to try a completely different style  on a single project/web application.
  • If you aren’t used to using a (MVC) framework you need to invest time in learning it.

Do keep an eye on the overhead caused by the framework. If you web application has a heavy load and the framework causes 5-10% overhead, it may a bad idea.

2) Ajax Frameworks
Great idea.. but to utilize the power of an ajax framework, you need to learn how to use it. Also Ajax isn’t the holy grail of the internet. Sometimes it’s cleaver to use it, other times it isn’t. By using plain old HTML and reloading the page from time to time, it may be easier to debug the application.

Do also notice, that writing great/professional quality Javascript often requires just as great skills as writing professional PHP.

3) IDEs
An IDE – Integrated Development Environment – can be a great idea if you know your way around it, and use it regularly. An IDE is – like any other tool – something you need to invest time and resources into, if you want something back – and especially if we’re talking a beast like Eclipse-based tools.

4) Database creation/management software
Let me skip comments on database creating management tools. I’ve never really used them and don’t know if they’re a pain or a benefit. I have often seen though, that too easy access to the database, leads to sloppy database design (or at least something, that isn’t thought through).

So far it’s been my experience, that people who knows the command-line clients to databases, often has invested much more time in thinking their data models through and often spend a little more time considering the table layout, the field definitions, indexes and other important aspects, than those who use a nice graphical interface.

5) Object Relational Mapping
Object-Oriented programming isn’t a holy grail, and most people doing OO programming (in my experience) doesn’t genuinely do object-oriented programming – instead they use the OO-facilities in the language to slap functions together in a class.

There’s nothing wrong with procedural programming as such, and trying to force object-orientation into your code – and even abstracting it away through Object-relational mapping, has often lead to slow performance and odd database designs. If you know what you’re doing great, but there’s no free lunch, and trying to utilze ORM without having a pretty solid grip on what’s happening and why, rarely does anything good to your productivity.

I’m quite sure for some developers, using some sort of object-relational mapping might be a gain in programmer productivity, but do keep in mind, that if you’re developing a site with massive traffic, the programmer gains may easily be crushed in added server resource requirements – spending a few minutes extra optimizing and tuning applications, may in some cases save hours and hours of cpu cycles once the solution is deployed.

Getting back to the article, I’m sure the advise is well-meant and thought through by the author, but do always – as with all advise – think it through and see how may apply to your adoptation. In may daily development some of it is quite terrible (as I often work on a site with thousands if not millions of pageviews every week).

I’m wondering what my 5 tips would be for developing PHP applications fast would be. Check back in a few days, and there may be an answer – which may or may not – apply to you.

Code archeology

I’ve been spending quite some time the past days digging around in old code (3+ years old with no or very little changes). It’s quite fun noticing what efforts pay off and which doesn’t when you go back to make chance to old stuff. I’m sure my observations aren’t generally applicable. We’re web developers and are keeping web platform (several pretty big websites on a common codebase) alive year after year.

Proper and sane variable- and function names pays off instantly. Incredibly short or misguiding function and variable names doesn’t. While it may be fun naming functions “doMagic” or likewise, chances that you – or someone else maintaining the code years after will probably not appreciate it at all.

Reasonable commenting on functions and tricky code parts often come to good use, but too much commenting is distracting. Some is good, and less is often more valuable.

Version control logs can be quite valuable, if they’re descriptive for the change made. Too often though, was the comment “misc. bug fixing”, “problem solved” or something which years later was of no use or value at all.

Documentation kept apart from the source code seem to have none or very little value. Not once have I bothered to look in the wiki-documents or any other documentation which was in the code. In the few instances I did, minor changes and bug fixes had changed the code to such an extend (while not updating the documentation) that it was misleading at best.

The only cases where the documentation apart seem to make a positive is database diagrams and descriptions. They do seem to have value.

What is twitter?

One of the hottest sites on the web for more than a year is twitter, but what is Twitter? – I’ve tried a few times to explain it, and while it may be a fun task, it has often become quite a mess. This is an attempt to capture the most successful explanation of twitter.

The core of Twitter is a combination of three different characteristics:

  • Twitter is like a blog – An author publish content. It may be personal, it may be themed, it may be interessting – there are no set rules for the contents except those set by the author.
  • Twitter is like an SMS – There are a 140 character limit on each piece of content. If you need more, you need to split contents in several “twits”.
  • Twitter is a network – It’s no just a website. Through build in services and APIs you can connect with twitter through SMS, desktop clients, Instant messaging and many other ways. Besides a technical network, it’s also a social network where you can follow other interesting users, communicate with other users (private or in public).

That’s pretty much the core.

There are a lot of other features stuff you can do, and while many has public feeds, you can even choose to keep you twittering private and only allow people you authorize to see you contents on twitter.

Another interesting use of twitter is when applications start interacting with it. When I post contents on the WordPress-based blog, a twit is automatically posted announcing it and thus twitter may in some extend be an alternative to my RSS feeds. See also iWantSandy of an interesting application interaction through twitter.

If you want to follow my ramblings on Twitter go ahead, they’re public.
If you have a better definition of what twitter is, please post a comment.

Code exit strategy – 3 tips

It seems many developers get stuck in the same systems maintaining the same code for years and years. While it may be a common phenomenon there are a few things you can do as a developer to avoid being trapped in your own code forever.

First make things readable.

While your brain may be wired to a system of only using single character variable and function names or naming global variables after your cats, no one else will get the system. Try to make all names as logical as you possible can. Making it easy to read the code – not only as syntax but also in the names used in classes, functions and other objects in your source, improves the likelihood that someone else can figure out what’s going on and inherit the code someday.

Second make the code do what it supposed to.

Sure it may be cool making the system a generic all-purpose Swiss army knife of software, but preparing the code for all sorts of functions with aren’t used and aren’t needed, it only cause confusion of the next developer trying to move your code forward. Objects and class hierarchies may be suitable in some cases, but sometimes it’s more wrapping and declarations than actual code – and you’re probably on a wrong path.

Third provide in-line documentation.

Documentation are like a ghosts – it probably doesn’t exist. Making pages of documentation is probably a neat way to spend time if your on billable hours, but if you’re a regular developer, documentation is – at best a necessary evil. Do try to provide brief relevant comments, when something complicated or “not too obvious” happens in the code. Don’t place small novels in the source, but short precise guidelines.

While the above tips doesn’t guarantee your freedom to move on to new exciting projects, it surely improves the odds dramatically when you code is inheritable and accessible by another developer.

Nortel VoIP: It just feels wrong

I have a VoIP phone on my desk. It’s part of the Corporate VoIP solution, and in most cases it works pretty well. One of my favorite features is the ability to act as a meeting phone with the speaker on. It usually works great, but there is one thing with the speaker feature, which just feels wrong – to turn off the speaker, you press the hangup buttom. Please fix this issue in a future firmware update, and allow the speaker buttom to act as a toggle that can turn the speak both on and off.

PHP Coding Standards

If you’re part of a larger team developing (and sharing) code, it is a pretty good idea to have some common standards. In the PHP work, such a set of common standards include the PEAR Coding Standards. While it may be a challenge to agree on standards, it can be done – the hard part is following the standards.

One excellent tool to help you check for standards compliance is the PHP CodeSniffer. We’ve had it running for quite awhile and it seems to work as advertised. We’ve made our own (“TDC”) coding standards which is a variation over the PEAR coding standards, and checking if you follow them is as simple as running a simple command line.While it doesn’t enforce following of the coding standards, it does make it a no-brainer to check if you do follow the common standards.

The output from the codesniffer could look something like this:

mahler@mahler$ phpcs --standard=TDC Example.php

FILE: Example.php
--------------------------------------------------------------------------------
FOUND 44 ERROR(S) AND 2 WARNING(S) AFFECTING 38 LINE(S)
--------------------------------------------------------------------------------
[LINE 2] ERROR: "require_once" is a statement, not a function. No parentheses are required.
[LINE 5] ERROR: "require_once" is a statement, not a function. No parentheses are required.
[LINE 7] ERROR: Space before opening parenthesis of function call prohibited
[LINE 9] ERROR: Space before opening parenthesis of function call prohibited
[LINE 20] ERROR: Line indented incorrectly. Expected 0 tabs but found 1.
[LINE 49] ERROR: Line indented incorrectly. Expected 1 tabs but found 2.
[LINE 51] WARNING: Equals sign not aligned correctly. Expected 1 space, but found 2 spaces.
[LINE 59] ERROR: Space after opening parenthesis of function call prohibited
[LINE 61] ERROR: Line indented incorrectly. Expected 2 tabs but found 3.
[LINE 61] ERROR: Expected "while (...) {\n" found "while(...){\n".
[LINE 63] WARNING: Equals sign not aligned with surrounding assignments. Expected 3 spaces, but found 1 space.
[LINE 74] ERROR: Line indented incorrectly. Expected 1 tabs but found 2.
...

Footers – not just the end of the page

On most websites – including this one – the footer is the boring place where you (usually) place all the pocket lint, which didn’t make it into the page anywhere else. It usually have a copyright notice, links to a site map and other stuff which may be important, but not interesting (speaking in very broad terms).

Today I came across an article on footers with great usability. Footer Usability? Yes, and from a blunt disregard and ignorance, I’ve discovered that the site footer is an excellent place to place a lot more effort.

The footer may have been important as an “end of page marker” in many designs, but using it as an active area to promote other parts of the site, seem to be a new trend from the late part of 2007.

I really like the footer usage on farfromfearless, where especially the flickr photostream adds a bit of life to the page, and ProBlogger.net, where the categories listing might act as a miniature site map.

While using the footer could be a very smart move, don’t over do it. Bad examples on footer misuse include popsugar, where footer eats up more than 30% of the total page length, and engadget, which  seem to claim a lot of screen space – without any interesting content (just headlines from their sister sites).

Quality of code

Not  all code are created equal. Some pieces of code are more important than others. The code that powers critical parts of a nuclear power plant is hopefully of a much higher standard than the code behind this site, but how do you recognize which quality of code you should aim for when developing websites?

A website may not have one set level for all content – usually the various pieces which make up the site (no matter if it be classes, files or functions – in PHP). On the level you’re looking, let me suggest, for each piece you judge the required (minimum) quality on two simple parameters:

Business/Real world value: What’s the consequence if the code breaks? are you loosing money? or is it “just” an embarrassment”? The higher the stakes the more effort should be put into the code.

Scope of impact: How large a part of your website breaks if the code fails.

While you may not have a precise scientific way of measuring the two, I’m quite certain, that most developers have a pretty good gut feeling on both, and ought to be able to set a suitable quality level using just that.

Is it so or are there better ways to find acceptable quality levels?