Settings

Theme

Ask HN: What's your simple but favourite coding trick?

1 points by zupzupzup 6 years ago · 1 comment · 1 min read

Reader

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.

catacombs 6 years ago

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;

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection