# Booleans as flags in loops

Booleans as flags in loops
0

#1

Is it a bad practice to use booleans as flags? I’ve been using this pattern for a couple of the intermediate algorithms and something tells me there’s better ways.

``````for (i=0; i < 2000; i ++) {
brokenPart = false;

for (loop criteria) {

if (part is broken){
brokenPart = true;
break;
}

}
if (brokenPart) {
throw out part.
}

}
``````

#2

When you find yourself starting to do something like this, it might be time to consider using a `while` loop.

#3

It depends, a while loop may end up just as ugly, since you’ll be manually incrementing the second index.

#4

But why do you even need a flag there, what is wrong with this?

``````for (i=0; i < 2000; i ++) {
for (loop criteria) {
if (part is broken) {
throw out part.
break;
}
}
}
``````

#5

Maybe I’m missing something, but why aren’t the two if statements combined and get rid of `brokenPart`?

``````for (i=0; i < 2000; i ++) {
for (loop criteria) {
if (part is broken){
throw out part.
break;
}
}
}
``````

#6

LOL, lynx beat me to it while I was typing.

#7

Hi,

Ah I think this might help with my question then. I’ve added the code after the inner for loop as an example of what I’m trying to do.

``````for (i=0; i < 2000; i ++) {
for (loop criteria) {
if (part is broken) {
throw out part.
break;
}
} if (part passed all testes) {
put the part into the bin. ( array.push(part) )
}
}
``````

#8

What does “throw out” mean? If it just means it is still in the object or array, “part passed all tests” could just be a membership check (is it still in?).

#9

Throw out is to do nothing with the part. Skip it.

#10

Then a flag is fine. If you wanted to avoid it, you could add all the parts to the bin first and remove the broken ones from there instead.

#11

Thanks everyone for all the insights.
I’ll try all the ideas out in the rest of the challenges and rewrite some of my finished ones.

#13

Any good books, resources, on how to make code more efficient and/or elegant or when to use x vs y when coding?

#14

I’m sure there are some good books out there, but I think a lot of this just comes with experience. After a while, you get a “Spidey Sense” for these things. In order to write good code, you’ve got to write a lot of bad code first.

#15

Check this SO question, I’m not sure it’s better style but will give you another option.

#16

A simpler and more functional/declarative style:

``````parts.forEach(part => {
if (part.status === 'broken') {
part.throwOut();
}
});
``````

Using JavaScript’s native array methods, you can also do things like filtering the parts by status:

``````const brokenParts = parts.filter(part => part.status === 'broken');
// a new array of all the broken parts
``````

Or checking if any of the parts are broken:

``````const somePartsBroken = parts.some(part => part.status === 'broken');
// true if 1 or more are broken; false if 0 are broken
``````

#17

Wasn’t aware of forEach() and some(). Definitely seeing the the usefulness and possibilities from your examples. Thank you for that. Much more reading to do.

#18

Thanks again everyone.

Before posting this topic I was stuck on the Sum All Primes challenge for a couple of days. It was definitely a brain buster. I finished it (the code is a bit verbose) then posted this topic. Your answers have directly helped me finish the next five challenges. Definitely made use of more while loops and forEach().