4
\$\begingroup\$

I've recently saw someone, who says that their code is 350 characters long.

Problem

It can vary depending on platform.

Code length compared across various platforms

My suggestions

Do either one of these:

  • Add an automatic character count, so it will be consistent.
  • Require users to use a specific line break format (stated in the FAQ or when asking/answering questions), and calculate character count based on that. It can be done very easily with most text editors, like Notepad++.
\$\endgroup\$
1

1 Answer 1

17
\$\begingroup\$

I would always treat newlines as single characters. Their representation as 2 bytes on windows is a historical oddity (and perhaps an abomination, too, but that is another story).

Figuring out how to count that way is your problem.

Unix people have an equivalent (if less distressing) issue where many editors append a trailing (and unnecessary) newline to files automatically for historical reasons relating to fragile core command-line tools. Usually I just accept the one character penalty on mine.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ for vim, :set binary noel. \$\endgroup\$
    – ninjalj
    Commented Feb 4, 2011 at 20:28
  • \$\begingroup\$ Interesting that Unix people treat CRLF as historical oddity and abomination but fail to see the same thing in terminal emulators ;-) \$\endgroup\$
    – Joey
    Commented Feb 5, 2011 at 15:24
  • \$\begingroup\$ On a related note: I have yet to see a language that mandates CRLF (not even batch files on Windows) or a trailing LF (some C compilers emit a warning but I guess most of them have outgrown the 1960s by now and none throws up on it). \$\endgroup\$
    – Joey
    Commented Feb 5, 2011 at 15:27
  • 1
    \$\begingroup\$ @Joey Oh "terminal emulators" as such are a historical oddity and we can be thankful that few people are targeting the more obscure terminals these days, but the command line persists because it is the Interface of the Gods. \$\endgroup\$ Commented Feb 5, 2011 at 15:40
  • 1
    \$\begingroup\$ Oh, nothing wrong with CLIs in my eyes ;-). Just treating text as an API always sounded quite wrong to me. And at one time something spit out a manpage in the form of b^Hbo^Hol^Hld^Hd text and I bet even NULs after line feeds are still eaten ;-) \$\endgroup\$
    – Joey
    Commented Feb 5, 2011 at 15:51
  • \$\begingroup\$ @Joey: Go mandates a trailing newline: ideone.com/0EEAu \$\endgroup\$
    – Joey Adams
    Commented May 3, 2011 at 15:57
  • 2
    \$\begingroup\$ @Joey: Yikes, that's a serious WTF. Who on earth thought that to be a good idea? \$\endgroup\$
    – Joey
    Commented May 3, 2011 at 16:09
  • \$\begingroup\$ It's a good idea insofar as it sidesteps the religious war. It sucks for code golf (I'm in the "no trailing newline counted if it isn't necessary" camp) \$\endgroup\$
    – J B
    Commented Sep 28, 2011 at 22:30
  • 3
    \$\begingroup\$ Text editors append a trailing LF because text files should have one. Without it, displaying a text file in a terminal emulator won't put the prompt on a new line, concatenating two text files will join the last line of the first with the first line of the second and some tools (apt-get) and compilers (C) and interpreters (PHP) will show errors or at least warnings. \$\endgroup\$
    – Dennis
    Commented May 19, 2014 at 5:27