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 guideline about empty strings inside interpolation. #952

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zopolis4
Copy link

@pirj
Copy link
Member

pirj commented Oct 29, 2024

I apologise for the pluralism of opinions. It doesn’t seem that we need a rule for a case like this. It is stylistic, quite unfrequent I believe, and some may say they prefer it explicit. Not to say that it has nothing to do with interpolation specifically, it’s just hard to detect other similar cases with static analysis, eg r = [“prefix”, cond ? “foo” : “”].join.

@Zopolis4 Zopolis4 changed the title Add guideline about empty strings inside tnterpolation. Add guideline about empty strings inside interpolation. Oct 30, 2024
@Zopolis4
Copy link
Author

Zopolis4 commented Oct 30, 2024

I searched on github for the two forms listed in the PR:
"#{condition ? 'foo' : ''}" had 1.2k results:
image
"#{'foo' if condition}" had 1.4k results:
image

Given the scale of rubocop, I do not know if this counts as frequent, but it is at least relevant information.

As to your point about the rule being stylistic, is that not the point of the ruby style guide? I'm open to discussion on whether this style is preferred, but I presumed it being a stylistic choice does not disqualify it from inclusion.

@pirj
Copy link
Member

pirj commented Oct 30, 2024

This is a confirmation that both styles take place.
Do you see a particular reason to enforce one over the other?

@Zopolis4
Copy link
Author

The modifier if form is shorter, and only contains the relevant logic, while the ternary form has redundant logic for assigning an empty string.

@pirj
Copy link
Member

pirj commented Oct 30, 2024

Is being shorter a goal?

assigning an empty string

What do you mean by “assigning”?

In general, I’m not against the cop.
But is there a place for such an edge case in the style guide?

A more generic rule could sound like:

Instead of a ternary that returns an empty string in one of the branches, in an expression where the result will be in some way concatenated with another string, prefer using an inline “if” instead

But I still think it’s not worth adding such a guideline.

@Zopolis4
Copy link
Author

Is being shorter a goal?

If code can be made more concise without sacrificing readability, performance or functionality, then yes, I think making it shorter is a goal.

What do you mean by “assigning”?

Assigning is probably the wrong word-- stating? creating?

But is there a place for such an edge case in the style guide?

I was told to open a PR, and it was my understanding that Style cops should have equivalent rules in the style guide. If that's not the case, I'm happy to close this PR and just focus on the cop itself.

@pirj
Copy link
Member

pirj commented Oct 30, 2024

without sacrificing readability

Don’t we sacrifice readability here by making it return a nil implicitly?

stating? creating?

Returning?
But with the widespread frozen_string_literal there’s even no excessive object initialisation. It’s slightly less efficient, as this string won’t be the same object in a different file, like nil, but still. I don’t think an overhead like this is worth optimising for in a general case.

@pirj
Copy link
Member

pirj commented Oct 30, 2024

Style cops should have equivalent rules in the style guide

This style guide is not only about style, despite its name.
And it has guidelines that readers users from introducing costly mistakes in their code.

I tend to agree that every cop should have a rationale behind it, but there are so many cops that putting this all into the style guide will make the style guide hard to read.

Off the top of my head, I can’t tell what the rule of adding vs not adding a guideline is. As far as I know, for rubocop-rails and rails-style-guide there is always a guideline that backs the cop. Not something I know of other extensions or the main style guide.

@Zopolis4
Copy link
Author

Don’t we sacrifice readability here by making it return a nil implicitly?

i see it like this:
modifier form: "if condition then string"
ternary form: "if condition then string otherwise no string"

the fact that nothing happens if the condition isn't true is, to me, self-evident in the modifier form, and so it is unnecessary to state it by using the ternary form.

I think its probably just personal opinion, though.

I don’t think an overhead like this is worth optimising for in a general case.

I wasn't thinking in terms of performance, just in terms of code clarity.

@Earlopain
Copy link

🤷 I thought a PR for the style guide would be mandatory for a style cop. My bad. Some general advice for the github code search: append -is:fork. Results are pretty overstated otherwise, though it doesn't really change proportions that much in the end. It has some extra troubles like not considering ""-style strings in interpolation but that starts to get pretty complicated for a regex.

It's 40%/60% which is pretty close.


I would say that not everyone will agree with the shorter version. Ruby has some constructs that look a bit weird when compared to other languages and I would count using 'foo' if bar as a shorthand for nil in else as one of those. I find it similar to this:

a = nil
a = foo if bar

# or the longer form
a = nil
if false
  a = foo
end

You don't need that first assignment but at least for me it looks pretty weird without it. I feel similar about this cop.

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

Successfully merging this pull request may close these issues.

3 participants