понедельник, 30 июня 2008 г.

New Line at End of File - What For?

When composing code style rules for my Digr2iD project, I've asked myself a question: "Why there is a demand to insert new line at end of source file?". Yeah, such a dumb silly question. But why?

Certain compilers emit warnings when dealing with files without line at end. I've also found this rule as a check module in CheckStyle configuration. Moreover, vi and emacs support these conventions: vi inserts line at the end automatically, emacs needs require-final-newline variable to be set with appropriate value.

Theory behind this rule is the following: "source file is a text file, each text file comprises zero or more lines, each line comprises zero or more line characters terminated by a newline character". So unterminated text line is not a proper text line. Well, it's ok but what's the practical side of this?

Careful reading of this post and some thinking made me understand why it is important to have a new line at end of source file:
  • When source files are merged, there is a potential to have unterminated line of one source file inproperly united with the first line of another source file. For example if you are writing in C or C++ and include the header file which contains unterminated line with #define syntax, expect #define directive to be appended with the contents which goes right after #include statement. You may receive compilation error, or, even worse, don't receive it :) and be frustrated by strange program behaviour. As far as many header files end up with preprocessor directives probability of receiving a headache for a newbie is high.
  • It complicates pattern-based search. If you are looking for a piece of text that appears at the end of line, match result should not include the snippet which appears at the end of the file, which doesn't have new line as the last symbol. For example if you are trying to determine the number of lines in the file(s) using regular expression based search by simply couting number of CR or LF or CR-LF symbols, you will have a problem because unterminated line is not a line.

2 комментария:

Анонимный комментирует...

Great blog :-) you've a regular reader ;)
KiborK

Tsukur Vladimir aka _flush_dia_ комментирует...

Thanks KiborK :)