Today I tried to run some code. It timed out with no response. A little bit later, I figured out I had inverted a boolean condition. Which caused an infinite loop.
After all these years, I still make that same mistake with booleans -- forgetting to put a not
(or !
) in front of a condition.
Do you have a thing that always trips you up?
/β
Top comments (22)
Tip to prevent this from happening in the future: Extract your if logic into its own function.
Instead of:
Write:
This has the additional benefits of making your logic reusable and also making your if statements more readable.
Cheers!
Believe it or not, I did exactly that and still managed to miss the
not
the first time around.Haha awesome! We have all been there.
I really like doing this, but sometimes, when looking at the code really quick, in those languages that use the
!
operator, I've found myself doing functions likeisNotEmpty()
so that I don't overlook the!
by mistake.Kotlin's standard library has methods like
isNotBlank()
andisNotEmpty()
as part of theString
class that showcase this.Ramda has a function called
complement
that can help you build these functions.The thing that trips me up time and again is trusting software. Today I've spent about half an hour tailoring a series of queries in the query pane of Sequel Pro, without saving them to a text file. So when the app crashes, I lose everything. I learn from my mistakes for about a week and then gradually fall back into the lazy old me who expects things to work.
I do this too! We're using pgAdmin, and sometimes it crashes and I haven't bothered to save the query. Most of the time I'm using it, the queries are only needed for a short while, so not a big deal. But that one time I am using it to prototype developing real queries and don't save is when pgAdmin crashes and I have to start over.
Auto save if your tools have it.. otherwise train that Ctrl-S reflex!
On the boolean logic front - I always try to use positive logic everywhere, I get uncomfortable when the nots start appearing, especially review others code.
My normal workflow with those tools doesn't need to save anything, and doing so is extra time spent for otherwise quick work. So in effect, needing to save something goes against my training. And unfortunately pgAdmin doesn't have auto-save. When actually developing a query I probably just need to open Notepad++, which does have autosave, and set the Language to SQL. Then copy paste it to pgAdmin to run it.
I think using only positive logic is not really achievable (unless I have mistaken your meaning). The code that tripped me up was this.
IsNullOrWhitespace
is a positive check.hasToken
is a positive check. But I need anot
to turn one into the other.Ah ok, I get the normal workflow thing - similar here when tinkering in SSMS or similar, and I've been bitten by a crash doing that too.
I don't think you mistook my meaning on positive logic, although I used the phrase "always try" as sometimes it can be impossible (thanks random APIs!)
Currently I am working on moving all of our background workers from a framework called Resque to one called Sidekiq. This means a lot of redundant updating of jobs and moving them to different folders. It is really easy to misspell something or move it to the wrong folder and I have actually done it a couple of times and broken things. One thing I have done as I work is each time I break our workers with a silly mistake I then write a nice general test to avoid that mistake again. I have a small test file now of 5 tests that are really good at catching all my stupid mistakes! It has saved me from myself multiple times now :) I guess if you can even protect yourself with a quick automated test that is always the way I go.
Similar to this, if I'm really distracted or tired. I sometimes mixup the combinatory logic.
Kasey do you mind pm'ing me? Id love to hear how you began folding f# into your professional environment, or whether it started out there? Thanks brother.
Sure thing! Although my particular path may not be interesting to you. I made a financially-lateral move to a smaller company in order to have more freedom to choose.
Make sure you pass your feedback along to the AWS SDK team. They are the ones who designed the workflow for the
ssm:GetParametersByPath
API. It requires checking for the presence ofNextToken
to know whether you got all the parameters or you need to send a request for more. Out of curiousity how would you design paging functionality to avoid the boolean check?I was hoping for a Britney Spears themed post based on that title ;)
Conditional goofs get us all. I did something silly this morning. I meant to subtract rather than add to a value and couldn't figure out why it grew instead of shrunk. It was clear in my brain but the hands typed
+
instead of-
. We all make silly mistakes.Lack of β I guess.
Lol...
Same here, instead of
!
, sometimes I usesomething !== something
.I guess this happens from time to time. The question is how fast you can catch it. If you have a good unit test suite, probably quite fast.
i try to forget ; writing JS and coming from Java world π
Border: 0 vs. Border: none
:before vs. ::before