Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

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

Open
glalloue opened this issue May 16, 2022 · 3 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation rule this is a problem on a rule

Comments

@glalloue
Copy link
Collaborator

glalloue commented May 16, 2022

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 #67 (comment)

@glalloue glalloue added bug Something isn't working documentation Improvements or additions to documentation labels May 16, 2022
@cyChop
Copy link
Collaborator

cyChop commented Jun 2, 2022

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?

@KuSh
Copy link
Collaborator

KuSh commented Jun 2, 2022

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

@cyChop
Copy link
Collaborator

cyChop commented Jun 2, 2022

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

@dedece35 dedece35 added the rule this is a problem on a rule label Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working documentation Improvements or additions to documentation rule this is a problem on a rule
Projects
None yet
Development

No branches or pull requests

4 participants