コードコアのクリア(5章フォーマット)


フォーマットはなぜ重要ですか?

public void horriblyFormattedMethod() {
	System.out.println("First line");
			System.out.println("Second line");
		System.out.println("Third line");
	for (int i = 0; i < 3; i++)
	System.out.println("number " + i);
}
私のドアは何ですか...?
public void horriblyFormattedMethod() {
	System.out.println("First line");
	System.out.println("Second line");
	System.out.println("Third line");
	for (int i = 0; i < 3; i++) {
		System.out.println("number " + i);
	}
}
今は理解できます!

読み取り可能性が必要です

  • コードは読みやすいです.
  • アマチュアのようには見えません.
  • フォーマットエラーコードによるエラー分析のリスクを低減!
  • クリーンコードの書式設定


    適当な長さを保つ


    ~200 lines < 500 lines

    200ライン

  • 「コード長を200行に制限するのは、厳格なルールではありませんが、通常は小さいファイルの方が大きいファイルの方が分かりやすいです.」
    ->現在、業界のほとんどのコードも200行程度に維持されています.
  • コードの長さが200行を超える場合、クラスは複数のタスクを実行している可能性があります.->SRP違反!
  • 密接な概念が互いに近づく。

  • 行の集合は完全な考えを表すので,概念は空行に分離される.
  • 変数は使用する位置でできるだけ近い.
  • Java Class Declarations



    クラス内のコード順序。


  • せいてきへんすう
    public→protected→package→private順

  • インスタンス変数
    public → protected → package → private

  • 生成者

  • 方法
    publicメソッドで呼び出されるprivateメソッドは、その下にあります.
    可読性をメイングループPing!
  • /*
     * @(#)Blah.java            1.82 99/03/18
     * 
     * Copyright (C) 1994-1998 Sun Microsystems, Inc.
     * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
     * All rights reserved.
     *
     * This software is the confidential and proprietary information of Sun
     * Microsystems, Inc. (*Confidential Information"). You shall not
     * disclose such Confidential Information and shall use it only in
     * accordance with the terms of the license agreement you entered into
     * with Sun.
     */
    
    package java.blah;
    
    import java.blah.blahdy.BlahBlah;
    
    /**
     *
     * Class description goes here.
     *
     * @version 1.82 18 Mar 1999
     * @author Firstname Lastname
     */
    public class Blah extends SomeClass {	
    	public static int classVar1;
    	private static Object classVar2;  // static 변수
    	public Object instanceVar1;
    	protected int instanceVar2;
    	private Object[] instanceVar3;  // instance 변수
    	public Blah() {  // 생성자
    		// ...implementation goes here...
    	}
    	public void doSomething() {  // 메서드
    		// ...implementation goes here...
    	}
    	public void doSomethingElse(Object someParam) {
    		// ...implementation goes here...
    	}
    }

    Team Coding Convention



    言語を開発する会議を優先しますが、あいまいな部分はチーム会議に従います.
    もしなかったら、一緒に創造してもいいです.

    リファレンス


    これらの情報はゼロベースラインクリーニングコード毎月1冊を聞いた後に整理された.