Everything is worth discussing, just not ad nauseam. I've never really question newline at the end of a file so this is interesting to me too.
Starting with an empty file you’re on a new line.
But if you don’t end every line with a newline character then your next line will end up on the same line in the file as the last one.
Source files are text files so they end in a new line.
echo “new line” >> textfile
cat <textfiles>
If you and your toolchain don’t see them as text files and never will, and you never use your editor to edit other text files (e.g. /etc/…), you’re probably ok without a newline at eof.And to enforce it? Try https://editorconfig.org/ which is supported natively by many editors (eg Visual Studio) and via plugins in others (eg VS Code). All it needs is a .editorconfig file in your folder and you get a handful of helpful settings (trim trailing whitespace, end with a newline, tabs vs spaces, ASCII vs UTF8, etc) in a simple file of rules that cascade down your folder hierarchy. Usually just add it next to your .gitignore, README.md, etc.
It's a code formatter that provides one style with very limited configuration. The idea being the rigidness removes bike shedding and forces consistency.
Of course you can use any code formatter and configure it to your liking, but there's something to be said about not giving the user levers to pull and argue about. Everyone just download Black, and you're good to go.
It's an insignificant reason, but that's why it's traditionally recommended.
\ No newline at end of fileIt's having me think about the way prs should be done on divisive topics.
On one hand you have this "guy" who thinks he's gods gift to programming demanding a new line be added to all 30 files you're adding to the project.
On the other hand that "guy" is you.
What then? In both cases.
Being consistent is orthogonal to readability and modularity and extensibility.
It's actually delusional. Most programmers want to be consistent based simply off a feeling but in actuality the benefit of consistency is minimal.
You "are" that guy.
Which convention is chosen as the default doesn't usually matter (unless some of the edge-cases raised by the other comments are relevant to you), but what matters is what when you look at a diff it isn't polluted.
If you didn't care about consistency you wouldn't be changing others people code to be consistent.
You would make your own change and that relevant change remains inconsistent. That's it.
>Which convention is chosen as the default doesn't usually matter (unless some of the edge-cases raised by the other comments are relevant to you), but what matters is what when you look at a diff it isn't polluted.
Again the diff isn't polluted. It is simply inconsistent. This is different from two people battling over which convention to follow (which is what you are referring to). In the later case, yes you will see diffs where one person attempts to change the convention, in the former case you simply see a diff where the change doesn't follow a convention.
I like having the extra line, it is a very minimal difference, but this way it is possible to copy the last line by copying "down" rather than copying "out" to the end of the line.
> have pointed out, every line ends in a newline character, for most text editors).
My colleagues always add changes that end with that `\ No newline at end of file` diff for certain source files.
It shouldn't have to end in a newline, but I still end them all with a newline.
Con: Your file is 1 byte larger.