Hacker News new | ask | show | jobs
Ask HN: What's your simple but favourite coding trick?
1 points by zupzupzup 2230 days ago
When I started programming, 5-6 months into it, I was introduced to a very interesting trick/pattern by one of my mentors.

func(){

    if() {

    } else {

    }
}

can be beautifully converted to

func(){

    if() {
    
     
        return; 
    }

    // Write the else code here.
 
}

The reduced indentation gave a weird sense of relaxation. It was such a neat little trick.

What about you guys. Any common tricks which everyone can use.

1 comments

From your example, an even more concise way can be written using ternary operations in any C-style language:

    int i = 10;
    bool is_10 = (i === 10) ? TRUE : FALSE;