我有这段代码,我想找到一种方法来尽可能地清理它。希望人们提出以下建议:
if (App.viewablePhrases.Count() == 1)
{
await SetMessageAsync(
msgType: "Practice",
msg1: "Practice Mode",
msg2: $"You have 1 card viewable out of {selCds} {msg}\n\nThe card in your deck has a total of {selPts} {ptsMsg1} {ptsMsg2}",
btnText: App.cardsViewed ? "Continue Practice" : "Start Practice ");
return true;
}
if (App.viewablePhrases.Count() > 1)
{
await SetMessageAsync(
msgType: "Practice",
msg1: "Practice Mode",
msg2: "You have " + App.viewablePhrases.Count() + " cards viewable out of a total of " + selCds + msg +
"\n\nThe cards in your deck have a total of " + selPts + ptsMsg1 + ptsMsg2,
btnText: App.cardsViewed ? "Continue Practice" : "Start Practice ");
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果条件只是用调用SetMessageAsync()方法App.viewablePhrases.Count(),只需1从字符串中删除硬编码的值并使用App.viewablePhrases.Count()。
if(App.viewablePhrases.Count() > 0)
{
var cardString = App.viewablePhrases.Count() == 1 ? "card" : "cards";
await SetMessageAsync(
msgType: "Practice",
msg1: "Practice Mode",
msg2: $"You have {App.viewablePhrases.Count()} {cardString} viewable out of {selCds} {msg}\n\nThe {cardString} in your deck has a total of {selPts} {ptsMsg1} {ptsMsg2}",
btnText: App.cardsViewed ? "Continue Practice" : "Start Practice ";
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果条件不需2