[wikka-community] Coding guideline about backslash.

mahefa randimbisoa dotmg
Mon Oct 8 12:09:27 GMT 2007


Which coding guideline about backslash?

My coding style for writing a backslash inside a string, whether
double quote or single quote is used as delimiter; is to expressly
escape it with another backslash.

These 2 strings are the same to write a string composed of 2
characters: a backslash and the letter n.
'\\n' and '\n'

I prefer the first one, and the reasons are:



1) When in the future, someone changes my singlequote in doublequote,
errors due to this change are minimized.

2) clarity: I don't need to think if the character that follow the
backslash has a special meaning when eventually combined with it. I
just have to count the number of backslashes and divide them by 2.



Consider you want to write a constant \\192.168.1.2\d$, using a single
quote as delimiter.

If you use '\\192.168.1.2\d$', the string will be : \192.168.1.2\d$
You can easily get in trouble if you used to consider that escaping
backslash is not needed within single-quote-delimited strings.

To write the string correctly, you must do one of the 4 proposals below.
'\\\192.168.1.2\d$' or '\\\\192.168.1.2\d$' or '\\\192.168.1.2\\d$' or
'\\\\192.168.1.2\\d$'

For me, having in mind that I always escape my backslashes, the 4th
proposal is what I can read and understand more easily.

--
Mahefa Randimbisoa (aka DotMG)




More information about the community mailing list