Live data from Hacker News

When XML Beats JSON: UI Layouts

engineering.instawork.com

221–230 of 274 posts

Re: When XML Beats JSON: UI Layouts

#221
post #52

Earlier quoted context omitted.

I’ve been around long enough to use them all. I’m pretty happy with JSON as a debuggable serialization format for machine-to-machine or light machine-to-human use cases. YAML works well for human-to-machine (JSON could fill this use case as well if it would support comments and multi line strings). I don’t use TOML much, and good riddance to XML.

"Good riddance to XML" is a bit much. Did you read the article? XML is ideal for representing tree structures. Pick the tool for the job. For example, XAML does an excellent job in declaring your UI. Its use of namespacing, and how you can use child nodes to declare properties of an object, makes it highly flexible albeit slightly verbose.

> For example, XAML does an excellent job in declaring your UI.

JSX does a better job.

I hate XAML, it doesn't add anything except verbosity over just using C#. The original goal of XAML was to allow bindings to multiple programming languages and to allow designers to use a suite of amazing tools to finished designs off to developers for implementation.

What ends up happening is developers write XAML, then they write C# to back up what XAML can't do, then they have a complex build stage to shove all this crap together, but instead of XAML just being transpiled to C# it has its own run time thing that hosts compiled XAML files.

JSX by comparison is a trivial transformation to JavaScript. It is easy to read, and it does not try to replace JS control structures.

In fact you could probably trivially transform a JSX syntax to ANY C type of language, I know there is a proposal (implementation?) for a JSX-like to Dart.

The sheer awesomeness of JSX is hard to describe if you haven't used it. It is like something asked "what is the simplest, easiest to understand templating language we can possibly create?" and out popped JSX.

My favorite part of JSX is how it doesn't try to have control structures in it. Everyone else gets this wrong and adds some sort of string types DSL to their templating language.

JSX doesn't do that. If you want to map over an array and spit out a bunch of elements, you just do so in JavaScript/TypeScript.

Re: When XML Beats JSON: UI Layouts

#222

Who would've thunk that different tools excel at different tasks? We need more articles like this, to remember us that we should consider what's best for the task at hand and not what's trendy.

Yup, use object notation for passing around objects, use a markup language for markup. It's almost like reading the names gives a hint as to what it was designed for and probably best at.

My sniff test is that if you're editing it by hand, JSON is a poor format because you'll want the benefit of a schema or at least a user-friendly format. If you're not, the syntax doesn't really matter so we should evaluate it on technical merits (verbosity, computational complexity for serialization/deserialization, memory footprint, etc).

I use:

- config file formats for config files (usually TOML or INI format) - data formats for data (JSON, protocol buffers, etc) - markup formats (Markdown, XML/HTML) or code for markup

I really don't get why people try to force all use cases onto the same format. Use whatever is well suited for the task, preferring familiarity over unnecessary technical benefits.

Re: When XML Beats JSON: UI Layouts

#223

At this point I am convinced that the decision to define UI layouts in XML instead of code has been a terrible mistake. - For starters, it's overly verbose. If you've worked with stuff like XAML in WPF or XML layouts on Android, you know how quickly those files tend to get bloated. - It does not lend itself well to reuse compared to, well, actual code. Which means there tends to be a lot of repetition. Which leads to…

I'm writing Flutter apps with Dart, and not having to use XML (or HTML+CSS) for UI layout has been a breath of fresh air. It's just code. I really like it.

Re: When XML Beats JSON: UI Layouts

#224

At this point I am convinced that the decision to define UI layouts in XML instead of code has been a terrible mistake. - For starters, it's overly verbose. If you've worked with stuff like XAML in WPF or XML layouts on Android, you know how quickly those files tend to get bloated. - It does not lend itself well to reuse compared to, well, actual code. Which means there tends to be a lot of repetition. Which leads to…

One more interesting example of XML based GUI system that avoids most of the problems listed above is HaxeUI. The same way as Qt it solves them using compile time code generation.

Accessing objects from code is not a problem due to compile time code generation, no findById just access the field. Due to the powerful macro system in Haxe you even get in code autocomplete for objects defined in xml before running compilation.

For code reuse it allows easy way of defining custom widget classes based on xml file and using custom widgets in xml file.

In my experience with Qt bloat usually comes from graphical editors that add unnecessary properties you wouldn't have set in code or GUI systems that don't support creating reusable subcomponents.

Re: When XML Beats JSON: UI Layouts

#225

Earlier quoted context omitted.

XML has comments. They are the same as HTML comments.

That was indeed my point - whereas one can't in JSON.

I must have read your post three times and still thought you said "and XML". I'm sorry.

Re: When XML Beats JSON: UI Layouts

#226
post #41

What XML is also very good at is: representing tagged text, what is called "mixed content" in XML terminology. Just try to write the JSON equivalent of: The JSON format was invented by Douglas Crockford . If your document consists mainly in losely structured text with some annotation, you better use XML.

