I keep seeing this term being thrown around in blogs about frameworks. While I understand the difference between declarative and imperative programming, how does this apply specifically to UI? Why does there appear to be a special term for it? Are these different 'things'? If so, what is the alternative to Declarative UI and why would I want to use it?
-
2@Telastyn. Imperative and declarative are very different things. It is "what" vs "how". An HTML layout would be declarative. A sequence of drawing instructions would be imperative.– mike30Nov 2, 2012 at 16:04
-
@mike: The OP asked if imperative/declarative UI was different from imperative/declarative programming. They're not.– TelastynNov 2, 2012 at 16:57
-
@Telastyn The OP is asking the difference between declarative UI and <foo> UI (such as "imperative UI"), and if an alternative to declarative UI even exists.– IzkataNov 3, 2012 at 3:58
2 Answers
"Declarative UI" means you describe in some kind of language what elements you need in your UI, and to some degree how they should look, but you can leave out details like the exact position and visual style of the elements. For example, in HTML you can describe that you want an input field, but how and where this field will be placed in the UI is highly dependent on the browser you are using.
Today, the term is more often used for UI frameworks with a strict separation of the look of the UI from the behavior, which means the code reacting to UI events. For example, using XAML, you declare the look of your UI in a specific XML dialect, but you implement the behavior in separate program code.
-
2Good answer. Nitpick: whether details like the "exact position" are specified or not, is orthogonal to the declarative/imperative divide. However, in practice the approach of letting the computer handle the details is more common in declarative systems (though I'm not sure why).– sleskeNov 2, 2012 at 23:00
Declarative UI is a UI that's designed in a declarative way (you describe what it should be like) rather than an imperative way (you code the steps to create it.) It's really nothing special or unusual; it's been around since at least the 1990s, when you had Visual Basic and Delphi with visual form designers that let you lay out the user interface of your program in an intuitive, WYSIWYG manner.