When implementing a function, don’t make the function do anything until it absolutely has to do it. In other words, make the function be as lazy as possible. The function should procrastinate as much as possible.

Everything that the function does, translates to instructions that the CPU has to execute, which takes time to do.

For example, let’s say we need a function that calculates the area of a polygon, but also opens up a log file where it will store errors if it encounters any. A lot of engineers would open up the file at the very beginning (top) of the function, and then have an if statement to detect/log errors below, like so:

float calculateArea(Polygon p){
    logFile = open("myLogFile.txt") // open file

    // do some calculations

    if (error){
        logFile.write("the polygon has too few points!") // write log statement
    }

    // do some more calculations

}

Instead, don’t open the file until you absolutely have to open it, which is right where you log your error message, like so:

float calculateArea(Polygon p){
    // do some calculations

    if (error){
        logFile = open("myLogFile.txt") // NOTICE, we moved the file open to right before we needed it
        logFile.write("the polygon has too few points!")
    }

    // do some more calculations

}

This applies to anything that the function does. Opening files, opening sockets, initializing variables, literally anything. Don’t make the function do it in advance. The function should only do anything right before it needs it. Lazy functions are fast functions.