Jamaica trevoir.williams@gmail.com

Tips to Write Better Code image

Tips to Write Better Code

Mistakes are all a part of learning. However, there are certain mistakes that tend to continue and need to be addressed. Below is a list of common practices (that can be perceived as mistakes) that developers may continue to do overtime, while not realizing the underlying dangers.

Making these adjustments to your developer habits and patterns will not only be beneficial to you but also to the other developers that have to take a look at your code.

One Function, One Concern

A function should only be responsible for doing one thing. You don’t want to a have one function doing input, output and processing (acceptable in very special circumstances). This will bloat your function and potentially lead to a debugging nightmare.

That being said, it is not a one-size-fits-all situation where you have one function for input, another for output, etc. This can lead to too many functions too. I prefer to think that a function should address one concern (not necessarily operation) at a time. If your function is designed to get a car record, then that is all it should do. There might be something else involved in the record’s retrieval, but the function should not be trying to get a car record and calculate the mileage and find all the drivers (and so on) in one function.

Remove Commented-Out Code

You don’t want to leave old code lying around in your files. Sometimes we would have written a paragraph of code and then had an epiphany afterwards. We then commented it out and started over. That is fine until someone else needs to look at or use the code. Now, no one knows how relevant this commented out code is. I have actually been caught in this trap before, where I commented out a block of code, inadvertently uncommented it and then spent hours wondering why my new code wasn’t working.

Just delete the commented-out code. Even though the code is not in the latest revision, if someone has plans with it the code is still available in version control.

I wrote a post a while ago about some simple rules that you can follow to create good names for your variables. And I can’t emphasize enough how important good variable names are. Most of the time, you are not the only developer working on a project. Other developers need to understand your code as well.

Use Descriptive Variable Names

Simple fix for many problems. If you are storing a number, called the variable ‘num’. If you are storing first name, then name the variable ‘firstName’ or ‘fname’ or something relevant. Please avoid calling them ‘x’ and ‘y’. No one else will understand what x means. To be frank, after a night out enjoying yourself, you will look at ‘x’ and wonder what in the world this variable was being used for.

Do yourself and your team a favour and use sensible variable names.

Use Variables for Preset Values

Having poorly name variables is one part of the struggle. The next part is hard coding certain values in operations, without any indication as to what this hard coded value might be. They tend to call them ‘Magic Numbers’ or ‘Magic Strings’.

Take for instance the following operation:

    $area = 7 * 7 * 3.14;

What do those numbers represent? If you remember you math, then you might see that this formula is for the area of a circle…but what is 3.14? What are the 7s? It is better to place these values in variables with appropriate names, so that someone looking at the code can deduce the relevance of these seemingly randomly placed numbers.

    $pi = 3.14;
    //Raising 7 to the power of 2
    $area = pow(7, 2) * $pi;

The same principle applies to strings. Place preset values in variables so that others can figure out what those values are for.

Enhance your PHP Development Skills by enrolling in Modern PHP Web Development w/ MySQL, GitHub & Bootstrap 4 AND Basic PHP Development with Bootstrap, GitHub and Heroku.

Format Your Code

Messing up the formatting of code is something that is often done by people who don’t have a lot of programming experience.

That being said, most modern IDEs enforce certain formatting best practices. This makes it rather difficult for someone to write messy code. In fact, you have to be very DELIBERATE to mess up the indentation and ignore the auto complete prompts and naming conventions being suggested.

I suggest that you rely on the IDE’s suggestions and allow the suggested formatting to guide your way.

Enhance your ASP.NET MVC and Core Development Skills by enrolling in Learn ASP.Net MVC and Entity Framework (Database First) AND Complete ASP.Net Core 3.1 and Entity Framework Development.

Read More
Three Developer Tips When Using Third Party Libraries in Your Website image

Three Developer Tips When Using Third Party Libraries in Your Website

Some tips to help prevent malicious code from being injected into your web application, through third party libraries.

Read More
C# ADO.Net - LINQ vs SQL Data Reader image

C# ADO.Net - LINQ vs SQL Data Reader

A short comparison of how queries look in LINQ syntax vs using SQl Data Reader

Read More
The Ultimate HTML Primer image

The Ultimate HTML Primer

A succinct walk through of how to author an HTML page

Read More
View MySQL Database Records with PHP image

View MySQL Database Records with PHP

