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

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jan 27 01:01:56 UTC 2022

On Thu, Jan 27, 2022 at 12:45:20AM +0000, forkit via Digitalmars-d wrote:
> I just counted the globals in my GUI IDE (that I developed with C#/Winforms.
> 34
> many set deafault, that can later be modified during function calls,
> depending on needs at the time.
> Without globals, I'd never have bothered ;-)

Some of your globals are actually global values, like:

> string clang_exe = "clang.exe";
> bool Is64Bit;
> bool vs2019_available = false;
> bool vs2015_available = false;
> string dcompiler_mars = "dmd.exe";
> string dcompiler_llvm = "ldc2.exe";

These I wouldn't be ashamed of declaring as globals, since they ARE
globals (in the sense of describing the global environment the program
will run in).  You can usually tell these from other globals by whether
or not you can make them immutable (though sometimes you may want to
keep them mutable if, e.g., you're picking them up from the runtime
environment or via program options -- but the idea being that once
they're initialized they're effectively immutable).

Others, however, I'd stick into a configuration struct or as member
fields in your program's main class, such as:

> bool new_compile_needed = true;
> public bool new_record_mode = false;
> System.Drawing.Color MyBackColor = Color.Black;
> System.Drawing.Color MyForeColor = Color.Lavender;


Do not reason with the unreasonable; you lose by definition.

More information about the Digitalmars-d mailing list