Reasons:
- UNIX standard
- If the last line in a file doesn't end with a newline then addition of next line affects two lines instead of one. This also pollutes diff on multiple files, so reader may wonder what has changed in a line whereas no significant change has occured.
Multiple newlines at the file end are also redundant as well as spaces at the end of line.
Ensure your IDE/editor has the relevant setting set:
- IntelliJ: navigate to Settings > Editor > General and
- check Ensure line feed at file end on Save
- set Strip trailing spaces on Save to Modified Lines
- VSC: navigate to Preferences > Settings and
- search for "newline" and check the following
- Files: Insert Final Newline
- Files: Trim Final Newlines
- search for "whitespace" and check Trim Trailing Whitespace
- search for "newline" and check the following
- Sublime Text: navigate to Preferences > Settings > Default find and set the following to
true
:trim_trailing_white_space_on_save
ensure_newline_at_eof_on_save
- ... or use Single Trailing Newline package to ensure one and only one newline at the end of file
Other IDEs may have a setting similar to any of the above.
Can also be achieved with:
https://spec.editorconfig.org/index.html
insert_final_newline = true