Should source code be saved in UTF-8 format
How important is it to save your source code in UTF-8 format?
Eclipse on Windows uses CP1252 character encoding by default. The CP1251 format means non UTF-8 characters can be saved and I have seen this happen if you copy and paste from a Word document for a comment.
The reason I ask is because out of habit I set-up Maven encoding to be in UTF-8 format and recently it has caught a few non mappable errors.
(update) Please add any reasons for doing so and why, are there some common gotchas that should be known?
(update) What is your goal? To find the best practice so when ask why should we use UTF-8 I have a good answer, right now I don't.
Solution 1:
What is your goal? Balance your needs against the pros and cons of this choice.
UTF-8 Pros
- allows use of all character literals without
\uHHHH
escaping
UTF-8 Cons
- using non-ASCII character literals without
\uHHHH
increases risk of character corruption- font and keyboard issues can arise
- need to document and enforce use of UTF-8 in all tools (editors, compilers build scripts, diff tools)
- beware the byte order mark
ASCII Pros
- character/byte mappings are shared by a wide range of encodings
- makes source files very portable
- often obviates the need for specifying encoding meta-data (since the files would be identical if they were re-encoded as UTF-8, Windows-1252, ISO 8859-1 and most things short of UTF-16 and/or EBCDIC)
ASCII Cons
- limited character set
- this isn't the 1960s
Note: ASCII is 7-bit, not "extended" and not to be confused with Windows-1252, ISO 8859-1, or anything else.
Solution 2:
Important is at least that you need to be consistent with the encoding used to avoid herrings. Thus not, X here, Y there and Z elsewhere. Save source code in encoding X. Set code input to encoding X. Set code output to encoding X. Set characterbased FTP transfer to encoding X. Etcetera.
Nowadays UTF-8
is a good choice as it covers every character the human world is aware of and is pretty everywhere supported. So, yes, I would set workspace encoding to it as well. I also use it so.