views:

172

answers:

6

I've read many times and agree with avoiding the use of globals to keep code orthogonal. Does the use of the config file to keep read only information that your program uses similar to using Globals?

+3  A: 

If you're using config files in place of globals, then yes, they are similar.

Config files should only be used in cases where the end-user (presumably a computer-savvy user, like a developer) needs to declare settings for an application or piece of code, while keeping their hands out of the code itself.

David
I didn't mean for the first part of this answer to sound snippy. The point I was trying to make is that config files are not a work-around to prevent globals.
David
+3  A: 

My first reaction would be that it is not the same. I think the problem with globals is the read+write scenario. Config-files are readonly (at least in terms of execution). In the same way constants are not considered bad programming behaviour. Config-files, at least in the way I use them, are just easy-changable constants.

Ruben
A: 

Configuration files are really meant to be easily editable by the end user as a way of telling the program how to run.

A more specialized form of configuration files, user preferences, are used to remember things between program executions.

R. Bemrose
A: 

Global is related to a unique instance for an object which will never change, whereas config file is used as container for reference values, for objects within the application that can change.

One "global" object will never change during runtime, the other object is initialized through config file, but can change later on.

Actually, those objects not only can change during the lifetime of the application, they can also monitor the config file in order to realize "hot-change" (modification of their value without stopping/restarting the application), if that config file is modified.

VonC
+3  A: 

Well, since a config file and a global variable can both have the effect of propagating changes throughout a system - they are roughly similar.

But... in the case of a configuration file that change is usually going to take place in a single, highly-visible (to the developer) location, and global variables can affect change in very sneaky and hard to track down ways -- so in this way the two concepts are not similar.

Having a configuration file ususally helps with DRY concepts, and it shouldn't hurt the orthogonality of the system, either.

Bonus points for using the $25 word 'orthogonal'. I had to look that one up in Wikipedia to find out the non-Euclidean definition.

Dan Esparza
Yes, reading The Pragmatic Programmer and the subject of globals is introduced after decoupling. The idea of config files came to mind and wanted to determine their level of evil.
osp70
A: 

They are absolutely not the same or replacements for eachother. A config file, or object can be used non-globally, ie passed explicitly.

You can of course have a global variable that refers to a config object, and that would be defeating the purpose.

Ali A