Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are conditional statements in views bad news?

I need a quick sanity check. I'm trying to design my views such that they are clean, concise, and as free from any sort of conditional logic as possible. However, I'm having a hard time ridding them of all conditional statements. I'm wondering if having some conditional statements in views is unavoidable?

For example:

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

There are not many options if you have a view that has several elements that can change or be shown/hidden depending on various conditions.

So what guidelines should I follow regarding where to draw the line on allowing conditional logic in your views? What are some ways to reduce conditional logic in my views that I may not be thinking of?

Thanks in advance.

like image 668
Jerad Rose Avatar asked Apr 25 '11 21:04

Jerad Rose


People also ask

What is a conditional statement in philosophy?

Introduction. In English, a conditional is a sentence of the form, “If p, then q” (or of a synonymous form). The part of the sentence following “if” is the antecedent, while the part following “then” is the consequent.

What is the conditional statement?

A conditional statement is a statement that can be written in the form “If P then Q,” where P and Q are sentences. For this conditional statement, P is called the hypothesis and Q is called the conclusion. Intuitively, “If P then Q” means that Q must be true whenever P is true.

What is conditional statement example?

Example: We have a conditional statement If it is raining, we will not play. Let, A: It is raining and B: we will not play. Then; If A is true, that is, it is raining and B is false, that is, we played, then the statement A implies B is false.

What are conditional statements in R?

Conditional if-else statements in R language allow you to make decisions on single value vectors with if and on multi-value vectors with ifelse. Using conditional if statements you can control the flow of the program and change the output based on certain conditions being satisfied.


2 Answers

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

It might be better to have one check at the controller level and then render a different view with an appropriately tailored model. This way, each view remains 'dumb' and you are not passing more information to it than is needed.

To directly answer your question - in theory, sure it is possible to avoid 'all' conditional checks by having a separate view for each such state. However, you can have a view that fits multiple similar purposes and having conditionals while maintaining readable code is not unreasonable.

like image 32
Alex Avatar answered Oct 23 '22 05:10

Alex


I wouldn't say that it is all bad using conditionals in Views - after all the main purpose of a view is actually displaying data from your Model. (And some times conditional statements are required to display the data.)

However - using an abundance of conditionals can make maintenance a nightmare and eventually readability. It's important to remember to not include conditionals to the extent of becoming business logic, but allow them to serve their purpose as "presentation logic".

Possible Alternatives:

Custom HTML Helpers:

If you aren't crazy about using conditionals - you can look into using Helpers to clean up things a bit. For more information on that check out Creating Custom HTML Helpers.

Additional Views / Partial Views:

Also, as many will point out - using conditionals to make a single view function as multiple views should is not the best way to tackle that issue.

like image 108
Rion Williams Avatar answered Oct 23 '22 04:10

Rion Williams