📜 ⬆️ ⬇️

configs: XML vs. API

I believe that the use of XML in configs is evil and cowardice. Further I will try to explain why.


Why try to use XML? "Because you don't have to be a programmer to edit it." But this is nonsense! I did not see simple people (not programmers) who would rule xml themselves! None of the IDEs will save you from having to use your hands in an XML file and edit some squiggles on others.

Now let's consider what such cowardice and brainwashing overflow for such simple developers like me.
')
The main disadvantage is that in XML configs it is not possible to use long-familiar and convenient OOP techniques: inheritance and polymorphism.

Consider an example. There are two web.xml files, part of the configuration (servlet advertisements, parameters, filters, etc.) is in one, part - in the other.

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter-name>filterA</filter-name>
<filter-class>ru.FilterA</filter-class>

</web-app>


<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter-name>filterB</filter-name>
<filter-class>ru.FilterB</filter-class>

</web-app>


So - by means of web.xml DO NOT UNITE them. Such an elementary operation! ..

What would I do if the configuration was done by calling some Factory method? I would create a WebA ancestor class.
public class WebA implements WebConfig {
...
}


and class descendant of WebB
public class WebB extends WebA {
...
}


After that, all you need is to create your own config:

<web-conf configClass="WebB" />


EVERYTHING! This elementary operation in Java (yes, there in any PL) is practically impossible in XML.

Of course, the XML format can be developed to such an extent that it will be possible to do almost everything the same as in ordinary PL. For example, in the POM format maven-ovsky, some “inheritance for the poor” is provided by specifying the “parent” tag.

Basically, in maven, you can inherit things like dependencies. But what is inside the “build” tag is not inherited anywhere! Just the developers of the format do not look this far, for a typical project it is not necessary. But in practice, this usually results in the use of copy-paste technology.

So which of all the bullshit does it make? The conclusion is that as soon as you get a config a little more complicated than specifying the factory method, immediately transfer the configuration from XML to normal PL (replacing Schema with API), otherwise you will constantly suffer, implementing all standard programming language features in your XML format.

Source: https://habr.com/ru/post/42658/


All Articles