How much logic should be entered into the user interface?

I'm not sure if this was set or not, but how much logic should you introduce into your user interface classes?

When I started programming, I used all my code for events in a form that everyone knows makes them feel absolutely pain in the butt to test and maintain. Overtime Work I came to the conclusion of how bad this practice is, and I began to break everything into classes.

Sometimes when refactoring I still feel like β€œwhere should I put this stuff”, but because most of the time the code I'm working on is in the user interface layer, has no unit tests and doesn't break in unimaginable places, I usually leave it in the user interface layer.

Are there any good rules about how much logic you enter into your user interface classes? What patterns should I look for so that I do not do this in the future?

+4
source share
8 answers

Just the logic associated with the user interface.

Sometimes people try to put it on a business level. For example, it could be in BL:

if (totalAmount < 0) color = "RED"; else color = "BLACK"; 

And the totalAmount user interface display uses color - which is completely wrong. It should be:

 if (totalAmount < 0) isNegative = true; else isNegative = false; 

And it should be all the way to the user interface level, how totalAmount should be displayed when isNegative is true.

+6
source

As little as possible ... The user interface should have only the logic associated with the view. My personal preference now is for user interface / view

  • just raise events (with data support) to PresenterClass, indicating that something happened. Let the speaker respond to this event.
  • have data display / display methods to be presented
  • the minimum number of checks on the client side to help the user get it right the first time ... (preferably in a declarative way), screening invalid entries before he even reaches the master, for example. make sure the text field value is within the ab range by setting the min and max properties.

http://martinfowler.com/eaaDev/uiArchs.html describes the evolution of user interface design. Excerpt

When people talk about self-testing, code user interfaces quickly change their head as a problem. Many people find that GUI testing will be somewhere between hard and impossible. This is mainly because the user interface is tightly integrated into a common environment interface and it is difficult to tease to split and check in pieces. But there are times when this is not possible, you will miss important interactions, there are threads of the problem, and tests are too slow to work.

The result was a steady movement for designing user interfaces in a way that minimizes behavior in objects that are inconvenient to test. Michael Feathers abruptly summed it up in the Modest Dialog. Gerard Mesaros generalized this concept of the idea of ​​a humble object - any object that is difficult to verify should have minimal behavior. Here, if we cannot include it in our test suites we will minimize the chances of an undetected failure.

+6
source

I suggest that the user interface does not include any business logic. Even checks. All of them should be at the level of business logic. This way you make your BLL independent of the user interface. You can easily convert a Windows application to a web application or web services and vice versa. To do this, you can use the infrastructure of objects, for example Csla .

+2
source

The sample you are looking for could be a Model-view-controller , which basically separates the DB (model) from the GUI (view) and the logic (controller). Here Jeff Atwood accepts this. I believe that you can not be fanatical about any framework, language or template. Although heavy numerical computations probably should not sit in the graphical interface, it is fine to perform some basic input checks and formatting the output.

+1
source

Input checks attached to the control. Like emails, age, date validators with text fields

0
source
James is right. Typically, your business logic should not make any assumptions about the presentation.

What if you plan to show your results on different media? One of them may be a black and white printer. "RED" would not cut him off.

When I create a model or even a controller, I try to convince myself that the user interface will be a bubble bath. Believe me, this greatly reduces the amount of HTML in my code;)

0
source

Always set the minimum amount of logic at any level at which you work.

By this, I mean that if you add code to the user interface level, add the minimum amount of logic needed for this level to perform its operations with the UI (only).

Not only does this make a good separation of layers ... it also saves you from bloating code.

0
source

I already wrote a "compatible" answer to this question here . The rule (for me) is that there should not be any logic in the user interface, except for the user interface logic, and requires standard procedures that will manage general / specific cases.

In our situation, we have come to the conclusion that the form code is automatically generated from the list of controls available in the form. Depending on the type of control ( bound text, bound boolean, bound number, bound combobox, unbound label, ... ) we automatically generate a set of event procedures (such as beforeUpdate and afterUpdate for text controls, onClick for labels, etc. ) That run common code located outside the form.

This code can either generate general things (if the value can be updated in beforeUpdate , arrange the set of records in ascending / descending order onClick event, etc.) or specific procedures based on the form and / or name of the control (for example, some work in afterUpdate event, for example, calculating the value of the totalAmount control unitPrice value and quantity ).

Our system is now fully automated, and the creation of forms is based on two tables: Tbl_Form for a list of forms available in the application, and Tbl_Control for a list of controls available in our forms

Following this answer and other posts in SO, some users asked me to develop my ideas. Since the subject is quite complex, I finally decided to open a blog to talk about this UI logic. I have already started talking about the user interface, but it can take several days (.. weeks!) Until I can specifically cover the object of interest to you.

0
source

All Articles