[코드리뷰 스터디 - 🚗 자동차 경주] No newline at end of file

Hyunjoon Choi·2023년 9월 26일
0

코드리뷰 스터디

목록 보기
2/5
post-thumbnail

🤷 No newline at end of file?

코드리뷰를 받던 중, 새로운 것을 알았다. 바로 No newline at end of file 문제를 해결해달라는 것이었다. 이것은 문서의 마지막에 개행문자 (줄바꿈)가 포함되어야 함을 의미한다.

언제 발생하는가?

인텔리제이 등 IDE를 이용할 때, 별도의 설정을 하지 않았을 경우 발생할 수 있다. 아래 코드의 예시처럼 마지막에 추가 개행이 발생하지 않았음을 확인할 수 있다. (변수 네이밍이 클린하지 못하긴 하지만.. 다른 내용이기 때문에 패스)

왜 문제인가?

해당 문제가 기록되어 있는 스택오버플로우 글을 보면 다음과 같이 그 이유가 되어 있다.

It indicates that you do not have a newline (usually \n, aka LF or CRLF) at the end of file.
That is, simply speaking, the last byte (or bytes if you're on Windows) in the file is not a newline.
The message is displayed because otherwise there is no way to tell the difference between a file where there is a newline at the end and one where is not. Diff has to output a newline anyway, or the result would be harder to read or process automatically.
Note that it is a good style to always put the newline as a last character, in text files, if it is allowed by the file format. Furthermore, for example, for C and C++ header files it is required by the language standard.

요약하자면, 개행을 붙이지 않았을 경우 개행을 붙인 문서와 개행을 붙이지 않은 것의 구분이 어려우며, 텍스트 파일에서 파일 형식이 허용하는 경우 항상 개행 문자를 마지막 문자로 넣는 것이 좋은 스타일이다. 더불어 예시로 C/C++ 헤더 파일은 이것을 언어 표준으로 지정한다. 우리는 자바로 작성하지만, 전반적인 코드 습관을 따졌을 때는 위 관례를 따르는 게 협업을 위해서는 적용해야 할 규칙이라고 생각된다.

다른 스택오버플로우 글을 보면 위에서 말하듯이 C++11 스탠다드 문서가 기록되어 있다.

A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file (C++11 §2.2/1).

어떻게 해결할 수 있는가?

인텔리제이에서는 매번 번거롭게 직접 개행문자를 넣는 것 보다, 설정을 통해 자동으로 추가되도록 할 수 있다.
설정 > 에디터 > 일반 > 모든 저장된 파일이 줄 바꿈으로 끝나도록 함을 활성화해주면 된다.

부족하거나 보완할 점이 있다면 댓글 부탁드립니다 😃

profile
개발을 좋아하는 워커홀릭

0개의 댓글