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

rempas rempas at tutanota.com
Thu Jan 27 06:59:48 UTC 2022


On Wednesday, 26 January 2022 at 11:52:13 UTC, Mark wrote:
> Some examples of global variables that are frequently used:
> 1) C has an implicit global variable in all programs - errno.
> 2) If you are writing `asm` blocks (in C or D), then the 
> processor's flags and registers are obviously global variables 
> that can affect the function's behavior. At least in D the 
> compiler will complain if a function is marked `pure` but has 
> an inner `asm` block; the block has to be annotated explicitly 
> with `pure`.
> 3) The language runtime might also be considered as one giant 
> global variable. As an example, a function may return 
> successfully on one invocation and fail on another despite 
> being pure and being invoked with the same parameters, just 
> because a memory allocation has failed. Potentially, you could 
> even catch and handle OurOfMemoryErrors (something which you 
> aren't supposed to do, I think) and change function behavior 
> based on that. However, if I'm not mistaken, any issues related 
> to memory allocation aren't typically considered a part of the 
> function's "contract"; otherwise, `pure` would only be possible 
> if `@nogc` is also present.

Thanks for the examples! In '3', you could of course pass 
"-betterC" so there is no garbage collector at all. Also even if 
there is no garbage collector, you can have a memory allocation 
and it can fail. Just wanted to note that.


More information about the Digitalmars-d mailing list