Organising Your Code

Published: Aug 10, 2016 by C.S. Rhymes

Its easy to start writing code without thinking about the organisation, but take some time to plan and it will reward you later. Sometimes a new project is so exciting you just want to start coding and making things work, thinking that you will sort out the structure at a later date. After all, who cares as long as it works, right?


Coding is a creative process and once you are in your flow you don’t want to interrupt it. A couple of files can suddenly grow into a few files, followed by a couple of JavaScript libraries and then some css files, images and all of a sudden you find yourself in a right mess.

Spending some time sorting out your file structure can save you a lot of time, as well as your sanity in future.

File Structure

A really simple step you can take before starting is to create a range of empty folders in your project folder. You can start with a folder for JavaScript, a folder for CSS and a folder for images. Simple but effective.

This simple step causes you to think about where your files will live and how you are going to categorize them. For example, if you are writing a blog, you might want to create a posts folder and put all your post files in there. It will make you think about where your files will be and make your url paths more logical in the end.

The next step you can think about is the libraries you want to include. It used to be simple to download a JavaScript file and then save it in the JavaScript directory, but modern libraries can often be split into many different files.

You don’t need to manually copy and paste these files in anymore. Use a package manager.

Use a package manager

A package manager or dependency manager takes care of importing all the files you need. There are tools such as npm or bower that can be used to pull in packages into your project for you to use.

There is a bit of work to be done before you can use the package managers, as in you may have to install them on your system first, but once you have installed them once you can reuse them on all your projects.

You start out by initialising the package manger in your project and then you can start adding the dependencies. The package manager will then go off and install the packages into a directory for you.

The other big benefit is that the packages are under version control. Packages are updated with bug fixes and enhancements, so to make the most of it you just need to run the update command and the packages will be automatically updated.

Use a methodology

Another tool in your tool box is methodologies. The traditional way to write a web page was to write the HTML in the index.html file, along with some in line styles and then a bit of JavaScript spread throughout the page. Then you may start including a programming language such as PHP within the same file. This quickly becomes a nightmare of unmaintainable and non reusable code.

The world has moved on from this and now people tend to think more logically about the way they code. There are many different methodologies that fit different circumstances so its best to do some research to see what suits your project. A common methodology for organising code is MVC (Model, View, Controller), where each part of your code performs a specific purpose. I’m not going into this in much detail now, but there is a wealth of information already available online.

Use a framework

Another way is to start with a framework, such as Laravel for PHP, or Foundation for frontend. These frameworks are a great starting point and mean that you have a place to start from and an existing folder structure to follow.

Frameworks are useful in the fact that they provide a lot of commonly used methods, but just research how they work first as, to get the most benefit, you need to work in they way they want, otherwise you will find yourself trying to undo the work they have done to make it work your way. Then there is no point using a framework.

This blog is created using Jekyll, hosted on GitHub pages. Its a good system to use as it provides a lot of basic functionality, such as liquid templating and support for pages and posts, but you remain in control of the frontend HTML, JavaScript and CSS.

I’ll say it again, use the benefits of a framework to your advantage.

All organised now?

Hopefully you will spend a bit of time planning before jumping in to your next project, even if it is a simple sketch on a bit of spare paper. You will thank yourself later, trust me!


Latest Posts

Building a directory system API in Laravel
Building a directory system API in Laravel

Sometimes it’s difficult to know whether to buy a system, use an existing open source system, or even build your own system. Each approach has its own risks and rewards. As a developer sometimes you get the opportunity to try things out and build a prototype to see what is possible. I recently got this chance and built a prototype for an API.

Using PHP Enums in Laravel to store additional information
Using PHP Enums in Laravel to store additional information

Sometimes it’s difficult to decide where to put code, trying to figure out where it really belongs. Laravel has a predefined folder structure that you can keep to for most scenarios, but I wasn’t sure whether the settings should be kept in a config file, a class, or somewhere else. In the end I decided to use an enum.

Upgrading a Laravel Vapor app to PHP 8.2
Upgrading a Laravel Vapor app to PHP 8.2

I recently updated a Laravel app running on Laravel Vapor to PHP 8.2. I was a bit nervous about the upgrade but it went smoothly for me. This article goes through the steps I went through to upgrade, from local development environment, dependencies, testing and deployment to vapor.

How NOT to make a website

How NOT to make a Website

By C.S. Rhymes

From £8.99

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £2.69