A guide to writing good code

Writing/Debugging/Executing code becomes a lot easier once you follow a few basic optimisation guidelines. This becomes especially apparent when working in a team since the need for consistency is paramount for ease of problem-solving.

Good coding techniques revolve around optimization of 3 simple things

  1. Time taken for your code to execute
  2. Robustness (The ability to handle different albeit practical and observable use cases)
  3. Time taken to write the code

While the above is the correct order for an end product, the order changes when one is in the process of developing the product. The order for the latter is

  1. Robustness (The ability to handle different albeit practical and observable use cases)
  2. Time taken to write the code
  3. Time taken for your code to execute

As more and more use cases are thrown in the mix, the time taken to write code increases. For me, in the process of chasing efficiency without compromising quality, I encountered techniques that not only reduce the time taken but also reduce the processing time, killing two birds with one stone. Written below are my own set of commandments that have reduced the development time down by 30-40% and frankly, have made my life much simpler.

1) Write complex SQL queries instead of performing operations on the client’s device

SQL, apart from the usual arrangement functions (ORDER BY, GROUP BY), offers basic math operations that can be carried out, eliminating the need to run these operations on the client’s device. Math examples such as calculation of sum, average, maximum, minimum in a column and arrangement examples such as Inner Join and other variants populate the dataset, leaving you with only relevant fields. They also cut down on the number of queries called.

For Example: If you have different tables for a user’s email id and password, use an INNER JOIN which links the common user id (present in both tables) to get a single dataset. This dataset houses both these values, instead of getting two separate datasets. While, such a technique, will allow you to individually check which password corresponds to the user Id, it will eliminate the need to write/debug code for functions thereby cutting down your development time and reducing server dependency in a poor network area.

So, incorporating complex SQL queries will work faster than your code will and exploiting them as much as possible is a word to the wise.

2) Use Template functions

Writing a single function which performs multiple similar operations can help cut down your development time considerably. For instance, let us assume you want to get a certain row of a certain table. You can write a SQL query for the same but when you have to repeat the same operation for different rows across different tables, it becomes a tedious process.. It’s better to write a single function which will accept the table name and row id as parameters and will return the row as an array. The use of template functions makes the function extremely flexible and makes error handling easier.

So one for all is a lot more efficient than one for one.

3) Common Header/Footer files

I realised the dire need for common header and footer files on my second project when I had to change the CDN for a CSS file and the website had 28 pages. Writing out your header and footer HTML/PHP code in a header.php or a footer.php format and simply using include(“header.php”); across all pages ensures uniformity. Making use of such common header or footer files will allow the changes made to be reflected across all pages at once. Another advantage is that PHP variables declared on the parent file carry over to this file so continuity is not broken. The same can be done for common meta tags, css files, javascript files, configuration files which have database access parameters. ( Do make sure that you don’t end up loading extra and unused js/css files your page.Also, take special care of the mysqli connection variable which acts erratically at times.

Make use of common header/footer files and ensure uniformity.

4) Use a design framework

Plenty of design frameworks like Bootstrap, ZURB, Pure.css, Semantic UI etc.exist today. These design frameworks make website creation faster and cleaner. They not only offer grid-based systems but also offer a plethora of customization options for text, images, menus etc. All these are designed to ensure you have a good looking website in a matter of hours.

For instance to make ‘a’ tag 25% bigger, have it red colored with a margin of 16 px from the top


<p class="lead text-danger mt-1">Sample Text</p>


<p style="font-size: 125%; color: #cc0000; margin-top: 16px>Sample text</p>

We see that Bootstrap code is much short and is easier to read when collaborating with different members in a team.

If you wish, you can purchase a theme which uses the above frameworks and makes your life even simpler by already having a design in place.

So instead of reinventing the wheel, use the wheel to roll off the production time faster and get cracking.

5) Use Google reCaptcha

This is should be a no brainer for all your user forms. This will help cut down spam from the first day. Using reCaptcha by Google also ensures that your server is not bogged down by a traditional captcha service which will utilize your server to generate captcha code.

Nobody wants bots replying to your contact forms, its an omen; and Google reCaptcha is your holy dispeller.

 6) Have a proper website flow

Firstly, it is important to identify why a user might visit your website and what their ultimate goal is. Once determined, you must then highlight the steps which the user might take in order to accomplish a user-friendly website. You must ask yourself when developing a website, should the users be allowed to backtrack on their journey? What will happen when they branch off to a different section while on a path? All these questions have to be kept in mind and be answered. Once you have the answers to the above questions a logical flow is established. For instance, if a user is viewing your services offered, then it makes more sense to have a section below which has a contact us form than a list of your company executives below.

Any good developer must know thy customer for they expect you to do so.

7) Sign Up for Google Cloud Console / Amazon AWS

Google and Amazon are offering free credits for their Web Services Console when you sign up with your email account. These services range from Deep Learning to establish LAMP stacks which can be deployed on the click of a button and give you full shell (terminal) access. Executing commands and solving commands on the terminal itself is much faster and also gives you ground level knowledge of your server. Executing simple commands (like zipping a big folder) is also much faster. Other practical examples include learning how to set up a free security certificate for your domain through Lets Encrypt in a matter of minutes, handling multiple domains on the same server, setting up automated tasks (chron jobs), setting file permissions. It should be noted that hosting services will also offer the same service but unless you have a Virtual Private Server, there will be some restrictions.

Using the terminal does more than making you feel cool. It actually is really powerful. Thanks Google/Amazon.

8) Meta tags and schema

Meta tags and schema play a pivotal role in making sure your website is discovered by people who will be interested in your offerings. They help you describe each page with specific keywords which are then crawled by Search Engine spiders and make your website visible corresponding to the user’s query. The schema will help you provide essential information like open hours, popular services etc on the search page itself, thereby cutting down the number of steps between a user’s search online and them availing of your services/products.

Make use of Meta Tags and Schema to ensure your pages are visible to the correct pair of eyes.

9) Optimise your website for all mobile devices

Websites visitors on mobile devices have risen exponentially and continue to do so. Therefore, it is important to make sure your website performs well and looks good across all screen sizes.. Simple measures include arranging your website sections in columns instead of rows (as per the case of desktop), removing elements which have a horizontal overflow (makes users scroll horizontally) and optimizing images for your mobile. The last one is especially important as a smaller sized image works well on a mobile screen and it also reduces the loading time by a lot.

Pay attention to the mobile trend, it’s here to stick. You might as well as cater to their needs if you want your website to stick

10) Exercise your body

It’s easy to get lost while coding and not notice that you haven’t changed your position in the past 3 hours. It’s important to get up every hour and take a break for 5-10 minutes to give your body some respite and your mind, a breather. Back and hip problems have increased in the younger population since our jobs started being more and more sedentary so it’s important to take a time out and move your body whether that is by playing a sport, doing yoga, working out or doing any other activity that promises physical movement.. Another small detail is to make sure your monitor is at an eye level. Invest in a good chair and (if required) a laptop stand paired with an external keyboard to maintain the best posture possible while your fingers code away.

Don’t let your body die before your mind

Photo by Omar Flores on Unsplash