AL icon indicating copy to clipboard operation
AL copied to clipboard

Please prevent same name in Globals and Locals

Open bvbeek opened this issue 8 years ago • 10 comments

Please let the compiler give an error in this case:

image

bvbeek avatar Jul 19 '18 12:07 bvbeek

Or add (at least) CodeCop warning.

IvoMol avatar Jul 19 '18 13:07 IvoMol

+1 for CodeCop warning not error.

rdebath avatar Jul 19 '18 14:07 rdebath

I vote for the error. Having global and local vars with the same name is incredibly confusing to read and quite bad form. I've seen it lead to bad logic as well when the wrong variable was being used from what the developer intended.

MikeGlue avatar Jul 19 '18 14:07 MikeGlue

An example would be in C#, you can save variable name in class level or method level, you can distinguish the scope by using this keyword.

shlemonz avatar Jul 19 '18 15:07 shlemonz

Yup fair enough, I just think that it's not a very clean way to write code. I'm actually reworking as much code as I can to eliminate global vars completely :) ….but of course that's not always possible.

MikeGlue avatar Jul 19 '18 16:07 MikeGlue

I agree, it gets messy...

shlemonz avatar Jul 19 '18 17:07 shlemonz

"... eliminate global vars completely ..."

Agreed, the functional style of programming usually helps making things easier to understand. Reducing or eliminating global variables reduces the number of interactions that a function could possibly do by explicitly listing only the variables that a function is allowed to access.

"...reworking..."

This is the issue. While in the theoretical world everything can be converted, in the real world you're waiting on other people (usually Microsoft) to allow the change to be possible.

This reminds me of a feature of other code analysis tools (Inline excuses #2970) with that additional feature it could be reasonable to add a $CodeCop warning for every use of a global variable.

rdebath avatar Jul 20 '18 11:07 rdebath

@rdebath good point!

bvbeek avatar Jul 20 '18 13:07 bvbeek

Changing this behaviour will inevitably lead to messy variable names, further impacting readability.

Frantic42 avatar Oct 22 '18 07:10 Frantic42

Is added as rule AA0198

MarcHansenMicrosoft avatar Jan 16 '20 11:01 MarcHansenMicrosoft