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

0 Members and 1 Guest are viewing this topic.

Offline davidshq

  • Newbie
  • Posts: 55
    • View Profile
Eliminating Some Code Redundancies in QB64?
« on: February 27, 2018, 12:26:53 am »
Hi Folks,

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

Dave

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Eliminating Some Code Redundancies in QB64?
« Reply #1 on: February 27, 2018, 07:15:42 am »
In the specific case of return followed by break, I wouldn't bother. The possible benefit is quite small but like any code changes there's the chance of introducing bugs.

However, if you want to take a crack at making some changes, I draw your attention to libqb.cpp and the functions contained therein. Many of them declare random temporary variables as static, which has the same effect as in BASIC - the value is preserved between calls to that function. However, the vast majority of these don't need to be static at all.

Having all these static variables means that a) none of these functions are able to be called recursively, b) they are not thread safe (qb64 uses a few threads of execution) and c) waste memory, because all static variables in a program must be allocated all the time (non-static variables are just allocated on the stack when the function is called them thrown away.

So, one thing you might consider taking a crack at is going through all these statics and removing them. If you do decide to take this on you're free to do as much or as little work as you like. Basically it would involve looking at each case and ensuring the static behaviour isn't being used, then just removing the keyword. If a variable is always assigned to in a function before it's ever read, that's a guarantee it doesn't need to be static. If it's read before it's set, that's a sure sign it does need to be static (or it's a bug!).

As a nice side effect, this'd take you on a nice tour through the source.

Offline davidshq

  • Newbie
  • Posts: 55
    • View Profile
Re: Eliminating Some Code Redundancies in QB64?
« Reply #2 on: February 27, 2018, 11:11:02 am »
Thanks Luke! I'll take a look! :)