Are conditional statements in submissions bad news?

I need a quick sanity check. I am trying to formulate my views in such a way that they are clean, concise and free from any conditional logic as much as possible. However, I find it difficult to get rid of all conditional statements. I wonder if some conditional statements in representations are inevitable?

For instance:

@if (Model.UserCanEdit) { <button type="button" id="Edit">Edit</button> } 

There are several options if you have a view that has several elements that can change or show / hide depending on various conditions.

So, what guidelines should be followed regarding where to draw the line, allowing conditional logic in your views? What are some ways to reduce conditional logic in my views, which I may not think about?

Thanks in advance.

+6
source share
3 answers

I would not say that everything is bad with the use of conditional expressions in Views - after all, the main purpose of a view is to actually display data from your model. (And several times conditional statements are required to display the data.)

However - using an abundance of conventions can be a nightmare and, ultimately, readability. It is important to remember that conditional expressions include to the extent of becoming business logic, but to allow them to perform their task as "presentation logic."

Possible alternatives:

Custom HTML Helpers:

If you're not crazy about using conditional expressions, you can explore the use of helpers to clean things up a bit. For more information about this, visit Create Custom HTML Helpers .

Additional views / partial views:

In addition, as many of them point out, using conditional expressions to create one view function, since multiple views should not be the best way to solve this problem.

+10
source

Many code checks may indicate that you are trying to make one view as two separate views.

It might be better to have one check at the controller level and then render another view using the appropriate model. Thus, each performance remains "dumb", and you do not pass on to it more information than necessary.

To directly answer your question - theoretically, you can avoid "all" conditional checks, having a separate view for each such condition. However, you may have an idea that meets several similar objectives and has conventions while maintaining readable code, is not unreasonable.

+3
source

In the best scenarios, try to avoid them as much as possible. They start off quite simply, but later all of this can turn into a lot of spaghetti codes. Saying that this is not always possible and largely inevitable that you will have to do them.

Usually you separate your views or (have several partial views) and then select the if / else bit on the controller. This ensures that your ViewModels will also be different.

-

As a rule, from my experience I always try to understand the business perspective of the views, not the technical ones. Sometimes your two views may look very similar right now, and it may only take an if / else pair to distinguish each other, but they are different in different ways, and itโ€™s obvious that each view will have many new requirements in each line that fully reflect its different from the other. Given the business perspective, you should create separate views and viewing models for both.

+1
source

Source: https://habr.com/ru/post/886655/


All Articles