I have heard some developers say they prefer to build their UIs in code behind... I am of the opinion that this leads them to write really bad code... Super large functions, spaghetti code, and mixed responsibilities of functions instead of pure functions. I think it's better to build your UIs in your platforms specific view language or markup (HTML, XAML, qml etc.). It separates real business logic from your platform interface, giving you the ability to upgrade the platform interface independent of your business logic. Like dropping Objective-C for Swift... Just wrap the business logic for now and build the new UI in Swift, then revisit the business logic could be an upgrade strategy.

What are the pros and cons to UI made in code behind vs UI made in specific view language/markup? Your thoughts?

Note: this really isn't about if they are using some MVC approach... It's purely about the mixing of code behind and ui styles.

  • I think this question might be too general. The answer is "it depends on the problem." I've had cases where building the UI in the code behind was easier than in the view, and I've also had the opposite true. – Greg Burghardt Oct 19 '18 at 19:48

I'm assuming you mean write things like

this.Window.Controls.Add(new TextBox());



I don't think it necessarily leads to bad code. You can still separate your concerns out into view, viewmodel, controller, business model etc and use good practice.

Obviously its nice if you can achieve your UI using the View Markup framework, and you should strive to do it where possible. but sometimes it doesn't support everything you want to do forcing you to make alternate choices.

Expand the framework with some custom controls that allow you to specify the behaviour within the framework OR ignore the framework and instantiate/fiddle with the controls manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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