Swarm - Website Design & Digital Media Agency

Welcome to the Swarm website! Swarm Media is a digital design agency based in Leeds & York in the United Kingdom. We specialise in many areas of digital design including website design, graphic design, web development, Flash animation, search-engine optimisation and web hosting. If you want quality digital design solutions, then look no further.

Blog / Updates

Spaghetti Code

I've recently gone over a few related topics to do with programming and/or web development. The pejorative term "spaghetti code" gets thrown around a lot, however I think many of the people who use it don't really understand what it means. Some use it to describe where HTML, PHP, SQL, CSS and Javascript are all intermingled; but this isn't (necessarily) spaghetti code. Similarly, others use it to describe the lack of a layered architecture or framework (such as MVC) where particular functionalities are separated; but this isn't (necessarily) spaghetti code either.

Spaghetti code specifically refers to code that is a tangled mess, meaning that it is incredibly difficult to follow and understand. For example, a script might have random includes, functions and objects scattered throughout it which make it extremely difficult to follow the code through in sequence, and nigh on impossible to continue working and building upon. Such an issue is often made worse by weak or non-existent documentation.

A lot of developers seem to think that avoiding writing "spaghetti code" means having to adhere to a particular architectural pattern, framework, or methodology. I strongly oppose this view because part of what makes web development and programming in general enjoyable is doing things your own way and coming up with your own solutions. Whilst programming obviously has a wholly logical foundation, that doesn't mean there isn't room for artistic license. There are a lot of supposed "best practices" which are questionable at best; one example being the non-mixing of different languages mentioned above.

Whilst I do not suggest one should carelessly mix three, four or more distinct languages together in an unreadable mess, I would argue that it is completely possible, and often beneficial to keep PHP and HTML (for example) intermingled without creating "spaghetti code". Abstraction and compartmentalisation, whilst both powerful and useful, can make things more complicated or bloated than they need to be. There are ways in which code can be organised and structured as to make it perfectly understandable and cohesive whilst at the same time mixing languages together (or doing other things which would make certain purists cringe).

Since the whole point of architectural patterns and coding conventions is to make things easy (or at least easier) for the developer, it doesn't matter if pattern X or convention Y is used, so long as the code is logically structured and consistent in both structure and style. If a developer finds it easiest to keep everything together, as to closely reflect the order in which things get processed and sent to the browser, then there is no reason to change. There seems to be a tendency to use particular coding styles or frameworks simply for the sake of using them, rather than because they offer any real benefit to the developer.

In summary, spaghetti code is very bad, but some of the things which are often wrongly labelled spaghetti code, are not bad at all.

Free Quote