Author Topic: Eliminating Some Code Redundancies in QB64 ?.  (Read 2057 times)

0 Members and 1 Guest are viewing this topic.

Offline jackyjoy123

  • Newbie
  • Posts: 2
    • View Profile
Eliminating Some Code Redundancies in QB64 ?.
« on: February 26, 2021, 12:02:16 pm »
Hello,


I don't know a lot about C++, but as I mentioned elsewhere, I've loaded it up in VS 2017 (the free community edition) and I've also installed an eval copy of JetBrain's ReSharper for C++.

I'd be interested in helping do some code cleanup at a very basic level, but would need someone to review my submissions to make sure I'm not breaking anything. :-) Let me know if that is something worth trying, I don't want to waste effort on it if it isn't considered helpful.

For example, ReSharper is showing up a number of places in which QB64 has switch statements with individual cases ended with return and break, the latter will never be triggered since return will always execute. Maybe there is a reason for this redundancy I'm not familiar with (as noted, I'm a C++ n00b).


thanks
jackyjoy

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Eliminating Some Code Redundancies in QB64 ?.
« Reply #1 on: February 26, 2021, 12:46:45 pm »
For example, ReSharper is showing up a number of places in which QB64 has switch statements with individual cases ended with return and break, the latter will never be triggered since return will always execute. Maybe there is a reason for this redundancy I'm not familiar with (as noted, I'm a C++ n00b).

Uhh, in this case you should better drop ReSharper immediately. A code optimizer, which suggests removing a break from a switch case, just because it is never reached during runtime yet, is a BAD tool.

One basic rule in C/C++ when doing switch blocks is: "Always have a break in EVERY case." First it prevents the
program from falling through from one to another case, if your code is not running as expected during development, second when you add more cases it's obvious where to add them (after the break) to not accidently destroy the flow of an existing case by adding the new case in the middle of another case.

EDIT:
And third, think about it. You remove the break now, as it's never reached because of the return before it, good.
If it happens in the future, that we need to replace the return with some other code (let it be a bugfix or a new feature), would you remember to add a break then again?? - Because then it is needed, cause the return is no more.
« Last Edit: February 26, 2021, 12:53:40 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline kelleyshore

  • Newbie
  • Posts: 1
    • View Profile
Re: Eliminating Some Code Redundancies in QB64 ?.
« Reply #2 on: March 04, 2021, 11:00:43 pm »
OK,I get it,tks!