Saturday, April 17, 2010

Rethinking the Evil of XML Configuration Files

Do you remember when your system's configuration grew from a simple file containing a few name/value pairs modified by a small set of people, into a large collection of files in multiple locations with their own business processes surrounding them?

When the config files were small and informal, you could easily get by with a YAML file or a Windows .INI file format. But now that there are hundreds or thousands of settings, maintaining them all has become challenging.

The main problems with YAML and .INI configuration files are:
  • Lack of Type Safety - If the configuration reading code expects 'true' or 'false' and the configuration file contains '1', how will the code handle that input? Will it log an error? Will it silently misinterpret the '1' as 'false'? Do you want to have to write type-checking code throughout your application? You could write a comment in the configuration file that specified the type of the setting but not everyone understands all the various types and formats (e.g. dates and times).
  • Lack of Range Checking - If the configuration code expects a value between 1 and 4 inclusive, and someone has configured the setting as 5, how will your system react? What if your configuration reading code expects 'high' or 'low' and someone enters 'medium'? That's another form of range violation. You could write comments that specify the range but the comments had better agree with the code that does the actual range checking.
  • Lack of Validation Support - If one of your configuration settings is mandatory for the system to operate correctly (e.g. a web service endpoint) and it's missing, you don't discover the error until run-time. You could add a comment to the configuration file that stated that the setting was mandatory, but will people read it?
  • Lack of Appropriate Defaults - If some of your configuration values have appropriate defaults that you want to communicate to the user, you are stuck writing comments in the YAML or .INI file that list the defaults. Unfortunately, you have now just introduced duplication between the code that must take the default when the configuration value is missing and the comment in the configuration file.
XML files and, more specifically, XSD files, provide for all of the above.
  • XSD allows you to specify the type of a given configuration value. If your configuration value has the wrong type, XSD validation of the configuration file will alert you to your mistake.
  • XSD has support for range checking of several types.
  • XSD by it's very nature handles validation. If your application does XSD validation upon startup, you can quickly catch configuration errors.
  • XSD allows you to specify default values for configuration settings.
  • With an XSLT transform you could generate an HTML document that would list all your settings along with their defaults.
In short XSD codifies and enforces all of the constraints that we would otherwise add to our configuration files as comments.

Maybe XML configuration files aren't completely evil. The main complaint I had about XML configuration files was that they were so hard to get right. Isn't that ironic! YAML and .INI files are hard to prove that they're right.

How many of my YAML and .INI files are wrong and I just don't know about it?

14 comments:

Matt Doar said...

"hundreds of thousands" of configuration settings? There's one problem - why so many?

Config files by themselves are easy enough, but I find it's managing changes as an app evolves that is hard. The app ends up having to know explicitly how to handle reading and transforming the values for every version. And then how to handle a changed default that now happens to match what the user had set as their custom value - ugg.

Garren said...

If you're looking at hundred and thousands of configuration options, then I can't help but wonder if maybe there's a bit too much complexity up front in whatever solution you've put together.

Something to consider anyway.

Maybe it's time to start looking at the ideas put forward by the convention-over-configuration crowd, in particular the idea of sensible defaults. Comments in your config file that relay the appropriate input types and ranges are probably more than sufficient in most cases, too.

But that's just me. Too much xml makes my eyes bleed.

Tim Stewart said...

Thanks for reading this post.

> There's one problem - why so many?

Different kinds of apps need different amounts of configuration. Your average desktop app or web site might only need dozens or hundreds.

In a system that requires a very high degree of flexibility and must provide numerous fine-grained customizations for a large number of different user groups thousands, if not tens of thousands of configuration settings would not be unheard of.

黃欣幸 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。......................................................

鍾FeR_Quade0426 said...

Nice Post~!!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ValarieEdmon靜宜 said...

您的部落格文章真棒!!有空我一定會常來逛!! ........................................

韋于倫成 said...

快樂與人生,是最好的伴侶..............................

9d5h41dV2h1f2_dgd45 said...

真是好文呀~~給你拍拍手再加分!!!..................................................

batesda said...

Time and tide wait for no man. ............................................................

芸茂 said...

成人線上卡通 視訊網 正妹聊天 免費成人圖 交友援 av女優寫真貼圖 漫畫性感美女 微風線上影片 自拍.偷拍 ut聊聊天室 成人視訊 情趣用品 18禁卡通遊戲 巨乳美少女 咆曉小咾鼠影城 免費a片 線上觀看情色影片 免費線上觀看色情 一夜情人 383成人影音成 sexygirl 裸照 虐待美女 辣妹寫真集dvd 淫亂人妻日記 歐美熟女色情圖片 免費視訊 空姐圖貼 69成人a圖下載 微風寫真館 85cc影 微風論壇情色 嘟嘟 xo777 情色性愛貼圖 台北援交網 嘟嘟情色網 鋼管秀 下載av 洪爺娛樂往 台灣大奶影片 sexy888影片分享區 下載 巨乳人妻初體驗h漫 台灣性愛文學網 後宮18jack 限制級國片 0204成人 免費性愛觀看 走光

玫友 said...

nice job! waiting for your new artical. .................................................................

彥霖 said...

謝謝格主的分享......................................................................

文群文群 said...

Pay somebody back in his own coin.................................................................

泓發 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................