How to Optimize Your Code

Table of Contents

Early in my software development career, application optimization was always a knowledge that seemed very hard to attain. As time progressed I came face to face with different situations where optimization was necessary in order to improve performance. I was forced to think deeply about the computational cost of the code that i was writing, which in turn opened my eyes to different methods of optimizing my code.

It is worth noting that this blog post will not highlight how to find the piece of code that requires optimization. Tools such as a profiler are used for that purpose.

Reduce Number of Loops

The first method of optimizing your code is to reduce the number of loops or iterations that are being performed.

I have come accross cases where it was possible to perform several operations on a dataset in a single loop instead of creating separate loops to calculate each.

Pre-Fetching Data

In some situations a dataset is needed to be used throughout the program, it is important to fetch the data only once and reuse the data.

This only applies to datasets that are constant. The same method of optimization might not be suited for dynamic data.

Bulk Fetching of Data

Another method of optimizing your code is to limit the number of calls being made to the database. One example is to fetch data in bulk instead of fetching it when it is needed.

Let’s imagine a scenario where a program requires a set of items belonging to a certain category. You can fetch this data by calling the database whenever the item is needed. An efficient method of doing this would be to fetch all the items belonging to that category once and create a map where the key is the ID of the item and the value is the item itself. By doing this we have limited the number of calls being made to the database and created a more optimal code.

Allowing Database to Handle Complex Queries

Databases have an underlying engine which optimizes the performance based on the queries being submitted. In a situation where data needs to be fetched by linking two different tables it is best to allow the database optimize the operation.

An example of this is when you are trying to fetch information about the categories of items based on users who have bought them. One way of implementing this is the following:

  • Fetch sales data
  • Iterate over it and filter by the user infomration
  • Save the categories of items bought by those users
  • Fetch the category information
  • While this method works, it is performing a task which would be best delegated to the database engine. A more optimal solution would be to create a single query which performs all the above task using database joins.

Fetching Only Necessary Data From Database

There are times where a list of data belonging to a column is needed in order to perform an operation. One way to do this is to query all the columns of the table and then loop through the returned result and create a new list consisting of the column that we require. Another method is to only fetch the column that is needed as a list. This reduces the number of loops that are needed to be performed and improves the efficiency.

Ending Notes

This blog post serves as a great introductory point for those wishing to learn how to optimize their applications and it has been written in hopes of helping those struggling like myself early in my career.

While not all methods of application optimization are highlighted here, it presents an important set of tips which might not be available to those just beginning their software development journey.

It is worth noting that this blog post will not highlight how to find the piece of code that requires optimization. In those situations tools such as a profiler are used.

Related Posts

AI isn’t replacing developers, it’s empowering them. Explore practical ways Large Language Models (LLMs) can boost your software development productivity, from generating data and code to assisting with testing and problem-solving. See real examples and prompts I use daily.

Read More

Understanding technical topics can feel overwhelming, but it doesn’t have to be. This post simplifies backend applications, databases, and architectures like monolithic and microservices, making them accessible for non-technical professionals.

Read More

Recently I had to migrate a React Native personal project which was written in JavaScript to TypeScript. This Blog post will high light the different areas and the changes that i had to make.

Read More