Why people dislike global variables so much while I find them so convenient?

Basile B. b2.temp at gmx.com
Wed Jan 26 17:48:09 UTC 2022


On Wednesday, 26 January 2022 at 11:38:51 UTC, forkit wrote:
> oh. what is actually going on in foo is irrelevant of course.
>
> foo does nothing of any relevance.
>
> the point I was making, was about having a global variable with 
> the same name as as a local variable, could 'potentially' slip 
> you up.

What you describe here is actually not specific to globals.
The same can happen with aggregate members

```d
struct S
{
     int e;
     function f() {int e;}
}
```

but also inheritance. This is more a problem of name hijacking, 
except that this is officially _not recognized as such_, since 
there is a way to distinguish (either using the global access 
operator or the explicit ThisExp.

Here is an example for the inheritance taken from DMD codebase 
and that was encountered, _for real_, 2 years ago while doing a 
refactoring

```d
class A
{
      bool isscope;
}

class B : A
{
     bool isscope(); // not even related to A.isscope
}
```

now remember that getters can be called without parens. Now 
imagine you want to rename one of the `isscope`.


More information about the Digitalmars-d mailing list