1

Which line endings should be used for platform independent code (not file access)?

My next project must run on both Windows and Linux.

I wrote code on Linux and used Hg to clone to Windows and it ran fine with Linux endings. The downside is that if you open the file in something other than a smart editor the line endings are not correct.

Kenny
  • 635
  • 8
  • 22
  • @pp_ This question is about which line ending to use in the actual code not file access. – Kenny Feb 25 '16 at 22:25
  • It's still a duplicate. – pp_ Feb 25 '16 at 22:29
  • @pp_ Please provide a reference. I'm happy to read it. Searching to find an answer to this was difficult because most are asking about file I/O. – Kenny Feb 25 '16 at 23:22
  • https://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types – pp_ Feb 25 '16 at 23:27
  • A some more [here](https://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n) and [here](https://stackoverflow.com/questions/8893132/writing-new-line-character-in-java-that-works-across-different-os) (somewhat related) – pp_ Feb 25 '16 at 23:33
  • I also found [this](https://stackoverflow.com/questions/3821784/whats-the-difference-between-n-and-r-n). And [this wikipedia article](https://en.wikipedia.org/wiki/Newline) is great, too. – pp_ Feb 25 '16 at 23:37
  • Possible duplicate of [What type of line breaks does a Python script normally have?](http://stackoverflow.com/questions/6907245/what-type-of-line-breaks-does-a-python-script-normally-have) – Mogsdad Feb 26 '16 at 22:52
  • @pp_ This is not what I am asking. – Kenny Feb 26 '16 at 22:58
  • @Mogsdad Yes. A duplicate of this question. Not a very satisfying answer but I think I can opt to just use Linux line endings and good editor (which I have). Just need to convince the team to do the same. – Kenny Feb 27 '16 at 00:40
  • On our team, we have devs on multiple OSes and editors. We use post-commit triggers in our VCS to enforce consistent line endings (Unix), and devs have their clients check files out according to their preferences. – Mogsdad Feb 27 '16 at 00:47

1 Answers1

1

In general newlines (as typically used in Linux) are more portable than carriage return and then newline (as used in Windows). Note also that if you store your code on GitHub or another Git repository, it will convert it properly without you having to do anything.

John Messenger
  • 351
  • 1
  • 8