Beginner C # code simplicity-code article

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.