-1

In the project I work on we use XSD to generate some classes. XSD determines the order of elements, similar to this snippet:

<xs:element name="Customer_ID" type="com:Text6"/>
<xs:element name="Customer_Desc" type="com:Text150"/>
<xs:element name="Customer_Name" type="com:Text50"/>
<xs:element name="Institution" type="com:Text150" minOccurs="0"/>
<xs:element name="Scheme" type="com:Text50"/>
<xs:element name="Country" type="com:Country"/>

One colleague created some tests in which he sets object fields in different order comparing to the order in XSD, for example:

obj.setCustomerName("name");
obj.setCountry("EU");
obj.setScheme("scheme");
obj.setCustomerDesc("description");
//etc.

Furthermore, he uses different order in different tests. I argued that we should stick to the same order in code as it is in XSD because that way it's easier to read or change code/XSD. He thinks it's nitpicking because, at the end, object will have its fields initialized anyway.

I'd like to hear your thoughts - is this really nitpicking from my side or not?

3 Answers 3

3

The fact that you are using XSD is not important. You are creating Java classes and those don't care the order (well, unless there is some dependency that should be dealt with in the constructor or a builder, rather than through setters). Keeping them in order because of the XSD can end being a maintenance nightmare, as any change on the class/schema could mean reordering every single place where you use your setters

2
  • im not sure thats true. xsd allows you to specify whether the order should be respected or not
    – Ewan
    Commented Dec 7, 2017 at 12:12
  • 1
    I am saying it doesn't matter what XSD does. It shouldn't be part of your considerations Commented Dec 7, 2017 at 12:14
4

Whenever I'm doing some tedious task, like making a new X object and setting 20 of its properties I tend to do so in alphabetical order, because that's the order I see things in the IDE's intellisense/code help combo dropdown - helps me avoid missing one. I'd be tempted to sort and update XSDs similarly, inserting new elements in an alphabetical order so long as there wasn't a declared sequentiality, but in these days of "search trumps navigation" I'd be more likely to Ctrl-F or some IDE based "goto reference/implementation/declaration" function than look at things and want them to be in a certain order so I wouldn't care so much if a fellow dev had a different take on ordering independent statements

Perhaps leverage a search approach more, then you'll care less about things such as this, or become responsible for the coding standards at your organisation then you can write the rules. (Beware of writing rules that go contra to the generally accepted/easy way of doing things - look how many years the Highway Code has been telling us "only flash your headlights to let the other driver know you're there" when everyone always and forever will have it mean "proceed with the course of action I assume you're going to make; if you surprise me I can always give you the horn and the finger")

3
  • If you are setting 20 properties individually you likely have a poor design. This is a case where an IDE "making it easy" is really making it easy to do the wrong thing.
    – user949300
    Commented Dec 7, 2017 at 16:23
  • It's contextual, but I must have set close to 20 properties last time I wanted to send a System.Net.MailMessage..
    – Caius Jard
    Commented Dec 7, 2017 at 16:35
  • I'm not a .net person, and it sounds like the design issue is in their code, not yours, but is there some way to use a Factory with good presets, or a Prototype, to construct a "pretty good" message, that then requires only a couple of changes via setters?
    – user949300
    Commented Dec 7, 2017 at 17:55
2

You don't say what the container for your elements is.

if you use a <xs:sequence> then the order of the elements is enforced.

I had a look and can't see what if any conditions are present if you just use schema I guess from your question no order is enforced.

On the whole xsd is a complex tool and defining classes through its use could get tricky if you are going to be strict with it. That's part of the reason for the switch from xml to json.

Also, in this case he is simply setting the values on the objects. not defining them.

Not the answer you're looking for? Browse other questions tagged or ask your own question.