1. Merge Similar code:
Remember heart: As long as any two-part code on the screen looks similar, there must be a merge method
Example:
if (!result. Contains ("true"))
{
_repsfc.grantauthoritytorole (authority, role, false);
}
if (result. Contains ("true"))
{
_repsfc.grantauthoritytorole (authority, role, true);
}
After the change:
_repsfc.grantauthoritytorole (authority, role,
Result. Contains ("true"));
2. Multi-use? Yes: no syntax:
Another small case:
if (Misc = = null)
return SFCCatches.LinkP2Cs.Where (i = I.P = = P && I.C = =
c);
Else
return SFCCatches.LinkP2Cs.Where (i = I.P = = P && I.C = =
c && I.misc = = Misc);
After the change:
return SFCCatches.LinkP2Cs.Where (i = I.P = = P && I.C = = C &&
(Misc = = null? true:i.misc = = Misc));
Sometimes this kind of writing is a bit fancy, but after the habit, the actual readability is much higher, especially if the single
When the line code is very long.
3. Postpone the Branch
if (...)
{
A ();
B ();
}
Else
{
A ();
C ();
}
After the change:
A ();
if (...)
{
B ();
}
Else
{
C ();
}
The heart is: any two places that look similar can be simplified.
The technique is that the same part is placed before or after the branch, and the different parts are branches.
Beginner C # code simplicity-code article