It depends on what you really mean by 'global'. In the example above, I'd say its fine.
You appear to be showing a main
module, which probably shouldn't be imported
by anything. In other words, it isn't really global, it is local to the main
module. It really isn't so different from
class Main {
private string _roothtml;
static this() { _roothtml = buildPath(getcwd, "html"); }
void run() { }
}
Even if it isn't really your main
, D's
module
system offers protections of its
own. Just stick a private
on roothtml
to encapsulate it within the module
(it wouldn't hurt to do this in your main module anyways, just to be clear).
A pattern like this is widely employed in the source code of git. Rather than
having a single main module that invokes a function for a given command, you
have many main
functions -- one for each top-level command.
For example, take a look at
upload-pack.c
.
See those variables declared at the top of the source file?
Would the code have been any clearer or safer if they were wrapped in a class in
typical OOP style or of explicitly passed to each function in a more purely
functional style?
Each source file acts as a unit of encapsulation for a given command. This style is not always appropriate, but in the case of a program that can be thought of as a set of distinct commands, it can be cleaner than the alternatives.
Ultimately, the answer will be specific to the context, your given project, and
your personal style. Generally speaking, cross-module globals are something to
be looked on with suspicion, but module-level variables can sometimes be cleaner
than the alternatives.