DEV Community

Discussion on: Coding Best Practices, Chapter One: Functions.

Collapse
 
jprochazk profile image
Jan Procházka

Personally, I would put throw statements at the start of a function to avoid large indentation. It really improves readability.

Instead of

func doThing(x: int) {
    if(x <= 10) { 
        print(x)
    } else {
        throw Exception("x is too large!")
    }
}

it would be

func doThing(x: int) {
    if(x > 10) { 
        throw Exception("x is too large!")
    }

    print(x)
}