Al: Please prevent same name in Globals and Locals

Created on 19 Jul 2018  Â·  10Comments  Â·  Source: microsoft/AL

Please let the compiler give an error in this case:

image

enhancement static-code-analysis

Most helpful comment

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

All 10 comments

Or add (at least) CodeCop warning.

+1 for CodeCop warning not error.

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.

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.

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.

I agree, it gets messy...

"... 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 good point!

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

Is added as rule AA0198

Was this page helpful?
0 / 5 - 0 ratings