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

forkit forkit at gmail.com
Wed Jan 26 11:29:31 UTC 2022


On Wednesday, 26 January 2022 at 11:24:34 UTC, rempas wrote:
> On Tuesday, 25 January 2022 at 23:28:36 UTC, forkit wrote:
>> // ---
>>
>> module test;
>> import std;
>>
>> int x = 0;
>>
>> void main()
>> {
>>     writeln(foo);
>> }
>>
>> int foo()
>> {
>>     int x = 10;
>>
>>     if(x) // ooh. you sure this was your intention??
>>         return x;
>>     else
>>         return .x;
>> }
>>
>> // ---
>
> What ".x" returns? Also this example is not very practical. You 
> could do the same if you had passed "x" as a function parameter?

so you asked for an example where you could slip up using a 
global variable.

well, that was an example.

you have a local variable the same name as global variable.. you 
mistyped... and... boom!


More information about the Digitalmars-d mailing list