3 Minutes with Kent

if statements and "We'll cross that bridge when we come to it"

05.27.2021 - By Kent C. DoddsPlay

Download our free app to listen on your phone

Download on the App StoreGet it on Google Play

Good morning or afternoon or evening friends, I wanted to talk today about

a saying that my wife has I think when my kids grow up they're gonna

remember my mom always used to say this and and so what she says is we'll

cross that bridge when we come to it now, of course this isn't like an

original saying but she just says it a lot and what she's saying is that

she doesn't want to make these decisions right now or use up the mental

energy to decide whether or not.

She wants to let the kids do a certain thing or whatever and I've taken to

saying it myself, um and what I'll often say is if you want me to answer

now, I'll just say no so why don't you wait until I'm able to think about

this but it was I was thinking about this in the context of programming and

that's kind of what doing calculations within an if statement or the right

side of a ternary operator something that's kind of what that's like, so

sometimes I will have a block.

Of code where I'm doing some calculations and then I have an if statement

that says if such and such then use the value of those calculations

otherwise do nothing or otherwise do something else and so if you do your

calculations first, then you're kind of crossing the bridge before you came

to it you're making a calculation for a value for which you may not

actually need at all if you don't end up going into the consequent and

actually using that value and and so I get instead you put the calculation

within the if block or what we call the consequent then.

Then you're crossing the bridge when you come to it you don't actually need

to make that calculation until you actually need to get that value now

there are I explain this to my wife last night and I thought oh that would

be a good thing for three minutes with Kenton and she said well now what

are the situations in which you would ever want to do the calculation

before, you know, you actually need it and I said well, sometimes it can be

useful just for the way the code is structured you do all the calculations

up here, sometimes it's easy to calculate one thing while you're

calculating another thing even ifYou may not need both of those things and

so there are reasons where you might want to cross the bridge before you

come to it, but I I think in those situations you want to make sure that

you're not ending up with a performance problem of some kind so mentoring

can help out a lot so at the end of the day it comes down to readability

and what's the most readable and then the exception is for those times

where it's a performance problem then then you do things anyway just

thought it was kind of interesting analogy or metaphor.

Hope you're having a nice day, we'll talk to you later.

More episodes from 3 Minutes with Kent