DEV Community

Kasey Speakman
Kasey Speakman

Posted on

I did it again.

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.

cat rolling soccer ball 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)

Collapse
 
joelnet profile image
JavaScript Joel

Tip to prevent this from happening in the future: Extract your if logic into its own function.

Instead of:

if (num % 2 === 0) {}

Write:

const isEven = num => num % 2 === 0

if (isEven(num)) {}

This has the additional benefits of making your logic reusable and also making your if statements more readable.

Cheers!

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Believe it or not, I did exactly that and still managed to miss the not the first time around.

    let hasToken nextToken =
        not (System.String.IsNullOrWhiteSpace nextToken)
Collapse
 
joelnet profile image
JavaScript Joel

Haha awesome! We have all been there.

Collapse
 
chrisvasqm profile image
Christian Vasquez • Edited

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 like isNotEmpty() so that I don't overlook the ! by mistake.

Kotlin's standard library has methods like isNotBlank() and isNotEmpty() as part of the String class that showcase this.

Collapse
 
joelnet profile image
JavaScript Joel • Edited

Ramda has a function called complement that can help you build these functions.

import complement from 'ramda/src/complement'

const isEven = num => num % 2 === 0
const isNotEven = complement(isEven)
Collapse
 
moopet profile image
Ben Sinclair

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.

Collapse
 
kspeakman profile image
Kasey Speakman

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.

Collapse
 
phlash profile image
Phil Ashby • Edited

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.

Thread Thread
 
kspeakman profile image
Kasey Speakman

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.

    let hasToken nextToken =
        not (System.String.IsNullOrWhiteSpace nextToken)

IsNullOrWhitespace is a positive check. hasToken is a positive check. But I need a not to turn one into the other.

Thread Thread
 
phlash profile image
Phil Ashby

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!)

Collapse
 
molly profile image
Molly Struve (she/her)

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.

Collapse
 
pim profile image
Pim

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.

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

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.

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

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 of NextToken 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?

Collapse
 
nickytonline profile image
Nick Taylor

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.

Collapse
 
sandordargo profile image
Sandor Dargo

Lol...

Collapse
 
mittalyashu profile image
Yashu Mittal

Same here, instead of !, sometimes I use something !== something.

Collapse
 
sandordargo profile image
Sandor Dargo

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.

Collapse
 
seimic profile image
seimic

i try to forget ; writing JS and coming from Java world 😂

Collapse
 
niorad profile image
Antonio Radovcic

Border: 0 vs. Border: none
:before vs. ::before