An "If" is just an "If", but ...

An "If" is just an "If", but ...

ยท

1 min read

This is so simple and make sense, but I just realized this today ๐Ÿง ๐Ÿง 

Imagine that you have these two functions

func a() -> Bool {
    sleep(10) //simulate a very a heavy function
    return Bool.random()
}

func b() -> Bool {
    return Bool.random()
}

and then if new need to test both functions

if a() && b() {
    // do something
}
// the code will take 10s to execute, because a()

In this case you should do

if b() && a() {
    // do something
}
/* 
 * 1) if b() is false it will not execute a()
 * will take 0s to execute
 *
 * 2) if b() is true, then will excute a()
 * will take 10s to execute
ย