麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Java 編碼規(guī)范

2019-11-14 09:53:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

問(wèn)題

怎么規(guī)范項(xiàng)目組組員的java代碼?

背景

大公司都有自己的編碼規(guī)范,這里參考華為的編碼規(guī)范,并加以適當(dāng)?shù)男薷摹?/p>

方案

一、編碼格式規(guī)范

在Eclipse中導(dǎo)入如下兩個(gè)文件,具體的導(dǎo)入方法可以參考:[http://blog.csdn.net/pengych_321/article/details/50659493]

codetemplates.xml<?xml version="1.0" encoding="UTF-8"?><templates><template id="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name="gettercomment" description="Comment for getter method" context="gettercomment_context" enabled="true" deleted="false">/** * @return Returns the ${bare_field_name}. */</template><template id="org.eclipse.jdt.ui.text.codetemplates.settercomment" name="settercomment" description="Comment for setter method" context="settercomment_context" enabled="true" deleted="false">/** * @param ${param} The ${bare_field_name} to set. */</template><template id="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name="constructorcomment" description="Comment for created constructors" context="constructorcomment_context" enabled="true" deleted="false">/** * ${tags} */</template><template id="org.eclipse.jdt.ui.text.codetemplates.typecomment" name="typecomment" description="Comment for created types" context="typecomment_context" enabled="true" deleted="false">/** * description: * * @author ${user} * @date ${date} * @since 1.0 */</template><template id="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name="fieldcomment" description="Comment for fields" context="fieldcomment_context" enabled="true" deleted="false">/** * 意義,目的和功能,以及被用到的地方&lt;br&gt; */</template><template id="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name="methodcomment" description="Comment for non-overriding methods" context="methodcomment_context" enabled="true" deleted="false">/** * function: * * ${tags} */</template><template id="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name="overridecomment" description="Comment for overriding methods" context="overridecomment_context" enabled="true" deleted="false">/* (non-Javadoc) * ${see_to_overridden} */</template><template id="org.eclipse.jdt.ui.text.codetemplates.newtype" name="newtype" description="Newly created files" context="newtype_context" enabled="true" deleted="false">/* * 文件名:${file_name} * 版權(quán):Copyright by www.midea.com * 修改人:${user} * 修改時(shí)間:${date} */${package_declaration}${typecomment}${type_declaration}</template><template id="org.eclipse.jdt.ui.text.codetemplates.catchblock" name="catchblock" description="Code in new catch blocks" context="catchblock_context" enabled="true" deleted="false">// ${todo} Auto-generated catch block${exception_var}.PRintStackTrace();</template><template id="org.eclipse.jdt.ui.text.codetemplates.methodbody" name="methodbody" description="Code in created method stubs" context="methodbody_context" enabled="true" deleted="false">// ${todo} Auto-generated method stub${body_statement}</template><template id="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name="constructorbody" description="Code in created constructor stubs" context="constructorbody_context" enabled="true" deleted="false">${body_statement}// ${todo} Auto-generated constructor stub</template><template id="org.eclipse.jdt.ui.text.codetemplates.getterbody" name="getterbody" description="Code in created getters" context="getterbody_context" enabled="true" deleted="false">return ${field};</template><template id="org.eclipse.jdt.ui.text.codetemplates.setterbody" name="setterbody" description="Code in created setters" context="setterbody_context" enabled="true" deleted="false">${field} = ${param};</template></templates>codeformate.xml<?xml version="1.0" encoding="UTF-8"?><profiles version="1"><profile name="mcity_codeformat" version="1"><setting id="org.eclipse.jdt.core.formatter.align_type_members_on_columns" value="false"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call" value="18"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_binary_expression" value="18"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_compact_if" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_conditional_expression" value="0"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants" value="0"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_multiple_fields" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration" value="18"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="18"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation" value="4"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration" value="0"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration" value="16"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration" value="0"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration" value="17"/><setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration" value="17"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_after_imports" value="2"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_after_package" value="1"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_field" value="1"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration" value="0"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_imports" value="2"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_member_type" value="1"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_method" value="1"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk" value="1"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_before_package" value="0"/><setting id="org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations" value="2"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration" value="end_of_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_array_initializer" value="end_of_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_block_in_case" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_constant" value="end_of_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration" value="end_of_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_method_declaration" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_switch" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.brace_position_for_type_declaration" value="next_line"/><setting id="org.eclipse.jdt.core.formatter.comment.clear_blank_lines" value="true"/><setting id="org.eclipse.jdt.core.formatter.comment.format_comments" value="true"/><setting id="org.eclipse.jdt.core.formatter.comment.format_header" value="true"/><setting id="org.eclipse.jdt.core.formatter.comment.format_html" value="false"/><setting id="org.eclipse.jdt.core.formatter.comment.format_source_code" value="true"/><setting id="org.eclipse.jdt.core.formatter.comment.indent_parameter_description" value="true"/><setting id="org.eclipse.jdt.core.formatter.comment.indent_root_tags" value="true"/><setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags" value="insert"/><setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter" value="insert"/><setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="99"/><setting id="org.eclipse.jdt.core.formatter.compact_else_if" value="true"/><setting id="org.eclipse.jdt.core.formatter.continuation_indentation" value="1"/><setting id="org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer" value="1"/><setting id="org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line" value="false"/><setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_statements_compare_to_block" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_statements_compare_to_body" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases" value="true"/><setting id="org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch" value="true"/><setting id="org.eclipse.jdt.core.formatter.indentation.size" value="4"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_annotation" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_block" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_assignment_Operator" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_binary_operator" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_assert" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_case" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_conditional" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_for" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_allocation_expression" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_annotation" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_array_initializer" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_parameters" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_throws" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_increments" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_inits" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_parameters" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_invocation_arguments" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_field_declarations" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_parameterized_type_reference" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_ellipsis" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_allocation_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_catch" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_constructor_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_enum_constant" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_after_unary_operator" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_binary_operator" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_brace_in_array_initializer" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_allocation_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_annotation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_cast" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_catch" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_constructor_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_enum_constant" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_for" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_assert" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_default" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_for" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_annotation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_array_initializer" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_parameters" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_throws" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_constant_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_declarations" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_explicitconstructorcall_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_increments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_inits" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_throws" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_parameterized_type_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_ellipsis" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_anonymous_type_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_block" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_constant" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_allocation_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_type_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation_type_member_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_catch" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_constructor_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_enum_constant" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional" value="insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_semicolon" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_before_unary_operator" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_annotation_type_member_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_constructor_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constant" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation" value="do not insert"/><setting id="org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line" value="false"/><setting id="org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line" value="true"/><setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="true"/><setting id="org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line" value="false"/><setting id="org.eclipse.jdt.core.formatter.lineSplit" value="99"/><setting id="org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body" value="0"/><setting id="org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve" value="1"/><setting id="org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line" value="false"/><setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/><setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="4"/><setting id="org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations" value="false"/></profile></profiles>checkstyle.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd"><!-- Generated by RHY @will_awoke --><module name="Checker"> <property name="charset" value="UTF-8"/> <property name="severity" value="warning"/> <!-- Checks for Size Violations. --> <!-- 檢查文件的長(zhǎng)度(行) default max=2000 --> <module name="FileLength"> <property name="max" value="2500"/> </module> <!-- Checks that property files contain the same keys. --> <!-- 檢查**.properties配置文件 是否有相同的key <module name="Translation"> </module> --> <module name="TreeWalker"> <!-- Checks for imports --> <!-- 必須導(dǎo)入類(lèi)的完整路徑,即不能使用*導(dǎo)入所需的類(lèi) --> <module name="AvoidStarImport"/> <!-- 檢查是否從非法的包中導(dǎo)入了類(lèi) illegalPkgs: 定義非法的包名稱(chēng)--> <module name="IllegalImport"/> <!-- defaults to sun.* packages --> <!-- 檢查是否導(dǎo)入了不必顯示導(dǎo)入的類(lèi)--> <module name="RedundantImport"/> <!-- 檢查是否導(dǎo)入的包沒(méi)有使用--> <module name="UnusedImports"/> <!-- Checks for whitespace <module name="EmptyForIteratorPad"/> <module name="MethodParamPad"/> <module name="NoWhitespaceAfter"/> <module name="NoWhitespaceBefore"/> <module name="OperatorWrap"/> <module name="ParenPad"/> <module name="TypecastParenPad"/> <module name="WhitespaceAfter"/> <module name="WhitespaceAround"/> --> <!-- 檢查類(lèi)和接口的javadoc 默認(rèn)不檢查author 和version tags authorFormat: 檢查author標(biāo)簽的格式 versionFormat: 檢查version標(biāo)簽的格式 scope: 可以檢查的類(lèi)的范圍,例如:public只能檢查public修飾的類(lèi),private可以檢查所有的類(lèi) excludeScope: 不能檢查的類(lèi)的范圍,例如:public,public的類(lèi)將不被檢查,但訪問(wèn)權(quán)限小于public的類(lèi)仍然會(huì)檢查,其他的權(quán)限以此類(lèi)推 tokens: 該屬性適用的類(lèi)型,例如:CLASS_DEF,INTERFACE_DEF --> <module name="JavadocType"> <property name="authorFormat" value="/S"/> <property name="scope" value="protected"/> <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/> </module> <!-- 檢查方法的javadoc的注釋 scope: 可以檢查的方法的范圍,例如:public只能檢查public修飾的方法,private可以檢查所有的方法 allowMissingParamTags: 是否忽略對(duì)參數(shù)注釋的檢查 allowMissingThrowsTags: 是否忽略對(duì)throws注釋的檢查 allowMissingReturnTag: 是否忽略對(duì)return注釋的檢查 --> <module name="JavadocMethod"> <property name="scope" value="private"/> <property name="allowMissingParamTags" value="false"/> <property name="allowMissingThrowsTags" value="false"/> <property name="allowMissingReturnTag" value="false"/> <property name="tokens" value="METHOD_DEF"/> <property name="allowUndeclaredRTE" value="true"/> <property name="allowThrowsTagsForSubclasses" value="true"/> <!--允許get set 方法沒(méi)有注釋--> <property name="allowMissingPropertyJavadoc" value="true"/> </module> <!-- 檢查類(lèi)變量的注釋 scope: 檢查變量的范圍,例如:public只能檢查public修飾的變量,private可以檢查所有的變量 --> <module name="JavadocVariable"> <property name="scope" value="private"/> </module> <!--option: 定義左大括號(hào)'{'顯示位置,eol在同一行顯示,nl在下一行顯示 maxLineLength: 大括號(hào)'{'所在行行最多容納的字符數(shù) tokens: 該屬性適用的類(lèi)型,例:CLASS_DEF,INTERFACE_DEF,METHOD_DEF,CTOR_DEF --> <module name="LeftCurly"> <property name="option" value="nl"/> </module> <!-- NeedBraces 檢查是否應(yīng)該使用括號(hào)的地方?jīng)]有加括號(hào) tokens: 定義檢查的類(lèi)型 --> <module name="NeedBraces"/> <!-- Checks the placement of right curly braces ('}') for else, try, and catch tokens. The policy to verify is specified using property option. option: 右大括號(hào)是否單獨(dú)一行顯示 tokens: 定義檢查的類(lèi)型 --> <module name="RightCurly"> <property name="option" value="alone"/> </module> <!-- 檢查在重寫(xiě)了equals方法后是否重寫(xiě)了hashCode方法 --> <module name="EqualsHashCode"/> <!-- Checks for illegal instantiations where a factory method is preferred. Rationale: Depending on the project, for some classes it might be preferable to create instances through factory methods rather than calling the constructor. A simple example is the java.lang.Boolean class. In order to save memory and CPU cycles, it is preferable to use the predefined constants TRUE and FALSE. Constructor invocations should be replaced by calls to Boolean.valueOf(). Some extremely performance sensitive projects may require the use of factory methods for other classes as well, to enforce the usage of number caches or object pools. --> <module name="IllegalInstantiation"> <property name="classes" value="java.lang.Boolean"/> </module> <!-- Checks for Naming Conventions. 命名規(guī)范 --> <!-- local, final variables, including catch parameters --> <module name="LocalFinalVariableName"/> <!-- local, non-final variables, including catch parameters--> <module name="LocalVariableName"/> <!-- static, non-final fields --> <module name="StaticVariableName"> <property name="format" value="(^[A-Z0-9_]{0,19}$)"/> </module> <!-- packages --> <module name="PackageName" > <property name="format" value="^[a-z]+(/.[a-z][a-z0-9]*)*$"/> </module> <!-- classes and interfaces --> <module name="TypeName"> <property name="format" value="(^[A-Z][a-zA-Z0-9]{0,19}$)"/> </module> <!-- methods --> <module name="MethodName"> <property name="format" value="(^[a-z][a-zA-Z0-9]{0,19}$)"/> </module> <!-- non-static fields --> <module name="MemberName"> <property name="format" value="(^[a-z][a-z0-9][a-zA-Z0-9]{0,19}$)"/> </module> <!-- parameters --> <module name="ParameterName"> <property name="format" value="(^[a-z][a-zA-Z0-9_]{0,19}$)"/> </module> <!-- constants (static, final fields) --> <module name="ConstantName"> <property name="format" value="(^[A-Z0-9_]{0,19}$)"/> </module> <!-- 代碼縮進(jìn) --> <module name="Indentation"> </module> <!-- Checks for redundant exceptions declared in throws clause such as duplicates, unchecked exceptions or subclasses of another declared exception. 檢查是否拋出了多余的異常 <module name="RedundantThrows"> <property name="logLoadErrors" value="true"/> <property name="suppressLoadErrors" value="true"/> </module> --> <!-- Checks for overly complicated boolean expressions. Currently finds code like if (b == true), b || true, !false, etc. 檢查boolean值是否冗余的地方 Rationale: Complex boolean logic makes code hard to understand and maintain. --> <module name="SimplifyBooleanExpression"/> <!-- Checks for overly complicated boolean return statements. For example the following code 檢查是否存在過(guò)度復(fù)雜的boolean返回值 if (valid()) return false; else return true; could be written as return !valid(); The Idea for this Check has been shamelessly stolen from the equivalent PMD rule. --> <module name="SimplifyBooleanReturn"/> <!-- Checks that a class which has only private constructors is declared as final.只有私有構(gòu)造器的類(lèi)必須聲明為final--> <module name="FinalClass"/> <!-- Make sure that utility classes (classes that contain only static methods or fields in their API) do not have a public constructor. 確保Utils類(lèi)(只提供static方法和屬性的類(lèi))沒(méi)有public構(gòu)造器。 Rationale: Instantiating utility classes does not make sense. Hence the constructors should either be private or (if you want to allow subclassing) protected. A common mistake is forgetting to hide the default constructor. If you make the constructor protected you may want to consider the following constructor implementation technique to disallow instantiating subclasses: public class StringUtils // not final to allow subclassing { protected StringUtils() { throw new UnsupportedOperationException(); // prevents calls from subclass } public static int count(char c, String s) { // ... } } <module name="HideUtilityClassConstructor"/> --> <!-- Checks visibility of class members. Only static final members may be public; other class members must be private unless property protectedAllowed or packageAllowed is set. 檢查class成員屬性可見(jiàn)性。只有static final 修飾的成員是可以public的。其他的成員屬性必需是private的,除非屬性protectedAllowed或者packageAllowed設(shè)置了true. Public members are not flagged if the name matches the public member regular expression (contains "^serialVersionUID$" by default). Note: Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default pattern to allow CMP for EJB 1.1 with the default settings. With EJB 2.0 it is not longer necessary to have public access for persistent fields, hence the default has been changed. Rationale: Enforce encapsulation. 強(qiáng)制封裝 --> <module name="VisibilityModifier"/> <!-- 每一行只能定義一個(gè)變量 --> <module name="MultipleVariableDeclarations"> </module> <!-- Checks the style of array type definitions. Some like Java-style: public static void main(String[] args) and some like C-style: public static void main(String args[]) 檢查再定義數(shù)組時(shí),采用java風(fēng)格還是c風(fēng)格,例如:int[] num是java風(fēng)格,int num[]是c風(fēng)格。默認(rèn)是java風(fēng)格--> <module name="ArrayTypeStyle"> </module> <!-- Checks that there are no "magic numbers", where a magic number is a numeric literal that is not defined as a constant. By default, -1, 0, 1, and 2 are not considered to be magic numbers. <module name="MagicNumber"> </module> --> <!-- A check for TODO: comments. Actually it is a generic regular expression matcher on Java comments. To check for other patterns in Java comments, set property format. 檢查是否存在TODO(待處理) TODO是javaIDE自動(dòng)生成的。一般代碼寫(xiě)完后要去掉。 --> <module name="TodoComment"/> <!-- Checks that long constants are defined with an upper ell. That is ' L' and not 'l'. This is in accordance to the Java Language Specification, Section 3.10.1. 檢查是否在long類(lèi)型是否定義了大寫(xiě)的L.字母小寫(xiě)l和數(shù)字1(一)很相似。 looks a lot like 1. --> <module name="UpperEll"/> <!-- Checks that switch statement has "default" clause. 檢查switch語(yǔ)句是否有‘default’從句 Rationale: It's usually a good idea to introduce a default case in every switch statement. Even if the developer is sure that all currently possible cases are covered, this should be expressed in the default branch, e.g. by using an assertion. This way the code is protected aginst later changes, e.g. introduction of new types in an enumeration type. --> <module name="MissingSwitchDefault"/> <!--檢查switch中case后是否加入了跳出語(yǔ)句,例如:return、break、throw、continue --> <module name="FallThrough"/> <!-- Checks the number of parameters of a method or constructor. max default 7個(gè). --> <module name="ParameterNumber"> <property name="max" value="5"/> </module> <!-- 每行字符數(shù) --> <module name="LineLength"> <property name="max" value="200"/> </module> <!-- Checks for long methods and constructors. max default 150行. max=300 設(shè)置長(zhǎng)度300 --> <module name="MethodLength"> <property name="max" value="300"/> </module> <!-- ModifierOrder 檢查修飾符的順序,默認(rèn)是 public,protected,private,abstract,static,final,transient,volatile,synchronized,native --> <module name="ModifierOrder"> </module> <!-- 檢查是否有多余的修飾符,例如:接口中的方法不必使用public、abstract修飾 --> <module name="RedundantModifier"> </module> <!--- 字符串比較必須使用 equals() --> <module name="StringLiteralEquality"> </module> <!-- if-else嵌套語(yǔ)句個(gè)數(shù) 最多4層 --> <module name="NestedIfDepth"> <property name="max" value="3"/> </module> <!-- try-catch 嵌套語(yǔ)句個(gè)數(shù) 最多2層 --> <module name="NestedTryDepth"> <property name="max" value="2"/> </module> <!-- 返回個(gè)數(shù) --> <module name="ReturnCount"> <property name="max" value="5"/> <property name="format" value="^$"/> </module> </module></module>

二、java代碼規(guī)范

傳說(shuō)中的華為編碼軍規(guī) …

轉(zhuǎn)至元數(shù)據(jù)起始1.1 排版1.1.1 規(guī)則規(guī)則1 程序塊要采用縮進(jìn)風(fēng)格編寫(xiě),縮進(jìn)的空格數(shù)為4個(gè),不允許使用TAB縮進(jìn)。(1.42+)說(shuō)明:縮進(jìn)使程序更易閱讀,使用空格縮進(jìn)可以適應(yīng)不同操作系統(tǒng)與不同開(kāi)發(fā)工具。規(guī)則2 分界符(如大括號(hào)‘{’和‘}’)應(yīng)各獨(dú)占一行,同時(shí)與引用它們的語(yǔ)句左對(duì)齊。在函數(shù)體的開(kāi)始、類(lèi)和接口的定義、以及if、for、do、while、switch、case語(yǔ)句中的程序或者static、,synchronized等語(yǔ)句塊中都要采用如上的縮進(jìn)方式。(1.42+) 示例: if (a>b) { doStart(); }規(guī)則3 較長(zhǎng)的語(yǔ)句、表達(dá)式或參數(shù)(>80字符)要分成多行書(shū)寫(xiě),長(zhǎng)表達(dá)式要在低優(yōu)先級(jí)操作符處劃分新行,操作符放在新行之首,劃分出的新行要進(jìn)行適當(dāng)?shù)目s進(jìn),使排版整齊,語(yǔ)句可讀。(1.42+)示例: if (logger.isDebugEnabled()) { logger.debug("session destroyed,call-id" + event.getSession().getCallId()); }規(guī)則4 不允許把多個(gè)短語(yǔ)句寫(xiě)在一行中,即一行只寫(xiě)一條語(yǔ)句(1.42+) 說(shuō)明:閱讀代碼更加清晰 示例:如下例子不符合規(guī)范。 Object o = new Object(); Object b = null;規(guī)則5 if, for, do, while, case, switch, default 等語(yǔ)句自占一行,且if, for, do, while,switch等語(yǔ)句的執(zhí)行語(yǔ)句無(wú)論多少都要加括號(hào){},case 的執(zhí)行語(yǔ)句中如果定義變量必須加括號(hào){}。(1.42+) 說(shuō)明:閱讀代碼更加清晰,減少錯(cuò)誤產(chǎn)生 示例: if (a>b){doStart();} case x:{int i = 9;}規(guī)則6 相對(duì)獨(dú)立的程序塊之間、變量說(shuō)明之后必須加空行。 (1.42+)說(shuō)明:閱讀代碼更加清晰示例:if(a > b){doStart();}//此處是空行return;規(guī)則7 在兩個(gè)以上的關(guān)鍵字、變量、常量進(jìn)行對(duì)等操作時(shí),它們之間的操作符之前、之后或者前后要加空格;進(jìn)行非對(duì)等操作時(shí),如果是關(guān)系密切的立即操作符(如.),后不應(yīng)加空格。(1.42+)說(shuō)明:閱讀代碼更加清晰示例:if (a == b){objectA.doStart();}a *= 2;1.1.2 建議建議1 類(lèi)屬性和類(lèi)方法不要交叉放置,不同存取范圍的屬性或者方法也盡量不要交叉放置。(1.42+)格式:類(lèi)定義{ 類(lèi)的公有屬性定義 類(lèi)的保護(hù)屬性定義 類(lèi)的私有屬性定義 類(lèi)的公有方法定義 類(lèi)的保護(hù)方法定義 類(lèi)的私有方法定義}建議2 修飾詞按照指定順序書(shū)寫(xiě):[訪問(wèn)權(quán)限][static][final] 。(1.42+) 示例: public static final String str = “abc”;1.2 注釋1.2.1 規(guī)則規(guī)則1 源程序注釋量必須在30%以上。(1.42+)說(shuō)明:由于每個(gè)文件的代碼注釋不一定都可以達(dá)到30%,建議以一個(gè)系統(tǒng)內(nèi)部模塊作為單位進(jìn)行檢查規(guī)則2 包的注釋?zhuān)簩?xiě)入一個(gè)名為 package.html 的HTML格式的說(shuō)明文件放入包所在路徑。包的注釋內(nèi)容:簡(jiǎn)述本包的作用、詳細(xì)描述本包的內(nèi)容、產(chǎn)品模塊名稱(chēng)和版本、公司版權(quán)。(1.42+)說(shuō)明:方便JavaDoc收集,方便對(duì)包的了解示例:com/huawei/iin/websmap/comm/package.html<html><body><p>一句話簡(jiǎn)述。<p>詳細(xì)描述。<p>產(chǎn)品模塊名稱(chēng)和版本<br>公司版權(quán)信息</body></html>示例:<html><body><P>為 WEBSMAP 提供通信類(lèi),上層業(yè)務(wù)使用本包的通信類(lèi)與 SMP-B 進(jìn)行通信。<p>詳細(xì)描述。。。。。。。。<p>IIN V100R001 WEBSMAP<br>(C) 版權(quán)所有 2000-2001 華為技術(shù)有限公司</body></html>規(guī)則3 類(lèi)和接口的注釋放在class 或者 interface 關(guān)鍵字之前,import 關(guān)鍵字之后。注釋主要是一句話功能簡(jiǎn)述與功能詳細(xì)描述。類(lèi)注釋使用“/** */”注釋方式(1.42+)說(shuō)明:方便JavaDoc收集,沒(méi)有import可放在package之后。注釋可根據(jù)需要列出:作者、內(nèi)容、功能、與其它類(lèi)的關(guān)系等。功能詳細(xì)描述部分說(shuō)明該類(lèi)或者接口的功能、作用、使用方法和注意事項(xiàng),每次修改后增加作者和更新版本號(hào)和日期,@since 表示從那個(gè)版本開(kāi)始就有這個(gè)類(lèi)或者接口,@deprecated 表示不建議使用該類(lèi)或者接口。/** * 〈一句話功能簡(jiǎn)述〉 * 〈功能詳細(xì)描述〉 * @author [作者](必須)* @see [相關(guān)類(lèi)/方法](可選) * @since [產(chǎn)品/模塊版本] (必須) * @deprecated (可選) */示例:package com.huawei.iin.logwebsmap.comm;import java.util.*;/** * LogManager 類(lèi)集中控制對(duì)日志讀寫(xiě)的操作。 * 全部為靜態(tài)變量和靜態(tài)方法,對(duì)外提供統(tǒng)一接口。分配對(duì)應(yīng)日志類(lèi)型的讀寫(xiě)器, * 讀取或?qū)懭敕蠗l件的日志紀(jì)錄。 * @author 張三,李四,王五* @see LogIteraotor * @see BasicLog * @since CommonLog1.0 */public class LogManager規(guī)則4 類(lèi)屬性(成員變量)、公有和保護(hù)方法注釋?zhuān)簩?xiě)在類(lèi)屬性、公有和保護(hù)方法上面,注釋方式為“/** */”.(1.42+)示例:/** * 注釋內(nèi)容 */private String logType;/** * 注釋內(nèi)容 */public void write()規(guī)則5 公有和保護(hù)方法注釋內(nèi)容:列出方法的一句話功能簡(jiǎn)述、功能詳細(xì)描述、輸入?yún)?shù)、輸出參數(shù)、返回值、異常等。(1.42+)格式:/** * 〈一句話功能簡(jiǎn)述〉 * 〈功能詳細(xì)描述〉 * @param [參數(shù)1] [參數(shù)1說(shuō)明] * @param [參數(shù)2] [參數(shù)2說(shuō)明] * @return [返回類(lèi)型說(shuō)明] * @exception/throws [異常類(lèi)型] [異常說(shuō)明] * @see [類(lèi)、類(lèi)#方法、類(lèi)#成員]* @since [起始版本] * @deprecated */說(shuō)明:@since 表示從那個(gè)版本開(kāi)始就有這個(gè)方法,如果是最初版本就存在的方法無(wú)需說(shuō)明;@exception或throws 列出可能仍出的異常;@deprecated 表示不建議使用該方法。示例: /** * 根據(jù)日志類(lèi)型和時(shí)間讀取日志。 * 分配對(duì)應(yīng)日志類(lèi)型的LogReader, 指定類(lèi)型、查詢(xún)時(shí)間段、條件和反復(fù)器緩沖數(shù), * 讀取日志記錄。查詢(xún)條件為null或0的表示沒(méi)有限制,反復(fù)器緩沖數(shù)為0讀不到日志。 * 查詢(xún)時(shí)間為左包含原則,即 [startTime, endTime) 。 * @param logTypeName 日志類(lèi)型名(在配置文件中定義的) * @param startTime 查詢(xún)?nèi)罩镜拈_(kāi)始時(shí)間 * @param endTime 查詢(xún)?nèi)罩镜慕Y(jié)束時(shí)間 * @param logLevel 查詢(xún)?nèi)罩镜募?jí)別 * @param userName 查詢(xún)?cè)撚脩?hù)的日志 * @param bufferNum 日志反復(fù)器緩沖記錄數(shù) * @return 結(jié)果集,日志反復(fù)器 * @since 1.2 */ public static LogIterator read(String logType, Date startTime, Date endTime, int logLevel, String userName, int bufferNum)規(guī)則6 對(duì)于方法內(nèi)部用throw語(yǔ)句拋出的異常,必須在方法的注釋中標(biāo)明,對(duì)于所調(diào)用的其他方法所拋出的異常,選擇主要的在注釋中說(shuō)明。 對(duì)于非RuntimeException,即throws子句聲明會(huì)拋出的異常,必須在方法的注釋中標(biāo)明。(1.42+)說(shuō)明:異常注釋用@exception或@throws表示,在JavaDoc中兩者等價(jià),但推薦用@exception標(biāo)注Runtime異常,@throws標(biāo)注非Runtime異常。異常的注釋必須說(shuō)明該異常的含義及什么條件下拋出該異常。規(guī)則7 注釋?xiě)?yīng)與其描述的代碼相近,對(duì)代碼的注釋?xiě)?yīng)放在其上方,并與其上面的代碼用空行隔開(kāi),注釋與所描述內(nèi)容進(jìn)行同樣的縮排。(1.42+)說(shuō)明:可使程序排版整齊,并方便注釋的閱讀與理解。示例:/* * 注釋*/public void example2( ){ // 注釋 CodeBlock One // 注釋 CodeBlock Two}/* * 注釋*/public void example( ){ // 注釋 CodeBlock One // 注釋 CodeBlock Two}規(guī)則8 對(duì)于switch語(yǔ)句下的case語(yǔ)句,必須在每個(gè)case分支結(jié)束前加上break語(yǔ)句。(1.42+)說(shuō)明:break才能真正表示該switch執(zhí)行結(jié)束,不然可能會(huì)進(jìn)入該case以后的分支。至于語(yǔ)法上合法的場(chǎng)景“一個(gè)case后進(jìn)入下一個(gè)case處理”,應(yīng)該在編碼設(shè)計(jì)上就避免。規(guī)則9 修改代碼同時(shí)修改相應(yīng)的注釋?zhuān)员WC注釋與代碼的一致性。不再有用的注釋要?jiǎng)h除。(1.42+)規(guī)則10 注釋的內(nèi)容要清楚、明了,含義準(zhǔn)確,防止注釋二義性。(1.42+)說(shuō)明:錯(cuò)誤的注釋不但無(wú)益反而有害。規(guī)則11 避免在注釋中使用縮寫(xiě),特別是不常用縮寫(xiě)。(1.42+)說(shuō)明:在使用縮寫(xiě)時(shí)或之前,應(yīng)對(duì)縮寫(xiě)進(jìn)行必要的說(shuō)明。規(guī)則12 對(duì)重載父類(lèi)的方法必須進(jìn)行@Override聲明(5.0+)說(shuō)明:可清楚說(shuō)明此方法是重載父類(lèi)的方法,保證重載父類(lèi)的方法時(shí)不會(huì)因?yàn)閱卧~寫(xiě)錯(cuò)而造成錯(cuò)誤(寫(xiě)錯(cuò)方法名或者參數(shù)個(gè)數(shù),類(lèi)型都會(huì)編譯無(wú)法通過(guò))示例: @Override public void doRequest(SipServletRequest req) throws ServletException, IOException1.2.2 建議建議1 避免在一行代碼或表達(dá)式的中間插入注釋。(1.42+)說(shuō)明:除非必要,不應(yīng)在代碼或表達(dá)中間插入注釋?zhuān)駝t容易使代碼可理解性變差。建議2 在代碼的功能、意圖層次上進(jìn)行注釋?zhuān)峁┯杏谩㈩~外的信息。(1.42+)說(shuō)明:注釋的目的是解釋代碼的目的、功能和采用的方法,提供代碼以外的信息,幫助讀者理解代碼,防止沒(méi)必要的重復(fù)注釋信息。示例:如下注釋意義不大。// 如果 receiveFlag 為真if (receiveFlag)而如下的注釋則給出了額外有用的信息。// 如果從連結(jié)收到消息if (receiveFlag)建議3 對(duì)關(guān)鍵變量的定義和分支語(yǔ)句(條件分支、循環(huán)語(yǔ)句等)必須編寫(xiě)注釋。(1.42+)說(shuō)明:這些語(yǔ)句往往是程序?qū)崿F(xiàn)某一特定功能的關(guān)鍵,對(duì)于維護(hù)人員來(lái)說(shuō),良好的注釋幫助更好的理解程序,有時(shí)甚至優(yōu)于看設(shè)計(jì)文檔。建議4 注釋?xiě)?yīng)考慮程序易讀及外觀排版的因素,使用的語(yǔ)言若是中、英兼有的,建議多使用中文,除非能用非常流利準(zhǔn)確的英文表達(dá)。中文注釋中需使用中文標(biāo)點(diǎn)。方法和類(lèi)描述的第一句話盡量使用簡(jiǎn)潔明了的話概括一下功能,然后加以句號(hào)。接下來(lái)的部分可以詳細(xì)描述。(1.42+)說(shuō)明:注釋語(yǔ)言不統(tǒng)一,影響程序易讀性和外觀排版,出于對(duì)維護(hù)人員的考慮,建議使用中文。JavaDoc工具收集簡(jiǎn)介的時(shí)候使用選取第一句話。建議5 方法內(nèi)的單行注釋使用 //。(1.42+)說(shuō)明:調(diào)試程序的時(shí)候可以方便的使用 /* 。。。*/ 注釋掉一長(zhǎng)段程序。建議6 一些復(fù)雜的代碼需要說(shuō)明。(1.42+)示例:這里主要是對(duì)閏年算法的說(shuō)明。 //1. 如果能被4整除,是閏年; //2. 如果能被100整除,不是閏年; //3. 如果能被400整除,是閏年。建議7 使用Html標(biāo)簽使JavaDoc生成更加美觀。(1.42+)示例:/** * Returns a hash code for this string. The hash code for a * <code>String</code> object is computed as * <blockquote><pre> * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre></blockquote> * using <code>int</code> arithmetic, where <code>s[i]</code> is the * <i>i</i>th character of the string, <code>n</code> is the length * of * the string, and <code>^</code> indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */public int hashCode() 生成后的JavaDoc圖1 生成后的JavaDoc1.3 命名1.3.1 規(guī)則規(guī)則1 類(lèi)名和接口使用類(lèi)意義完整的英文描述,每個(gè)英文單詞的首字母使用大寫(xiě)、其余字母使用小寫(xiě)的大小寫(xiě)混合法。(1.42+)示例:OrderInformation, CustomerList, LogManager, LogConfig, SmpTransaction規(guī)則2 方法名使用類(lèi)意義完整的英文描述:第一個(gè)單詞的字母使用小寫(xiě)、剩余單詞首字母大寫(xiě)其余字母小寫(xiě)的大小寫(xiě)混合法。(1.42+)示例:private void calculateRate();public void addNeWorder();規(guī)則3 方法中,存取屬性的方法采用setter 和 getter方法,動(dòng)作方法采用動(dòng)詞和動(dòng)賓結(jié)構(gòu)。(1.42+)格式:get + 非布爾屬性名()is + 布爾屬性名()set + 屬性名()動(dòng)詞()動(dòng)詞 + 賓語(yǔ)()示例:public String getType();public boolean isFinished();public void setVisible(boolean);public void show();public void addKeyListener(Listener);規(guī)則4 屬性名使用意義完整的英文描述,第一個(gè)單詞的字母使用小寫(xiě),剩余單詞首字母大寫(xiě)其余字母小寫(xiě)的大小寫(xiě)混合法。屬性名不能與方法名相同。(1.42+)示例:private customerName;private orderNumber;private smpSession;規(guī)則5 常量名使用全大寫(xiě)的英文描述,英文單詞之間用下劃線分隔開(kāi),并且使用 static final修飾。(1.42+)示例:public static final int MAX_VALUE = 1000;public static final String DEFAULT_START_DATE = "2001-12-08";1.3.2 建議建議1 包名采用域后綴倒置的加上自定義的包名,采用小寫(xiě)字母,都應(yīng)該以com.huawei開(kāi)頭(不包括一些特殊原因)。在部門(mén)內(nèi)部應(yīng)該規(guī)劃好包名的范圍,防止產(chǎn)生沖突。部門(mén)內(nèi)部產(chǎn)品使用部門(mén)的名稱(chēng)加上模塊名稱(chēng)。產(chǎn)品線的產(chǎn)品使用產(chǎn)品的名稱(chēng)加上模塊的名稱(chēng)。(1.42+)說(shuō)明:除特殊原因包結(jié)構(gòu)都必須以com.huawei開(kāi)頭,如果因?yàn)镺EM合作等關(guān)系,可以不做要求。格式:com.huawei.產(chǎn)品名.模塊名稱(chēng)示例:融合WEBSMAP包名 com.huawei.iin.websmap建議2 通過(guò)對(duì)函數(shù)或過(guò)程、變量、結(jié)構(gòu)等正確的命名以及合理地組織代碼的結(jié)構(gòu),使代碼成為自注釋的。(1.42+)說(shuō)明:清晰準(zhǔn)確的函數(shù)、變量等的命名,可增加代碼可讀性,并減少不必要的注釋。建議3 常用組件類(lèi)的命名以組件名加上組件類(lèi)型名結(jié)尾。(1.42+)示例:application 類(lèi)型的,命名以App 結(jié)尾——MainAppFrame 類(lèi)型的,命名以Frame 結(jié)尾——TopoFramePanel 類(lèi)型的,建議命名以Panel 結(jié)尾——CreateCircuitPanelBean 類(lèi)型的,建議命名以Bean 結(jié)尾——DataAccessBeanEJB 類(lèi)型的,建議命名以EJB 結(jié)尾——DBProxyEJBApplet 類(lèi)型的,建議命名以Applet 結(jié)尾——PictureShowApplet建議4 如果函數(shù)名超過(guò)15 個(gè)字母,可采用以去掉元音字母的方法或者以行業(yè)內(nèi)約定俗成的縮寫(xiě)方式縮寫(xiě)函數(shù)名。(1.42+)示例:getCustomerInformation() 改為 getCustomerInfo()建議5 準(zhǔn)確地確定成員函數(shù)的存取控制符號(hào):只是該類(lèi)內(nèi)部調(diào)用的函數(shù)使用 private 屬性,繼承類(lèi)可以使用的使用protected屬性,同包類(lèi)可以調(diào)用的使用默認(rèn)屬性(不加屬性控制符號(hào)),對(duì)外公開(kāi)的函數(shù)使用public屬性(1.42+)示例:protected void getUserName(){。。。。。。}private void calculateRate(){。。。。。。}建議6 含有集合意義的屬性命名,盡量包含其復(fù)數(shù)的意義。(1.42+)示例:customers, orderItems1.4 編碼1.4.1 規(guī)則規(guī)則1 數(shù)據(jù)庫(kù)操作、IO操作等需要使用結(jié)束close()的對(duì)象必須在try -catch-finally 的finally中close(),如果有多個(gè)IO對(duì)象需要close(),需要分別對(duì)每個(gè)對(duì)象的close()方法進(jìn)行try-catch,防止一個(gè)IO對(duì)象關(guān)閉失敗其他IO對(duì)象都未關(guān)閉。(1.42+)示例:try{ // ... ...}catch(IOException ioe){ //... ...}finally{ try { out.close(); } catch (IOException ioe) { //... ...}try { in.close(); } catch (IOException ioe) { //... ... }}規(guī)則2 系統(tǒng)非正常運(yùn)行產(chǎn)生的異常捕獲后,如果不對(duì)該異常進(jìn)行處理,則應(yīng)該記錄日志。 (1.42+)說(shuō)明:此規(guī)則指通常的系統(tǒng)非正常運(yùn)行產(chǎn)生的異常,不包括一些基于異常的設(shè)計(jì)。若有特殊原因必須用注釋加以說(shuō)明。示例:try{ //.... ...}catch (IOException ioe){ logger.error(ioe);}規(guī)則3 自己拋出的異常必須要填寫(xiě)詳細(xì)的描述信息。(1.42+)說(shuō)明:便于問(wèn)題定位。示例:throw new IOException("Writing data error! Data: " + data.toString());規(guī)則4 運(yùn)行時(shí)異常使用RuntimeException的子類(lèi)來(lái)表示,不用在可能拋出異常的方法聲明上加throws子句。非運(yùn)行期異常是從Exception繼承而來(lái)的,必須在方法聲明上加throws子句。(1.42+)說(shuō)明:非運(yùn)行期異常是由外界運(yùn)行環(huán)境決定異常拋出條件的異常,例如文件操作,可能受權(quán)限、磁盤(pán)空間大小的影響而失敗,這種異常是程序本身無(wú)法避免的,需要調(diào)用者明確考慮該異常出現(xiàn)時(shí)該如何處理方法,因此非運(yùn)行期異常必須有throws子句標(biāo)出,不標(biāo)出或者調(diào)用者不捕獲該類(lèi)型異常都會(huì)導(dǎo)致編譯失敗,從而防止程序員本身疏忽。運(yùn)行期異常是程序在運(yùn)行過(guò)程中本身考慮不周導(dǎo)致的異常,例如傳入錯(cuò)誤的參數(shù)等。拋出運(yùn)行期異常的目的是防止異常擴(kuò)散,導(dǎo)致定位困難。因此在做異常體系設(shè)計(jì)時(shí)要根據(jù)錯(cuò)誤的性質(zhì)合理選擇自定義異常的繼承關(guān)系。還有一種異常是Error 繼承而來(lái)的,這種異常由虛擬機(jī)自己維護(hù),表示發(fā)生了致命錯(cuò)誤,程序無(wú)法繼續(xù)運(yùn)行例如內(nèi)存不足。我們自己的程序不應(yīng)該捕獲這種異常,并且也不應(yīng)該創(chuàng)建該種類(lèi)型的異常。規(guī)則5 在程序中使用異常處理還是使用錯(cuò)誤返回碼處理,根據(jù)是否有利于程序結(jié)構(gòu)來(lái)確定,并且異常和錯(cuò)誤碼不應(yīng)該混合使用,推薦使用異常。(1.42+)說(shuō)明:一個(gè)系統(tǒng)或者模塊應(yīng)該統(tǒng)一規(guī)劃異常類(lèi)型和返回碼的含義。但是不能用異常來(lái)做一般流程處理的方式,不要過(guò)多地使用異常,異常的處理效率比條件分支低,而且異常的跳轉(zhuǎn)流程難以預(yù)測(cè)。注意:Java 5.0 程序內(nèi)部的錯(cuò)誤碼可以使用枚舉來(lái)表示。規(guī)則6 注意運(yùn)算符的優(yōu)先級(jí),并用括號(hào)明確表達(dá)式的操作順序,避免使用默認(rèn)優(yōu)先級(jí)。(1.42+)說(shuō)明:防止閱讀程序時(shí)產(chǎn)生誤解,防止因默認(rèn)的優(yōu)先級(jí)與設(shè)計(jì)思想不符而導(dǎo)致程序出錯(cuò)。示例:下列語(yǔ)句中的表達(dá)式word = (high << 8) | low (1)if ((a | b) && (a & c)) (2)if ((a | b) < (c & d)) (3)如果書(shū)寫(xiě)為high << 8 | lowa | b && a & ca | b < c & d(1)(2)雖然不會(huì)出錯(cuò),但語(yǔ)句不易理解;(3)造成了判斷條件出錯(cuò)。規(guī)則7 避免使用不易理解的數(shù)字,用有意義的標(biāo)識(shí)來(lái)替代。涉及物理狀態(tài)或者含有物理意義的常量,不應(yīng)直接使用數(shù)字,必須用有意義的靜態(tài)變量或者枚舉來(lái)代替。使用異常來(lái)表示方法執(zhí)行錯(cuò)誤,而不是使用C++的錯(cuò)誤返回碼方式。(1.42+)示例:如下的程序可讀性差。if (state == 0){ state = 1; ... // program code}應(yīng)改為如下形式:private final static int TRUNK_IDLE = 0;private final static int TRUNK_BUSY = 1;private final static int TRUNK_UNKNOWN = -1;if (state == TRUNK_IDLE){ state = TRUNK_BUSY; ... // program code}注意:Java 5.0 下建議使用枚舉來(lái)表示。異常:public void function(){... throw new RuntimeException(“。。。”);}規(guī)則8 數(shù)組聲明的時(shí)候使用 int[] index ,而不要使用 int index[] 。(1.42+)說(shuō)明:使用int index[] 格式使程序的可讀性較差,int [] index 表示聲明了一個(gè)int數(shù)組(int [])叫做index示例:如下程序可讀性差:public int getIndex()[]{ ....}如下程序可讀性好:public int[] getIndex(){ ....}規(guī)則9 不要使用 System.out 與 System.err 進(jìn)行控制臺(tái)打印,應(yīng)該使用工具類(lèi)(如:日志工具)進(jìn)行統(tǒng)一記錄或者打印。(1.42+)說(shuō)明:代碼發(fā)布的時(shí)候可以統(tǒng)一關(guān)閉控制臺(tái)打印,代碼調(diào)試的時(shí)候又可以打開(kāi)控制臺(tái)打印,方便調(diào)試。規(guī)則10 用調(diào)測(cè)開(kāi)關(guān)來(lái)切換軟件的DEBUG版和正式版,而不要同時(shí)存在正式版本和DEBUG版本的不同源文件,以減少維護(hù)的難度。 (1.42+)規(guī)則11 集合必須指定模板類(lèi)型(5.0+)說(shuō)明:方便程序閱讀,除去強(qiáng)制轉(zhuǎn)換代碼示例:Map<String,MyObject> map = new HashMap<String,MyObject>();規(guī)則12 一個(gè)文件不要定義兩個(gè)類(lèi)(并非指內(nèi)部類(lèi))。(1.42+)說(shuō)明:方便程序的閱讀與代碼的維護(hù)規(guī)則13 所有的數(shù)據(jù)類(lèi)必須覆寫(xiě)toString()、hashCode()、equals() 方法,toString()方法返回該類(lèi)有意義的內(nèi)容。(1.42+)說(shuō)明:方便數(shù)據(jù)類(lèi)的比較,父類(lèi)如果實(shí)現(xiàn)了比較合理的toString() ,子類(lèi)可以繼承不必再重寫(xiě)。hashCode與equals可以使用eclipse自動(dòng)生成。示例:public TopoNode{ private String nodeName; public String toString() { return "NodeName : " + nodeName; }}規(guī)則14 判斷語(yǔ)句不要使用”* == true”來(lái)判斷為真說(shuō)明:方便閱讀,減少?zèng)]有必要的計(jì)算以下錯(cuò)誤:if (ok == true){……}以下正確:if (ok){……}規(guī)則15 不要寫(xiě)沒(méi)有必要的向上強(qiáng)制轉(zhuǎn)型。(1.42+)說(shuō)明:沒(méi)必要寫(xiě)的向上強(qiáng)制轉(zhuǎn)型會(huì)浪費(fèi)性能,增加代碼閱讀難度示例:以下錯(cuò)誤:FileInputStream fis = new FileInputStream(f);InputStream is = (InputStream)fis;1.4.2 建議建議1 記錄異常不要保存exception.getMessage(),而要記錄exception.toString(),一般可通過(guò)日志工具記錄完整的異常堆棧信息。(1.42+)說(shuō)明:NullPointException拋出時(shí)常常描述為空,這樣往往看不出是出了什么錯(cuò)。示例:try { ... } catch (FileNotFoundException e) { logger.error(e); }建議2 一個(gè)方法不應(yīng)拋出太多類(lèi)型的異常。(1.42+)說(shuō)明: 如果程序中需要分類(lèi)處理,則將異常根據(jù)分類(lèi)組織成繼承關(guān)系。如果確實(shí)有很多異常類(lèi)型首先考慮用異常描述來(lái)區(qū)別,throws/exception子句標(biāo)明的異常最好不要超過(guò)三個(gè)。建議3 異常捕獲盡量不要直接 catch (Exception ex),應(yīng)該把異常細(xì)分處理。(1.42+)說(shuō)明:可以設(shè)計(jì)更合理異常處理分支建議4 如果多段代碼重復(fù)做同一件事情,那么在方法的劃分上可能存在問(wèn)題。(1.42+)說(shuō)明:若此段代碼各語(yǔ)句之間有實(shí)質(zhì)性關(guān)聯(lián)并且是完成同一件功能的,那么可考慮把此段代碼構(gòu)造成一個(gè)新的方法。建議5 集合中的數(shù)據(jù)如果不使用了應(yīng)該及時(shí)釋放,尤其是可重復(fù)使用的集合。(1.42+)說(shuō)明:由于集合保存了對(duì)象的引用,虛擬機(jī)的垃圾收集器就不會(huì)回收。建議6 源程序中關(guān)系較為緊密的代碼應(yīng)盡可能相鄰。(1.42+)說(shuō)明:便于程序閱讀和查找。示例:矩形的長(zhǎng)與寬關(guān)系較密切,放在一起。rect.length = 10;rect.width = 5;建議7 不要使用難懂的技巧性很高的語(yǔ)句,除非很有必要時(shí)。(1.42+)說(shuō)明:高技巧語(yǔ)句不等于高效率的程序,實(shí)際上程序的效率關(guān)鍵在于設(shè)計(jì)與算法。建議8 明確方法功能,精確(而不是近似)地實(shí)現(xiàn)方法設(shè)計(jì)。一個(gè)函數(shù)僅完成一件功能,即使簡(jiǎn)單功能也編寫(xiě)方法實(shí)現(xiàn)。 (1.42+)說(shuō)明:雖然為僅用一兩行就可完成的功能去編方法好象沒(méi)有必要,但用方法可使功能明確化,增加程序可讀性,亦可方便維護(hù)、測(cè)試。建議9 應(yīng)明確規(guī)定對(duì)接口方法參數(shù)的合法性檢查應(yīng)由方法的調(diào)用者負(fù)責(zé)還是由接口方法本身負(fù)責(zé),缺省是由方法調(diào)用者負(fù)責(zé)。(1.42+)說(shuō)明:對(duì)于模塊間接口方法的參數(shù)的合法性檢查這一問(wèn)題,往往有兩個(gè)極端現(xiàn)象,即:要么是調(diào)用者和被調(diào)用者對(duì)參數(shù)均不作合法性檢查,結(jié)果就遺漏了合法性檢查這一必要的處理過(guò)程,造成問(wèn)題隱患;要么就是調(diào)用者和被調(diào)用者均對(duì)參數(shù)進(jìn)行合法性檢查,這種情況雖不會(huì)造成問(wèn)題,但產(chǎn)生了冗余代碼,降低了效率。建議10 盡量使用Java 5.0新循環(huán)寫(xiě)法。(5.0+)說(shuō)明:代碼更加簡(jiǎn)潔示例:ArrayList<String> list = new ArrayList<String>();list.add...for(String str:list){System.out.println(str);}建議11 使用Java 5.0枚舉來(lái)替代以前用數(shù)字與字符串的同等目的的操作。(5.0+)說(shuō)明:Java 5.0以前沒(méi)有枚舉,大家都用數(shù)字或者字符串做枚舉同樣功能的事情示例:public enum EnumDemo{ ERROR,INFO,DEBUG}In other function:EnumDemo t = EnumDemo.DEBUG;if (t == EnumDemo.ERROR){ 。。。。。。 }建議12 interface 中定義的常量不要寫(xiě)public、static、final的修飾詞,方法不要寫(xiě)public修飾詞。(1.42+)說(shuō)明:更加簡(jiǎn)潔示例:public interface InterfaceT{ String TT = "abcl"; void doStart();}建議13 新起一個(gè)線程,都要使用Thread.setName(“…”)設(shè)置線程名。說(shuō)明:性能測(cè)試時(shí)可對(duì)線程狀態(tài)進(jìn)行監(jiān)控,異常時(shí)也可以知道異常發(fā)生在哪個(gè)線程中1.5 性能與可靠性1.5.1 規(guī)則規(guī)則1 對(duì)Debug,Info級(jí)別日志輸出前必須對(duì)當(dāng)前的調(diào)試等級(jí)先進(jìn)行判斷。(1.42+)說(shuō)明:日志一般都會(huì)有不少字符串的處理,如果不是Debug級(jí)別就沒(méi)有必要進(jìn)行處理示例:if (logger.debugEnable()){logger.debug(“request : ” + request.getMethod());}規(guī)則2 數(shù)組復(fù)制使用System.arraycopy(*) 。(1.42+)說(shuō)明:更好的性能規(guī)則3 不要使用循環(huán)將集合轉(zhuǎn)為數(shù)組,可以使用集合的toArray()方法。(1.42+)說(shuō)明:更好的性能,代碼更加簡(jiǎn)潔示例:ArrayList list = new ArrayList(); list.add.... String [] array = new String[list.size()]; list.toArray(array);規(guī)則4 大量字符串的相加等于處理應(yīng)該使用StringBuffer。(1.42+)說(shuō)明:大量的String相加等于處理性能消耗較多。“大量”一般指5次“+=”以上或者在循環(huán)中進(jìn)行字符串+=操作。示例:不推薦:String str = “”;str += ”a”;str += ”b”;推薦:StringBuffer sb = new StringBuffer();sb.append(“aa”);sb.append(“bb”);sb.append(“cc”);規(guī)則5 對(duì)類(lèi)中日志工具對(duì)象logger應(yīng)聲明為static. (1.42+)說(shuō)明:防止重復(fù)new 出logger對(duì)象(logger指各種日志工具類(lèi),可以是log4j,common log,jdk logger,內(nèi)部API等,盡管一些logger對(duì)LogFactory工廠有一些優(yōu)化,但是我們也必須防止代碼沒(méi)有必要的運(yùn)行)。1.5.2 建議建議1 public類(lèi)型的底層函數(shù)需對(duì)輸入?yún)?shù)進(jìn)行判斷,參數(shù)不合法應(yīng)該主動(dòng)拋出RuntimeException。(1.42+)說(shuō)明:底層函數(shù)必須保證輸入?yún)?shù)正確性再進(jìn)行其他處理(防止后面的代碼拋出錯(cuò)誤,減少?zèng)]有必要的后續(xù)代碼運(yùn)行)。使用RuntimeException 減少了try catch滿天飛,并有利于快速定于異常代碼。示例:public void doDivide(int a,int b){if (b == 0){throw new IllegalArgumentException(“denominator can’t be zero”);}...}建議2 盡量使用JDK自帶的API函數(shù),不要自己寫(xiě)類(lèi)似功能的函數(shù). (1.42+)說(shuō)明:JDK自身的函數(shù)在可靠性,性能方面一般有更好的表現(xiàn),大家必須熟練掌握,特別是算法方面的。建議3 IO操作流使用有Buffer功能的class. (1.42+)說(shuō)明:更好的性能,沒(méi)有Buffer的輸出流頻繁IO操作,效率反倒低。示例: FileOutputStream file= new FileOutputStream("test.txt"); BufferedOutputStream out = new BufferedOutputStream(file); for (int i = 0; i < bytes.length; i++ ){ out.write(…);} out.flush();

