if statements and"We'll cross that bridge when we come to it" - a podcast by Kent C. Dodds

from 2021-05-27T14:08:33

:: ::

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 andthat'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 calculationsotherwise 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 calculationwithin 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 nowthere 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 whatare 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 beuseful 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 andso 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 thatyou'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 readabilityand 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.

Further episodes of 3 Minutes with Kent

Further podcasts by Kent C. Dodds

Website of Kent C. Dodds