Code Review: C # common error reminders, code review

Source: Internet
Author: User
Tags switch case

Code Review: C # common error reminders, code review

Methods shocould not be empty

Do not enter An empty method unless in this case: An abstract class may have empty methods, in order to providedefault implementations for child classes.

Source files shocould not have any duplicatedblocks

Do not duplicate code segments, lines, or strings in the source file. I don't understand.

"Switch case" clses shocould not havetoo rows

"Switch case" the Code in each case should not be too long. If it is too long, you can consider writing a method instead, mainly to enhance code readability.

Nested blocks of code shocould not be leftempty

Do not leave the nested code block empty. For example, if (a> 0) {doSomething ()} else {} should be removed at this time.

Methods shocould not be too complex

The method should not be too complex, otherwise it is difficult to understand and maintain.

Unused private fields shocould be removed

Private member variables that are not used should be removed.

Dead stores shocould be removed

Unused local variables or other dead storage should be removed, that is, when writing a method, if the Defined variables are not found to be used at all, remember to delete the line of code.

"Switch" statements shocould endwith a "default" clause

The switch statement should end with default. This is a defensiveprogramming idea.

Unused method parameters shocould be removed

Unused method parameters should be removed

Control flow statements "if", "for", "while", "switch" and "try" shocould not be nested too deeply

Such nesting as if/for/while/try should not be too complicated

Useless parentheses around expressionsshoshould be removed to prevent any misunderstanding

Do not add meaningless brackets to avoid misunderstanding. For example, if the object types on both sides are the same, do not convert them strongly.

"For" loop stop conditions shouldbe invariant

The result condition of the for Loop cannot be a variable, but a constant.

"Static" members shoshould beaccessed statically

Static members are associated with classes and static methods.

Catches shoshould be combined

I have not understood

Primitives shoshould not be boxed just for "String" conversion

Do not convert the int value to a string in the format of 4 + "", but use Integer. toString (4. Like Integer. parseInt ("I Am a string"), do not be lazy.

Classes shocould not be empty

Do not write empty classes

Unused local variables shocould be removed

Delete unused local variables

"EntrySet ()" shocould be iteratedwhen both the key and value are needed

Directly read English: When only the keys from a map are needed in a loop, iterating thekeySet makes sense. but when both the key and the value are needed, it's moreefficient to iterate theentrySet, which will give access to both the key andvalue, instead.

That is to say, if you only need the Map Key, you can directly use the iterate Map keySet. However, if both the Key and value are required, iterate the Map.

Method parameters, caught exceptions andforeach variables shocould not be reassigned

Method parameters/caught exceptions/foreach variables should not be assigned a value again.

Collection. isEmpty () shocould be used to testfor emptiness

Do not use if (myCollection. size () = 0), but use if (myCollection. isEmpty (), which has higher performance.

Standard outputs shocould not be useddirectly to log anything

Standard output does not print anything directly, that is, when logging, do not use System. out. println ("My Message") is used instead of logger. log ("MyMessage.

Related 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.