[Sketcher] Possible bug in new line split code

About the development of the Part Design module/workbench. PLEASE DO NOT POST HELP REQUESTS HERE!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

[Sketcher] Possible bug in new line split code

Post by chennes »

LGTM caught what appears to be a logic error on the line splitting code added by git commit 4d6b1f3. It takes the form of code that looks like this:

Code: Select all

do {
            ...
            int newId = addGeometry(newLine);
            if (newId < 0) {
                continue;
            }
           ...
} while (false);
I don't know what's intended here, exactly -- LGTM complains because that continue statement does not do what programmers normally think of "continue" as meaning, which is to restart the loop. Because the loop condition is always false, this continue actually functions as a break. Is that the intended behavior? Or is this code expecting to restart the loop from the top?

(cppreference has a good explanation of how continue works)
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: [Sketcher] Possible bug in new line split code

Post by openBrain »

I think the behavior is correct (looking at a glance) but it's indeed weird and 'continue' would happily be replaced by 'break'. ;)
tpavlicek
Posts: 61
Joined: Sun Jan 07, 2018 2:15 am

Re: [Sketcher] Possible bug in new line split code

Post by tpavlicek »

Hello,

the intention here is to stop any further processing if adding a new edge failed. I agree, the whole construction

Code: Select all

do {
   ...
   if (...) continue;
   ...
}
while (false);
is rather unorthodox, but goto could be considered even worse ;-) Of course, I could have replaced this by try/catch, but I am rather plain C guy, so if it is not neccessary, I stick to basic statements.

If continue raises false positives here, please feel free to replace it with break.

Kind regards,


Tomas
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: [Sketcher] Possible bug in new line split code

Post by openBrain »

tpavlicek wrote: Sun Aug 22, 2021 9:24 amOf course, I could have replaced this by try/catch, but I am rather plain C guy, so if it is not neccessary, I stick to basic statements.
I'm also basically a C coder. :)
But even there, 'break' better denotes the intention IMO.
'break' is "unconditionally exit the current loop without further processing".
'continue' is "re-evaluate conditional statement of the loop and proceed accordingly".
Anyway, thanks for fast feedback.
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: [Sketcher] Possible bug in new line split code

Post by chennes »

So if I am understanding this code correctly, then, it's not ever intended to loop at all, is that correct? It's essentially just mimicking goto behavior without making explicit the goto itself?

ETA: Here's a PR with my proposed change... https://github.com/FreeCAD/FreeCAD/pull/4990
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
Post Reply