Почему так много программ используют конфигурации в стиле "foo.bar.baz = qux"?

492
cpast

Я заметил, что тонны программ хранят информацию о конфигурации в различных иерархических ключах. Например, в Firefox about:configесть ключи, как network.http.pipeliningи network.http.pipelining.sslи network.http.use-cache. Я заметил этот стиль конфигурации в Firefox, в OS X ( Library/Preferences) sysctl, среди прочего. Почему это так часто? Была ли какая-то ранняя программа, которая использовала это, так что другие копировали это?

3
Когда что-то слишком сложно для эффективной обработки, вы разбиваете его на иерархию с меньшими, более управляемыми компонентами ... Mehrdad 11 лет назад 0
Я снова открыл вопрос. Это не «не конструктивно», как видно из очень информативных ответов. slhck 11 лет назад 0

2 ответа на вопрос

5
mtone

This hierarchical convention is there to provide clarity, helping us to know the scope of influence of each settings and to differentiate multiple settings with an identical or ambiguous name among various modules.

This is useful in applications with too many possible settings to include in the main user option pages (where the hierarchy is shown by tabs or pages), and a good alternative to plain INI files (with [title] section delimiters) that are more prone to user error and usually require an application restart to take effect.

Hierarchal settings are also likely to reflect, to an extent, the internal top-down object-oriented model used by the developers:

Image of hierarchy

In this example model, we could very well imagine a person.professor.disallow_strikes = true setting in there, while a students.disallow_strikes could remain to false or not be available at all. This compartmentalization is also the basis for Namespaces in programming languages (and the popular .NET framework follow the exact same naming convention: using System.Threading.Tasks).

So, we now can assume that network.http.xxx setting should not have influence on network.ftp or other network submodules, while a network.xxx setting will likely have influence on all of them.

The extra information is beneficial...

  • for the user: we have a better idea of which part of the application a given setting will have influence, making problems easier to troubleshoot (and avoid!)

  • and for the developers: the person working on or troubleshooting a specific module can be easily aware of which user-modifiable setting can affect his/her current work and concentrate on that.

4
SuperMagic

You've mentioned one of the main reasons in your opening sentence: hierarchical keys.

Each group of keys is, uh, grouped. All the network related keys are network.something. All the http related keys are network.http.something and so on. This makes the key itself somewhat self-documenting what the value refers to. If one were to use the ini file style (which, to be clear, nothing would stop you from using these sorts of keys if you wanted) with [section] and key=value pairs, a given key may be ambiguous until the section is known. More over, the placement of keys in a file is important. Putting the ssl key in the [GUI] section is probably a mistake and may cause the key to be ignored. The a.b.c style should mean the order of keys in a file doesn't matter. If you read networking.http.ssl.key= it doesn't matter if the previous line was gui.background.color= or something else. The fully qualified key is named.

Why is it so common? It's bloody useful, that's why. It's also easy for humans to read and understand (on a key by key basis) and edit.

Where did it come from? I'd say C style structures which probably have a lineage I'm unaware of, and which have trickled down into many other programming languages as well. Setting a value in a C structure would be structure.property=value, and for nested structures structure.substructure.property=value and so on (though in real life, pointers would translate many of those dots into -> instead, but that's a minor trifle).

Это также похоже на * пространства имен *, подразумевается, что "." служит семантическим разделителем. mr.spuratic 11 лет назад 3
Даже лучше. Что сказал мистер Спуратик. SuperMagic 11 лет назад 0

Похожие вопросы