Skip to main content

Tips on Writing Readable Code

Code readability may be defined as the convenience with which the source code is understood and modified. Code readability is of vital importance when maintaining larger legacy codebase and working in teams. If the code is easy to read, it would be easy to debug, refactor, enhance and maintain, thereby reducing the lifecycle cost of software product. As readability directly impacts software quality, researchers have proposed adding a separate phase during development focused on improving code readability. Although, code readability is subjective to human judgement, static analysis tools can provide a fair estimate on various factors impacting readability such as indenting, naming and commenting. As a programmer, follow the tips provided below to create self-documenting software program, which can be understood conveniently by other programmers during it's lifetime.

Code Optimization Tips for Increasing Execution Speed of a Software Program

Code Optimization Tips for Increasing Execution Speed of a Software Program

CPU (Central Processing Unit) is the primary component of a computer system, which executes the basic arithmetic, logical, controlling and input/output (I/O) operations specified by the instructions in a computer program. CPU utilization is an estimation of the CPU used by a particular task, which is often expressed in clock ticks or as a percentage. In embedded computing world, one of the critical design decisions include customizing the software for better CPU utilization. 

As a programmer, code optimization is one practical way to improve the CPU utilization. Remember that you should first write correct code and then optimize. Efficient programmers spend more time optimizing code as compared to writing code. This post lists some established techniques to optimize the source code for a better CPU utilization. 

1. Select Efficient Algorithm 

Select a computationally efficient and appropriate algorithm for your code. This means that the algorithm should not use a lot of CPU resources with the increase in input size. The time complexity or time efficiency of an algorithm is typically expressed in Big O notation. As an example, if you need to write a program to sort elements in an array, you may prefer Quicksort algorithm having average time complexity in O(n log n). As an other example, if you have to search for an element in a sorted array, you may prefer Binary Search having complexity in O(log n).

Big O Complexity Chart
Big O Complexity Chart

2. Optimize Loops

Huge improvement can be achieved in execution time of the software by reducing the number of loops, minimizing the iterations or reducing the number of code statements inside the loop. Following are few of the loop optimization techniques which are effective for better CPU usage:
  • Move all the code statements outside the loop body, which can be moved without changing the overall program logic.
  • Combine two or more loops in one single loop where possible.
  • Unroll the loop be replacing iterations with sequential statements. Loop unrolling increases the program speed by eliminating loop control and test instruction.
  • Remove expensive function calls from loop condition and control statement. Reverse the value assignment from loop condition as it is faster to test if something is equal to zero than to compare two numbers.
  • Optimize the code in loop body by finding faster alternates to expensive operations. Eliminating expensive operations inside the loop can speed the program to a great extent. 
  • Split the loop if it contains conditional statements to assign values on different index ranges.
  • Remove any dead code from the loop. Generally, the loop condition variable is redundant and it can be replaced by some other variable.
  • Try to minimize the number of iterations of a loop and avoid nested loops by finding an alternate way to implement your program logic.
Reduce Code from Loop Body
Reduce Code Inside Loop Body

3. Optimize Functions

You can optimize function calls for improving execution time using the following guidelines:
  • Remove function calls from inside loop body, as they may be expensive.
  • Use inline functions for short functions to reduce overhead.
  • Minimize the number of local variables in functions, if possible.
  • Reduce the number of function parameters, if possible.
  • Do not define a return value from the function, if you do not need it.
  • Passing objects by reference is more efficient as compared to passing by value.
  • If the function returns a value, optimize it in a way that most common return statement comes early in the code.
  • If a variable / object is required sometimes (in an if condition), declare it when necessary, not at the beginning of the function.
  • Avoid recursive function calls, replace with alternate implementation like iterations. 
Remove Function Calls from Loop Body
Remove Function Calls from Loop Body

4. Optimize Conditional Statements

Conditional statements and branches are an integral part of every software program. You can optimize your conditional blocks for CPU performance by following the under-mentioned guidelines:
  • Replace the long if...else if...else if...else conditional blocks with switch statement.
  • If switch statement is not possible, optimize the if block by placing the most common condition at the beginning. 
  • Avoid multiple concatenated if conditions using && and | | operators, if it is possible to evaluate the condition using fewer expressions. 

5. Simplify and Optimize Math

You can increase the execution speed of your software program by simplifying or optimizing the math and equations. Consider the following when thinking about optimizing math:
  • Simplify equations on paper, as the terms may cancel out sometimes.
  • Consider the appropriate variable type for math calculations i.e. if you need floating point calculations.
  • Rephrase math to eliminate expensive calculations. As an example, if you repeatedly divide a number by x, consider calculating 1/x once and multiply it with that number. Division is more expensive than multiplication and multiplication is more expensive than addition. Consider if you can replace expensive math functions like sqrt( ) or pow( )with alternate implementations.
  • Consider if you can compute values incrementally in a loop or need to compute from scratch every time.   

6. Optimize Arrays and Data Structures

You can improve the performance of a software program by using appropriate data structures. Consider the following when thinking about optimizing arrays and data structures:
  • Multi dimensional arrays and data structures are stored in one dimensional memory. Accessing data from the array in a sequential fashion can speed up the code.
  • Do not use arrays or data structures if not required and the problem can be solved without using them.
  • Depending upon the data requirements, one form of data structure may be faster than the other. Select the appropriate data structure for your software program. 
Various Data Structures in Java
Various Data Structures in Java

These are few of the guidelines for optimizing the CPU usage of software programs and making them faster and time efficient. Note that sometimes optimizing for speed conflicts with readability. So you should go for speed optimization when absolutely necessary and prefer readability of your code over anything else.

Comments