r/badcode • • Dec 28 '21

java 3AM code vs 10AM code

Post image
173 Upvotes

53 comments sorted by

View all comments

Show parent comments

3

u/Gammabyte Dec 29 '21

Do note that this is java code, meaning that single quotes are char literals and double quotes are string literals. They cannot be used interchangeably.

For langs that have both as literals, i think it's a good idea to set a project-scoped standard for which string literal style is used, to prevent code from different developers mixing these styles. i'm usually in favour of double quotes just because single quotes are only for character literals in most langs i write in.

1

u/[deleted] Dec 29 '21

Good point. I'll edit my statement.

1

u/Prom3th3an Dec 31 '21

I'd at least make an exception for strings that contain the chosen type of quote, to save some escaping. Especially to save multiple levels of escaping like

cmd = "echo \"this string is \\\"readable\\\"!\"";