避免针对多个 (9) 条件使用过多的 if else、else if 语句
我的 if else 语句有 9 个条件,我在想是否还有其他替代解决方案可以使代码干净而简短。所有 9 个条件对 asp.net mvc view 执行不同的计算。我已经包含了代码和显示某些条件视图的图像,希望它有意义,我一直在为这种情况寻找更好、更强大的解决方案..谢谢
//Condition 1
//when no selection is made for the 2 dropdownlist (garages & helmets)
if (formvalues["helmets"] == "" && formvalues["garages"] == "")
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = 1;//
ViewBag.SelectedGarages = 1;//
ViewBag.TotalHelmets = 1;
ViewBag.TotalGarages = 1;
ViewBag.TotalAmount = 1;
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 2
//When garages are selected from dropdown & helmets are not selected
else if ((formvalues["helmets"] == "") && (Convert.ToInt32(formvalues["garages"]) > 0))
{
ViewBag.hideHelmet = 0;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = 1;
ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
ViewBag.TotalHelmets = 1;
ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalGarages + trackEventCost.UnitCost;
}
//Condition 3
//When helmets are selected from dropdown & garages are not selected
else if ((formvalues["garages"] == "") && (Convert.ToInt32(formvalues["helmets"]) > 0))
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 0;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedGarages = 1;
ViewBag.SelectedHelmets = Convert.ToInt32(formvalues["helmets"]);
ViewBag.TotalGarages = 1;
ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalHelmets + trackEventCost.UnitCost;
}
//Condition 4
//When garages are not selected from dropdown & helmets dropdownlist is hidden on the view due to unavailablity of helmets for that event
else if (formvalues["garages"] == "" && (formvalues["helmets"] == null))
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 5
//When helmets are not selected from dropdown & garages dropdownlist is hidden on the view due to unavailablity of garages for that event
else if ((formvalues["garages"] == null) && (formvalues["helmets"] == ""))
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = 1;
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 6
//When both helmets and garages dropdown list is not displayed on the view as they are not present in the database
else if (formvalues["helmets"] == null && formvalues["garages"] == null)
{
ViewBag.SelectedHelmets = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.TotalHelmets = 1;
ViewBag.TotalGarages = 1;
ViewBag.hideHelmet = 1;
ViewBag.hideGarages = 1;
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 7
//When garages are selected from dropdown & helmets dropdownlist is hidden on the view due to unavailablity of helmets for that event
else if (formvalues["helmets"] == null && Convert.ToInt32(formvalues["garages"]) > 0)
{
ViewBag.hideHelmet = 0; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalGarages + trackEventCost.UnitCost;
}
//Condition 8
//When helmets are selected from dropdown & garages dropdownlist is hidden on the view due to unavailablity of garages for that event
else if (Convert.ToInt32(formvalues["helmets"]) > 0 && formvalues["garages"] == null)
{
ViewBag.hideHelmet = 1; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 0; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getHelmets.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalHelmets + trackEventCost.UnitCost;
}
//Condition 9
//When garages and helmet both dropdown are selected
else
{
ViewBag.hideHelmet = 0;//for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 0;//for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = Convert.ToInt32(formvalues["helmets"]);
ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getHelmets.UnitCost;
ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalHelmets + ViewBag.TotalGarages + trackEventCost.UnitCost;
}
I have got 9 condition for my if else statement , I was thinking is there any other alternative solution to make the code clean and short. All the 9 condition perform different calculations for asp.net mvc view . I have included the code and an image which shows the view of some conditions, hope it make sense , I was looking for any better and robust solution for this scenario.. thanks
//Condition 1
//when no selection is made for the 2 dropdownlist (garages & helmets)
if (formvalues["helmets"] == "" && formvalues["garages"] == "")
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = 1;//
ViewBag.SelectedGarages = 1;//
ViewBag.TotalHelmets = 1;
ViewBag.TotalGarages = 1;
ViewBag.TotalAmount = 1;
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 2
//When garages are selected from dropdown & helmets are not selected
else if ((formvalues["helmets"] == "") && (Convert.ToInt32(formvalues["garages"]) > 0))
{
ViewBag.hideHelmet = 0;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = 1;
ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
ViewBag.TotalHelmets = 1;
ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalGarages + trackEventCost.UnitCost;
}
//Condition 3
//When helmets are selected from dropdown & garages are not selected
else if ((formvalues["garages"] == "") && (Convert.ToInt32(formvalues["helmets"]) > 0))
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 0;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedGarages = 1;
ViewBag.SelectedHelmets = Convert.ToInt32(formvalues["helmets"]);
ViewBag.TotalGarages = 1;
ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalHelmets + trackEventCost.UnitCost;
}
//Condition 4
//When garages are not selected from dropdown & helmets dropdownlist is hidden on the view due to unavailablity of helmets for that event
else if (formvalues["garages"] == "" && (formvalues["helmets"] == null))
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 5
//When helmets are not selected from dropdown & garages dropdownlist is hidden on the view due to unavailablity of garages for that event
else if ((formvalues["garages"] == null) && (formvalues["helmets"] == ""))
{
ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = 1;
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 6
//When both helmets and garages dropdown list is not displayed on the view as they are not present in the database
else if (formvalues["helmets"] == null && formvalues["garages"] == null)
{
ViewBag.SelectedHelmets = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
ViewBag.TotalHelmets = 1;
ViewBag.TotalGarages = 1;
ViewBag.hideHelmet = 1;
ViewBag.hideGarages = 1;
ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 7
//When garages are selected from dropdown & helmets dropdownlist is hidden on the view due to unavailablity of helmets for that event
else if (formvalues["helmets"] == null && Convert.ToInt32(formvalues["garages"]) > 0)
{
ViewBag.hideHelmet = 0; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 1; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalGarages + trackEventCost.UnitCost;
}
//Condition 8
//When helmets are selected from dropdown & garages dropdownlist is hidden on the view due to unavailablity of garages for that event
else if (Convert.ToInt32(formvalues["helmets"]) > 0 && formvalues["garages"] == null)
{
ViewBag.hideHelmet = 1; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 0; //for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getHelmets.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalHelmets + trackEventCost.UnitCost;
}
//Condition 9
//When garages and helmet both dropdown are selected
else
{
ViewBag.hideHelmet = 0;//for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.hideGarages = 0;//for jquery , This value is passed to jquery script which then decides which field to hide/show
ViewBag.SelectedHelmets = Convert.ToInt32(formvalues["helmets"]);
ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getHelmets.UnitCost;
ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
ViewBag.TotalAmount = ViewBag.TotalHelmets + ViewBag.TotalGarages + trackEventCost.UnitCost;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当然,您可以做的一件事就是改进条件语句的编写方式。我认为,通过将条件提取到方法中来描述检查实际执行的操作,而不是到处添加注释,可能会更好。
Certainly 1 thing you could do is improve how you write your conditionals. I think you might be better served by extracting your conditionals into methods to describe what the check is actually doing instead of adding comments everywhere.
首先,将表单值的提取移至方法的开头,例如:
然后您可能可以非常轻松地设置属性,而无需任何 ifs/else ifs,例如:
更新(关于您的评论/问题):
? : 语句中的运算符 x = 条件 ? value1 : value2
称为条件运算符。如果condition
为 true,则返回value1
,否则返回value2
。int?
是一个 可空整数,可以是整数值,也可以为空。First, move the extraction of the form value to the beginning of the method, e.g:
Then you can probably very easily set the properties without any ifs/else ifs, e.g:
Update (regarding your comment/question):
?:
operator in the statementx = condition ? value1 : value2
is called conditional operator. It returnsvalue1
ifcondition
is true, andvalue2
otherwise.int?
is a nullable integer, which can either have an integer value or be null.为什么不关注任务目标,而关注条件?
条件似乎很简单并且重复。一个示例:
这不需要任何分支逻辑。
Why don't you focus on the assignment targets, instead of the conditions?
The condidition seems to be pretty simple and repeated. One sample:
This doesn't need any branching logic.
您可以将 ViewBag 抽象为一个设置所有数据的函数。这会清理一些东西。例如
You could abstract your ViewBag to a function that sets all of your data. This will clean things up a bit., e.g.
您可以将
某些参数移至具有可选参数的方法,因为您不会在所有情况下设置所有值。其次,您可以使用三元运算符。您可以使用三元运算符,例如。
You could move
To a method with certain parameter as
optional
because you dont set all the values in all cases. Secondly you could use ternary operator. You could use ternary operators like.看一下 策略 模式,对于您的情况,您必须使用每个策略的方法而不是类每个策略。
Take a look at the Strategy pattern, for your case you have to use method per strategy rather than class per strategy.
我将使用 工厂模式 生成您的
ViewBag
基于条件的对象。让工厂生成所需的 ViewBag 并将您的对象分配给它。这是控制台示例中的示例(仅用于快速演示):
I would use the Factory Pattern to generate your
ViewBag
object based on the conditions. Have the factory generate the desiredViewBag
and assign your object to that.Here is an example in a console example (just for quick demonstration):
一些很棒的建议。我喜欢按如下方式处理这些情况。我已经重构了您的代码以完全删除条件。相反,我将每个条件的代码封装到一个方法中,并创建一个具有
int
类型的键和Action
类型的值的哈希表。我为每个头盔/车库组合导出了一个唯一值,然后将其用作相应方法的密钥。我已经包含了您帖子中没有的代码位的虚拟实现,以便我可以编译并运行我的重构示例。希望有帮助。
Some great suggestions. I like to handle these situations as follows. I've refactored your code to remove the conditionals entirely. Instead I've encapsulated the code for each condition into a method and created a hashtable with a key of type
int
and a value of typeAction
. I've derived a unique value for each helmet/garage combination which is then used as the key for the corresponding method.I've included dummy implementations for the bits of code not in your post so that I could compile and run my refactored example. Hope that helps.
怎么样:
构建一个条件类并构建一个列表
然后 linq
What about building a condition class
and bulid a list
then linq: