项目作者: QueeniePeng

项目描述 :
Readability is rule of thumb.
高级语言:
项目地址: git://github.com/QueeniePeng/Java-Code-Standards.git
创建时间: 2020-03-18T20:59:31Z
项目社区:https://github.com/QueeniePeng/Java-Code-Standards

开源协议:

下载


Java Code Standards

Readability is rule of thumb.

Stack parameters:

  1. // RIGHT
  2. public View onCreateView(@NonNull LayoutInflater inflater,
  3. @Nullable ViewGroup container,
  4. @Nullable Bundle savedInstanceState) {
  5. return LayoutInflater
  6. .from(getContext())
  7. .inflate(R.layout.seeding_form, container, false);
  8. }
  1. // AVOID
  2. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  3. return LayoutInflater.from(getContext()).inflate(R.layout.seeding_form, container, false);
  4. }

Naming Conventions:

  • Any naming should have <= 3 words

BOOLEAN

  1. isSelected // RIGHT
  2. setSelected // AVOID
  3. selected
  1. hasHeader // RIGHT
  2. withHeader // AVOID
  3. containHeader

LIST

  1. List<Employee> employees; // RIGHT
  2. List<Employee> employeeList; // AVOID

FUNCTION

0. PARAMETERS

  1. void setEmployee(Employee employee)() // RIGHT
  2. void set(Employee e)() // AVOID
  3. void set(Employee employee)()

1. ON CLICK LISTENER

  1. void onAcceptButton()// RIGHT
  2. void acceptPressed() // AVOID

2. FOR-LOOP

  1. for (Company company : companies) {} // RIGHT
  2. for (int i = 0; i < companies.size(); i++) // AVOID

IF ELSE / Ternary Operator

  1. String greeting = isMorning // RIGHT
  2. ? "Good Morning"
  3. : "Good Night";
  1. String greeting = ""; // AVOID
  2. if (isMorning) {
  3. greeting = "Good Morning";
  4. } else {
  5. greeting = "Good Night";
  6. };

SWITCH CASE

  1. Empty space in between cases.
  2. Include every case.
  1. // RIGHT
  2. int getCalories() {
  3. switch (breakfast) {
  4. case BACON:
  5. return 140;
  6. case EGG:
  7. return 70;
  8. default:
  9. return 0;
  10. }
  11. }

RESOURCES

STRINGS

  1. // RIGHT
  2. <plurals name="employees_size">
  3. <item quantity="one">%d employee</item>
  4. <item quantity="other">%d employees</item>
  5. </plurals>
  6. setText(getResources().getQuantityString(
  7. R.plurals.employees_size,
  8. employees.size(),
  9. employees.size()));
  1. // AVOID
  2. setText(employees.size()
  3. ? "1 employee"
  4. : employees.size() + " employees")

ID

  1. // RIGHT
  2. TextView: text_
  3. ImageView: image_
  4. Button: button_
  1. <ImageView
  2. android:id="@+id/image_profile"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content" ></ImageView>
  5. <menu>
  6. <item
  7. android:id="@+id/menu_done"
  8. android:title="Done" ></item>