This lesson shows us how to query our database and be able to view the records that have been entered through our registration form.

Read More
Create and Manage SQL Database Views image

Create and Manage SQL Database Views

This article walks us through creating and managing these constructs.

Read More
Create MySQL Database Records with PHP image

Create MySQL Database Records with PHP

This lesson shows us how to use our PHP Data Object library in our PHP application, to insert new records into our MySQL Database

Read More
Connect To MySQL using PHP PDO image

Connect To MySQL using PHP PDO

This lesson shows us how to connect to a database using PHP Data Objects (PDO).

Read More
Design MySQL Database using phpMyAdmin image

Design MySQL Database using phpMyAdmin

This lesson walks us through using the free open source database management tool, phpMyAdmin, to create a new database to be used for our simple PHP Application.

Read More
SQL GROUP BY and Aggregate Functions image

SQL GROUP BY and Aggregate Functions

This article guides SQL developers on how to use aggregate functions to perform quick calculations in SQL.

Read More
Design PHP Form with $_POST Method image

Design PHP Form with $_POST Method

This lesson walks us through handling data from a form after the submit button has been clicked, using the POST method

Read More
Design PHP Form with $_GET Method image

Design PHP Form with $_GET Method

This lesson walks us through handling data from a form after the submit button has been clicked.

Read More
Types of JOINs in SQL image

Types of JOINs in SQL

A quick overview of JOIN commands that Relational Databases have to offer

Read More
Create a PHP Form using Boostrap 4 and JQUeryUI image

Create a PHP Form using Boostrap 4 and JQUeryUI

Bootstrap 4 makes it easy to create beautiful forms for web pages. In this video, we will explore how this can be done in a PHP website.

Read More
PHP Website Layout and Setup using Bootstrap 4 image

PHP Website Layout and Setup using Bootstrap 4

A jump start into creating a PHP application from scratch and setting up a consistent layout for all pages.

Read More
Setup and Use Free Heroku Account image

Setup and Use Free Heroku Account

A short guide on how to setup and use a Heroku account, create an app and publish a PHP application from GitHub.

Read More
Setup and Use Free GitHub Account image

Setup and Use Free GitHub Account

A short guide on how to setup and use a GitHub account, run a few Git commands and publish code to a repository.

Read More
Download and Install Microsoft Visual Studio Code for PHP Development image

Download and Install Microsoft Visual Studio Code for PHP Development

A short guide on how to install and configure Visual Studio Code for PHP Development

Read More
Setup for PHP using XAMPP Server image

Setup for PHP using XAMPP Server

A short guide on how to prep a development machine for PHP Development

Read More
Web Development Trend Traps image

Web Development Trend Traps

Web Development Trend Traps that developers should avoid falling into.

Read More
Be A Web Application Developer in 5 Steps image

Be A Web Application Developer in 5 Steps

A quick set of tips to help you get on your way to being a web developer

Read More
Create Your First React App image

Create Your First React App

Let’s explore the steps to get started with building a React

Read More
SQL Create New Database and Tables image

SQL Create New Database and Tables

A walk through of how to create a new database and tables using SQL

Read More
SQL Operators in the WHERE Clause image

SQL Operators in the WHERE Clause

Get familiar with commonly used to know SQL Operators and Keywords when filtering data with the WHERE Clause

Read More
Relational SQL vs. Non-Relational NoSQL Databases image

Relational SQL vs. Non-Relational NoSQL Databases

A comparison between Relational Databases and NoSQL Databases.

Read More
Working with Relational Databases image

Working with Relational Databases

A guide on how to get started with Relational Databases.

Read More
Bootstrap 4 and Visual Studio Code in 10 Minutes image

Bootstrap 4 and Visual Studio Code in 10 Minutes

A guide on how to author Bootstrap 4 Pages using Visual Studio Code

Read More
SQL SELECT Statement Syntax image

SQL SELECT Statement Syntax

A peek at the SQL Select Statement and the basic syntax

Read More
Helium Jekyll Theme Markdown Examples image

Helium Jekyll Theme Markdown Examples

Markdown samples usefull when you start write your blog 🎉 🎆.

Read More
Helium Theme Syntax Highlight Example image

Helium Theme Syntax Highlight Example

An example post about code insertion into posts.

Read More

Trevoir Williams

Jamaican Software Engineer and Lecturer.