/_posts/2012-07-09-A-plea-for-less-XML-configuration-files.markdown

https://github.com/nikic/nikic.github.com · Markdown · 86 lines · 65 code · 21 blank · 0 comment · 0 complexity · 92b557ea869cebf1cee4b06745680519 MD5 · raw file

  1. ---
  2. layout: post
  3. title: A plea for less (XML) configuration files
  4. excerpt: Configuration files typically use XML or some other domain specific language. But why? Why not just use the usual programming language instead?
  5. ---
  6. I recently tried using [Phing][phing] (a PHP build system) to do some simple release automation. Just creating a PEAR
  7. package and doing a few string replacements here and there.
  8. The result? After several wasted hours I ended up using Phing only for PEAR packaging and doing everything else in a
  9. custom PHP build script.
  10. The reason? Phing uses XML files to configure what it should do during a build. So an excerpt from a build file might
  11. look like this:
  12. {% highlight xml %}
  13. <?xml version="1.0" encoding="UTF-8"?>
  14. <project name="SomeName" default="package" basedir="..">
  15. <target name="package">
  16. <property file="build/build.properties" />
  17. <propertyprompt propertyName="project.version" promptText="Enter project version" />
  18. <delete dir="${path.results.lib}" />
  19. <mkdir dir="${path.results.lib}" />
  20. <copy todir="${path.results.lib}">
  21. <fileset dir="${path.lib}">
  22. <include name="**/**" />
  23. </fileset>
  24. </copy>
  25. <!-- do more stuff -->
  26. </target>
  27. <!-- more targets -->
  28. </project>
  29. {% endhighlight %}
  30. Could you explain me, why you have to do this? Why do you have to specify everything in an XML configuration file? Why
  31. can't you just do the same thing in the programming language you're using? E.g., why can't I write this instead:
  32. ```php?start_inline=1
  33. function package(Phing $phing) {
  34. include 'properties.php';
  35. $project->version = $phing->prompt('Enter project version');
  36. $phing->deleteDir($path->results->lib);
  37. $phing->createDir($path->results->lib);
  38. $phing->copyDir($path->lib, $path->results->lib);
  39. // do more stuff, like replace version strings with $project->version
  40. }
  41. ```
  42. Don't you think that this is much nicer? Some reasons why I prefer this over XML configs:
  43. * It uses an environment you're already used to. You don't have to learn how exactly the Phing XML files work. You
  44. don't need to learn where you can use properties and where you can't. You simply know already.
  45. * You can use the full power of the programming language. E.g. the main reason why I stopped using Phing was that I
  46. could not implement some slightly more complex string replacement in it without writing 200 lines of XML. I was able
  47. to implement those replacements in 5 lines of PHP on the other hand.
  48. * All the tools you normally use for programming will work. E.g. the IDE will be able to provide autocompletion and
  49. error analysis. One could use phpDocumentor to document the different build targets and options. Heck, you could even
  50. unit test the building process, if you really wanted to.
  51. And obviously: XML is a rather verbose language, whereas most programming languages (short of Brainfuck) try to be
  52. concise and readable.
  53. At this point you might say that the above isn't really a configuration file, but rather a program written in XML. I'd
  54. agree with you there, but I think the the above also applies to "real" configuration files. They can also benefit from
  55. the power of the programming language (like putting repeating configuration "patterns" into functions or loops), etc.
  56. Also, this isn't specific to XML. I think that it is *generally* preferable to just use your usual programming
  57. language to write configuration, instead of using some special format.
  58. But don't get me wrong, there certainly are cases where using a special configuration format is the way to go:
  59. * If the configuration will be used by multiple programming languages. Obviously it doesn't make sense to write PHP
  60. configs if they have to be used by Python too.
  61. * If the type of configuration is very specific and writing it would be much easier using a Domain Specific Language.
  62. In my personal experience neither of those points apply for most of the config files I've encountered.
  63. So, what I ask you for is: If you're creating a config file, consider writing it in your normal programming language
  64. instead of XML or some DSL.
  65. [phing]: http://www.phing.info/trac/