[Clean Code] Formatting

도모·2023년 3월 14일
0

Clean Code

목록 보기
4/5
post-thumbnail

정리한 내용 중 잘못된 내용이 있을 수 있습니다. 잘못된 내용이 있다면 꼭 알려주세요.

1.포맷팅이 중요한 이유

포맷팅이 중요한 이유

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);
    }
}

아래 처럼 바꾼다면 훨씬 이해하기가 쉽다.

코드의 포맷팅은 가독성에 필수적이다.

  • 코드를 수월하게 읽어나갈 수 있다.
  • 아마추어처럼 보이지 않는다.
  • 포맷팅으로 인해 코드를 잘못해석해 버그를 발생할 위험을 줄인다.

2.클린코드 포맷팅

적절한 길이 유지

~200lines
<500lines

  • 코드 길이를 200줄 정도로 제한하는 것은 반드시 지킬 엄격한 규칙은 아니지만, 일반적으로 큰 파일 보다는 작은 파일이 이해하기 쉽다.
  • 현업에서의 대부분의 코드들도 200라인 정도를 유지한다고 한다.
  • 코드 길이가 200라인을 넘어간다면 클래스가 여러 개의 일을 하고 있을 수 있다(SRP위배)

밀접한 개념은 가까이

밀접한 개념은 서로 가까이 둔다

  • 행 묶음은 완결된 생각 하나를 표현하기 때문에
    개념은 빈 행으로 분리한다.
  • 변수는 사용되는 위치에서 최대한 가까이 선언한다.

Java Class Declarations

Class 내부 코드 순서


1.static 변수 (public -> protected -> package -> private 순서)
2.instance 변수 (public -> protected -> package -> private 순서)
3.생성자
4.메서드
public 메서드에서 호출되는 private 메서드는 그 아래에 둔다. 가독성 위주로 그룹핑

/*
 * @(#)Blah.java        1.82 99/03/18
 *
 * Copyright (c) 1994-1999 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 {
  
           /* A class implementation comment can go here. */ 
    /**  
        classVar1 documentation comment */
    public static int classVar1;

    /** 
  
            *  
        classVar2 documentation comment that happens to be      *  
        more than one line long      */
    private static Object classVar2;

    /**  
        instanceVar1 documentation comment */
    public Object instanceVar1;

    /**  
        instanceVar2 documentation comment */
    protected int instanceVar2;

    /**  
        instanceVar3 documentation comment */
    private Object[] instanceVar3;

    /** 
     * ...
        constructor Blah documentation comment...      */
    public Blah() {
  
              // ...implementation goes here...     }

    /**
     * ...
        method doSomething documentation comment...      */
    public void doSomething() {
  
               // ...implementation goes here...      }

    /**
     * ...method doSomethingElse  
        documentation comment...      * @param someParam  
        description      */
    public void doSomethingElse(Object someParam) {
  
               // ...implementation goes here...      }
}

Team Coding Convention

개발 언어의 컨벤션이 우선이지만, 애매한 부분은 팀 컨벤션을 따른다.
없다면, 함께 만들어 가는 것도 좋아요!

MySQL Convention
컬럼명은 snake_case로 네이밍한다.

Team Convention
enum 타입으로 사용하는 varchar 타입의 경우 컬럼명은 _type으로 끝나도록 네이밍한다.

참고할만한 컨벤션

Google Java Style Guide
Naver Hackday Java Convention

profile
안녕하세요. 백엔드 엔지니어 도모입니다.

0개의 댓글