We use cookies on this site to enhance your user experience

By clicking the Accept button, you agree to us doing so. More info on our cookie policy

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?

Code

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

Marketing an ebook with the 4Ps
Marketing an ebook with the 4Ps

I have seen a lot of threads from indie authors asking for advice with marketing their ebook. If I’m honest, I have never really had a strategy for marketing my books, I just assumed that I could put it out there and people would find it. In the real world, that does not seem to be the case.

Building a landing page for your book
Building a landing page for your book

This post follows on from my last post about using your website to promote your ebooks. The first step of the article explains that you need to make a website, but didn’t go into too much detail. This post aims at explaining how you can build a landing page for your book with Bulma Clean Theme.

How to promote your ebooks with your website?
How to promote your ebooks with your website?

I’m a web developer by trade and a part-time author, so here are a few things that I have done to help promote my books and ebooks using my website and my tech know how from my day job.

How NOT to make a website

How NOT to make a Website

By C.S. Rhymes

From £5.49 or read for free on Kindle Unlimited!

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £4.99 or read for free on Kindle Unlimited!