Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@illusion466 if you're repeating the same code two or more times in a block, you should write a for statement.
-
@illusion466 pretty basic advice that one of my first teachers gave to the whole class, and it's being in my head ever since.
Funny thing is, I haven't written a for statement in a long while (pun intended), I use map and reduce most times.
Anyway, this reminds me that tedious, repeated work can be automated, and has driven me to write a lot of scripts that have saved me (and my coworkers) quite some time. -
Brolls31557y@rEaL-jAsE pffft. Triple digits and never charged... does that just make me a bad whore?
-
Sagi021347y@Bitwise iirc languages like js, java, c, python, or c++ CAN break out of one loop, but not multiple nested loops without using a flag:
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
printf("i: %i j: %i", i, j);
if (i == 7) {
goto END;
}
}
}
END:whatever
Is much cleaner imho than
int flag = 0;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
printf("i: %i j: %i", i, j);
if (i == 7) {
flag = 1;
break;
}
}
if (flag == 1) {
break;
}
}
Just a matter of opinion and circumstances.
I don't know for other languages, but for C I'm pretty sure that's the idiomatic way to do it. -
@Sagi02 You could use that flag on the for's condition:
int done = 0;
for (int i = 0; i < 10 && !done; i++) {
for (int j = 0; j < 10 && !done; j++) {
// whenever you want to break:
done = 1;
}
} -
Sagi021347y@shellbug that's clever, never tought about that... It must be because I program for good old sh3 30Mhz processor that shit themselves each time they must branch XD
-
@shellbug These are the best type of scripts. Especially when they can be hacked together with ducktape and toothpick types of fixes! Lots of time and mistakes can be saved in the end too.
Related Rants
-
CowboyBebop16"Don't give your 100%. Never. Once you gave, managers will start expecting more than that." - My mentor.
-
linuswillner15(Q: How much are you allowed to Google as a developer?) βYouβre allowed to Google as much as you want. Th...
-
ripbit18Never delete code immediately. Always comment it out first.
Two or more, use a for.
rant
wk103