r/shittyprogramming • • Nov 30 '18

Friday Code Confessions

If you have been living with technical debt and want absolution here is your opportunity.

Confess your sins and receive your penance.

155 Upvotes

263 comments sorted by

View all comments

Show parent comment

15

u/[deleted] Nov 30 '18

Guards are used to detect whether an operation can finish before you try to carry it out.

Here's a pseudo code guard (it's python or at least close):

def moveCharacter(character):
    
    if character.isFrozen():
        return

    if character.name == "steve":
        # Fuck you, Steve.
        return

    # All is well. Carry on.
    character.position += 1

I use guards and an exit early strategy like this a lot, because it can make very complicated logic very easy to read and understand.

Sounds like you found a nice pattern on your own. Good job!

9

u/foehammer23 Nov 30 '18

I've been doing this whenever I update our codebase...

I'm good at programming??? I'm not an impostor???

10

u/[deleted] Nov 30 '18

We're all impostors. You're just one of us.

5

u/[deleted] Nov 30 '18

Who knows? Just strive to write easy to understand code first and foremost, then use the profiler to figure out if you need to optimize. The Zen of Python is always pretty good advice.