有时候用
if
//...What're you she wanna go
else if
//...do do do
else
//...and i know something just like this...
感觉代码太多了怎么优雅的、高端的替换?
看个高端的栗子菊:
cs
LedCOM["parity"] = ledData[4] == "N" ? "None" : (ledData[4] == "O" ? "Odd" : (ledData[4] == "E" ? "Even" : "error"));
一行太多不够看,咱就换个行吧
cs
LedCOM["parity"] =
ledData[4] == "N" ? "None"
: (ledData[4] == "O" ? "Odd"
: (ledData[4] == "E" ? "Even" : "error"));
看上面的代码好像很牛逼的样子,其实这段代码可以替换如下:
(1)长得丑的胖梨
cs
if (ledData[4] == "N")
{
LedCOM["parity"] = "None";
}
else
{
if (ledData[4] == "O")
{
LedCOM["parity"] = "Odd";
}
else
{
if (ledData[4] == "E")
{
LedCOM["parity"] = "Even";
}
else
{
LedCOM["parity"] = "error";
}
}
}
(2)身材苗条匀称一般般的
cs
if (ledData[4] == "N")
{
LedCOM["parity"] = "None";
}
else if (ledData[4] == "O")
{
LedCOM["parity"] = "Odd";
}
else if (ledData[4] == "E")
{
LedCOM["parity"] = "Even";
}
else
{
LedCOM["parity"] = "error";
}
(3)漂亮优雅有气质的大漂亮
cs
switch (ledData[4])
{
case "N": LedCOM["parity"] = "None"; break;
case "O": LedCOM["parity"] = "Odd"; break;
case "E": LedCOM["parity"] = "Even"; break;
default: LedCOM["parity"] = "error"; break;
}
(4)穿越回到过去,且看这高贵冷艳、清新脱俗、优雅、不可方物的妖女迷行
cs
LedCOM["parity"] = ledData[4] == "N" ? "None" : (ledData[4] == "O" ? "Odd" : (ledData[4] == "E" ? "Even" : "error"));