Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advice on formatting strings using %q #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yuxincs
Copy link

@yuxincs yuxincs commented Jun 5, 2023

Note All credit goes to @abhinav

This PR adds suggestions on formatting strings using %q over %s.

Source: https://abhinavg.net/2021/12/29/fmt-errof-q/ by @abhinav

Fixes #185

@CLAassistant
Copy link

CLAassistant commented Jun 5, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@mway mway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Added a few suggestions, but looks good in general.

Comment on lines +33 to +38
This advice applies more generally to other contexts when reporting user-specified data, such as logging invalid usernames:

```go
log.Printf("User %q does not exist", username)
// User "no_name" does not exist
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would generalize this slightly to something like:

This advice can also apply more generally to other formatting situations
when a dynamic string could confuse or change the formatting or meaning of
surrounding text when interpolated.

Mostly to avoid bucketing "logging" as "formatting" (i.e., structured logging - which is at least as relevant as log.Printf scenarios - doesn't require the same treatment for fields, because they are already discrete values).

I think the log.Printf example is fine to keep, I would just put a good/bad example under the fmt.Errorf examples.

@@ -0,0 +1,38 @@
# Format Strings Using `%q`

Whenever formatting messages that contain a string component via `fmt`, use `%q` instead of `%s`. This will wrap the specified string in quotes, helping it stand out from the rest of the error message. More importantly, if the string is empty, it will provide a more helpful error message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever formatting messages that contain a string component via fmt, use %q instead of %s.

I think we might want to clarify slightly: it's not necessarily just when a string component in general is used, but more when dynamic strings (incl. user-provided values) could potentially cause interpolation to result in something weird.

Maybe something like:

Whenever dynamic strings - such as user-provided data - are used for formatting,
prefer the placeholder %q over %s.

@@ -0,0 +1,38 @@
# Format Strings Using `%q`

Whenever formatting messages that contain a string component via `fmt`, use `%q` instead of `%s`. This will wrap the specified string in quotes, helping it stand out from the rest of the error message. More importantly, if the string is empty, it will provide a more helpful error message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] helping it stand out from the rest of the error message.

More importantly, if the string is empty, it will provide a more helpful error message.

I'd either generalize these slightly or remove the word "error" (this guidance can apply to more than just errors). Maybe something like:

More importantly, if the string is empty or has its own formatting, the result
will be clearer and more consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: prefer %q to format strings over %s
3 participants