ecoCode icon indicating copy to clipboard operation
ecoCode copied to clipboard

Use single quote (') instead of quotation mark (") is only available with js/ts

Open glalloue opened this issue 2 years ago • 3 comments

Use single quote (') instead of quotation mark (") is a common rule used in javascript / typescript After some researches, it turns out that it is indeed not valid for java :

Use single quotes for literal chars, double quotes for literal Strings https://stackoverflow.com/questions/439485/is-there-a-difference-between-single-and-double-quotes-in-java

With PHP, the rule seems to be different :

A single-quoted string does not have variables within it interpreted. A double-quoted string does. https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php#:~:text=In%20PHP%2C%20people%20use%20single,b%20%24c%20%24d%22%20.

With python, it doesn't seem to matter

String literals can be enclosed in matching single quotes (') or double quotes ("). https://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python

And Rust is similar to java

==> We need to update to applicable rules table

Originally posted by @glalloue in https://github.com/cnumr/ecoCode/discussions/67#discussioncomment-2762066

glalloue avatar May 16 '22 18:05 glalloue

Concerning PHP, this might be the opportunity for a PHP-specific rule: don't use double quotes if you don't need the string to be interpreted. Basically, if a string is interpreted, it will replace variables, beginning with a $ (e.g. "My name is $userName"). If there is no variable, asking PHP to interpret it seems to be redundant. Some checks would be required before deciding to make:

  • Does a similar rule already exist in SonarQube? (a quick lookup through Sonarcloud rules seems to indicate that no, but a more thorough check would be welcome)
  • Is there an optimization mechanism I don't know about that will prevent PHP from needlessly interpreting a String?

cyChop avatar Jun 02 '22 05:06 cyChop

Reading the rule description in Frédéric Bordage's book, it is specifically targeted to PHP ! The rule should check if a string use double quotes without having variables in it.

The rule has no meanings in JS (doesn't matter) nor Rust / Java (different meaning)

In the future this could also apply to posix shell scripts too

KuSh avatar Jun 02 '22 16:06 KuSh

True, I didn't read the book again. In JS/TS, it's mainly a style issue. In PHP, it does have an impact.

cyChop avatar Jun 02 '22 18:06 cyChop