總結(jié)

從頭開(kāi)始,調(diào)整好心態(tài),現(xiàn)存的就是合理的。


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲精品一区二区三区在线看 | 99精品视频在线导航 | 在线播放中文 | av在线免费播放网站 | 91成人免费网站 | 国产精品999在线观看 | 日本a大片 | 久久美女色视频 | 中文字幕一区二区三区四区 | 久久艳片 | 欧美性猛交xxx乱大交3蜜桃 | 泰剧19禁啪啪无遮挡大尺度 | 成人做爰s片免费看网站 | 国产毛片自拍 | 成人国产视频在线观看 | 成人福利在线看 | 国产91九色 | 亚洲成人黄色片 | 羞羞答答视频 | av在线视| 久久国产成人午夜av浪潮 | 成年人性视频 | 性爱视频免费 | 黄色免费在线网址 | 海角在线观看91一区二区 | 成人午夜在线播放 | 日本欧美一区二区三区视频麻豆 | 久久久久久久久久久久免费 | 91精品国产乱码久久久久久久久 | 成人综合免费视频 | 午夜看毛片 | 久久久麻豆 | 中文字幕一二区 | 国产精品毛片无码 | 主播粉嫩国产在线精品 | 曰韩精品 | 一区国产精品 | 91成人午夜性a一级毛片 | 国产精品视频免费在线观看 | 国产又白又嫩又紧又爽18p | 国产一级毛片网站 |