|
Post by ptitjoz on Mar 2, 2018 14:44:37 GMT 1
Hello,
Now that OPTION EXPLICIT exists, is there a way to see if a declared variable is not used?
Here is an example in Pascal where c is declared but not used and a message indicates it at the compilation.
program test2; var a,b,c : string; begin a:='hello'; b:='world'; writeln (a,' ',b) end.
Result of compile :
Regards
|
|
|
Post by Pjot on Mar 2, 2018 20:45:31 GMT 1
Hi ptitjoz,
The GNU C compiler also has such an option called '-Wunused-variable'. It can be used in combination with BaCon, however, it will not help you, because the generated C code always will initialize variables to avoid unexpected behavior.
In C, declaring variables without actually initializing them is one of the many mistakes a programmer can make, and which can cause real puzzling problems taking lots of time to debug.
In other words, variables you are declaring always are being used in the generated C code, though this is not visible in your BaCon program. Therefore, the '-Wunused-variable' compile option does not work.
Note that most other BASIC's do not have such option either.
Conclusion: there is no possibility to detect if a variable is being used or not.
Best regards Peter
|
|
|
Post by ptitjoz on Mar 2, 2018 21:55:38 GMT 1
Good evening Peter
Thank you for these explanations. Yes I understand the technical constraints and it seems impossible so we will do without.
Yes, it is true that a "good" programmer must declare his variables in local or global if necessary and initialize them because we never know what value there may be at the start if we do not specify. It is a good habit to take as well as error management.
Sincerely and again thank you for taking the time to explain
Joz
|
|