Are you sure you want hierarchy though? [ ["the ", {}], ["JSON format", {"link": "https://www.json.org/"}], ["was invented by ", {}], ["Douglas Crockford", {"bold": true}], [".", {}] ]

In this particular example you need hierarchy for on at least two occasions.

1. The xml denotes that the whole thing should be wrapped in a div, and shouldn't just be considered a paragraph. With only lists you can't denote what kind of level you're at, unless you force each level to always carry the same meaning (which makes the format weaker than XML).

2. Setting "bold" to true isn't the same thing as emphasizing the text. Typically 'em' is italic, but more importantly a nested 'em' should no longer be italic. To achieve this with JSON you're forced to combine formatting and structure in a way that CSS was designed to prevent.

Re: When XML Beats JSON: UI Layouts

#227

At this point I am convinced that the decision to define UI layouts in XML instead of code has been a terrible mistake. - For starters, it's overly verbose. If you've worked with stuff like XAML in WPF or XML layouts on Android, you know how quickly those files tend to get bloated. - It does not lend itself well to reuse compared to, well, actual code. Which means there tends to be a lot of repetition. Which leads to…

Anything that builds the UI at runtime is silly. Your UI isn't going to change at runtime (other than in a few predictable spots), why would you waste processing time dynamically calculating it.

Re: When XML Beats JSON: UI Layouts

#228
If it's purely a choice between XML and JSON, I'd agree that XML is a bit better. I'd prefer to use neither.

Most of these UI layout formats are essentially complex domain specific languages, paired with IDE support. I don't see any particular need to base them on XML, which was intended for marking up documents.

Re: When XML Beats JSON: UI Layouts

#229

At this point I am convinced that the decision to define UI layouts in XML instead of code has been a terrible mistake. - For starters, it's overly verbose. If you've worked with stuff like XAML in WPF or XML layouts on Android, you know how quickly those files tend to get bloated. - It does not lend itself well to reuse compared to, well, actual code. Which means there tends to be a lot of repetition. Which leads to…

Its funny WinForms was just code. The designer generated the code or you code do it by hand. You could read it and understand it because it was the same language you used. If there was a problem you could debug it. I never felt more productive than when my UI declaration was just code. Inevitably you need to mix some logic in the layout language and then debug it (For each render this thing, binding syntax etc.) now…

If you use F#, Fabulous[1] for mobile apps and Fable+Elmish[2][3] for web exist. I use Giraffe's[4] view engine for server-side HTML templating. And it looks like AvaloniaUI is going to have a similar style UI in FuncUI[5], but I'm not sure how active that is.

I'm not sure about any C#-focused efforts.

1: https://github.com/fsprojects/Fabulous

2: https://github.com/fable-compiler/Fable (transpiles F# to JS)

3: https://github.com/elmish/elmish

4: https://github.com/giraffe-fsharp/Giraffe

5: https://github.com/JaggerJo/Avalonia.FuncUI

Re: When XML Beats JSON: UI Layouts

#230
post #184

At this point I am convinced that the decision to define UI layouts in XML instead of code has been a terrible mistake. - For starters, it's overly verbose. If you've worked with stuff like XAML in WPF or XML layouts on Android, you know how quickly those files tend to get bloated. - It does not lend itself well to reuse compared to, well, actual code. Which means there tends to be a lot of repetition. Which leads to…

" The only real strengths of XML are that 1) it's declarative, 2) it's hierarchical, and 3) it's diffable. But code can be too, without all of these other drawbacks. " Code generally isn't declarative or hierarchical unless your language is those things as well. A declaratively defined interface in C# would end up being some like a bunch of strings or enums passed to some object constructors. At that point I'd rather…

> Code generally isn't declarative or hierarchical unless your language is those things as well. A declaratively defined interface in C# would end up being some like a bunch of strings or enums passed to some object constructors. At that point I'd rather a an XML implementation that can be verified and processed with tools.

I should clarify and say code can be declarative with the correct language support. I agree that it's probably not well suited to C# in its current state.

> I frequently write WPF code these days and don't find it any more verbose than attempting the same thing in C# statements. If anything, it's less verbose and it has the added benefit that it's all organized in the same group of files.

Here are two examples I would consider overly verbose in XAML:

1. Triggers, DataTriggers, EventTriggers, MultiTriggers, etc. Here we're dealing with the fact that XML is not a programming language and lacks basic control structures, so we've gone ahead and defined our own mini-programming language just so conditional values can be expressed in XML. Why?

2. Anything having to do with overriding ControlTemplates.

> Hasn't really been my experience. Syntax errors are caught by the editor typically and further errors do get caught at compile time for XAML at least. Some errors don't get caught, but these are the same errors that would get missed if I attempted the same thing in C# code.

Ever make a typo in a binding?

I agree that Visual Studio is really good at helping you out where it can, but at the end of the day we're still trying to shoehorn programming constructs (objects, properties, enums, etc) into a document language format by shoving everything into XML attributes (where everything is a string) and there is a fundamental impedance mismatch between the two. Good tooling only goes so far.

Post reply on HN