Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an accepted style for indenting attributes in XAML?

I have seen both of these forms:

Style #1

<TextBox
    Name="someTextBox"
    Width="50"
    Height="60" >
    Some Text
</TextBox>

Advantages

  • Consistent indentation level for all elements. You won't see sibling1's attributes indented 8 spaces, while sibling2's attributes are indented 9 spaces.
  • Minimal indentation level. Long element names won't have extremely indented attributes.

Disadvantages

  • Hard to distinguish content from attributes.
  • Takes an extra line.

Style #2

<TextBox Name="someTextBox"
         Height="60"
         Width="50" >
    Some Text
</TextBox>

Advantages

  • Easy to distinguish content from attributes
  • Saves a line of text

Disadvantages

  • Different elements at the same level in the tree can have different indentation levels for attributes.
  • Indentation levels can get quite high.

In short, both work (and both are supported by Visual Studio). I lean towards style #1, but the most important thing would be that I am consistent with other developers. Is one of these styles more commonly used?

like image 966
Matthew Avatar asked Oct 11 '22 09:10

Matthew


2 Answers

I don't think there is an accepted style.

But since the structure of XAML is very different from the structure of normal imperative code, I think a different approach might be better.

I prefer a style you didn't even mention: usualy have all properties on one line. If that line gets too long, split it, preferably in a way that makes sense. So, for example, something like:

<TextBox Name="someTextBox"
         Width="50" Height="60">
    Some Text
</TextBox>

I don't think high indentation levels are problematic by themselves (they are in normal programming, but that's something else).

like image 126
svick Avatar answered Oct 18 '22 10:10

svick


Personally, I prefer having all properties in one line of the XAML, just when I write HTML or XML. I think this may be due to the fact that I always have word wrapping enabled in Visual Studio, and have trained my eyes to read it.

Looking at both of the styles you have provided, they are actually harder for me to read.

If I had to choose though, I would pick style 2 for its readability.

like image 25
Mathias Lykkegaard Lorenzen Avatar answered Oct 18 '22 10:10

Mathias Lykkegaard Lorenzen