SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.
Other
1.68k stars 445 forks source link

Rephrase rule on using variables outside the statement block they are declared in #302

Closed ConjuringCoffee closed 1 year ago

ConjuringCoffee commented 1 year ago

Fixes #272.

I've rephrased the tile and the body of the rule.

cribuc commented 1 year ago

This is a good one, the intention of the text is much clearer this way. Shouldn't it be more general, though? This is not only applicable for inline variables, one should generally not use variables outside of the scope where they have been declared.

ConjuringCoffee commented 1 year ago

This is a good one, the intention of the text is much clearer this way. Shouldn't it be more general, though? This is not only applicable for inline variables, one should generally not use variables outside of the scope where they have been declared.

I haven't thought about that before and I agree. I'll rephrase it again.

larshp commented 1 year ago

Do not use variables outside of the scope they are declared in

variables can never be used outside of their scope, https://en.wikipedia.org/wiki/Scope_(computer_science)

In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid

suggest something like "If a variable is declared in an optional branch, only use the variable inside that optional branch"

EDIT: or "Only use variables within the branch where its defined"

ConjuringCoffee commented 1 year ago

variables can never be used outside of their scope, https://en.wikipedia.org/wiki/Scope_(computer_science)

In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid

I found "scope" to be fitting with how I personally talk about this topic, but I can't argue with the actual definition of the word.

"If a variable is declared in an optional branch, only use the variable inside that optional branch"

EDIT: or "Only use variables within the branch where its defined"

Branch does fit better in this context, but I don't think it is necessary to call it "optional" - otherwise it would not be a branch anyway. I'm going to adapt your suggestion into my next commit...

ConjuringCoffee commented 1 year ago

I've also rephrased the rule to be about local variables. The rule doesn't make any sense for other kinds of variables like instance attributes.

ConjuringCoffee commented 1 year ago

I forgot the change the body of the rule, sorry 🤦‍♂️ I think its time to stop for today after I've corrected that...