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
Rule change suggestions #214
Comments
A few clarifications while reading these that might be useful for @searls or others: Method Parameter IndentationI think this is the overlapping area of a few config options. First is Replacing do/end with {}In this scenario, I think this is an error. The current rule is using a strategy called "semantic blocks". This means using |
@itsliamegan thanks for your helpful comments. I don't know much about the Rubocop rules, and I'm sure there are reasons for at least some of standardrb's choices of which I'm not aware and can learn from. Method Parameter IndentationIf I do this:
I get this when running standardrb: x.rb:3:4: Layout/MultilineMethodDefinitionBraceLayout: Closing method definition brace must be on the line after the last parameter when opening brace is on a separate line from the first parameter. This, however, is permitted:
This is mathematically and logically sound, and I kind of like it. Replacing do/end with {}I had seen mention of semantic blocks over the years, but it didn't occur to me that's what was going on. I like the idea of semantic blocks in principle, but when I've never used semantic blocks; my questions to those who have:
|
Horizontal Alignment of Values and BlocksI actually disagree with your points here. No, we would not design a web form in this way, but we would also not design a web form that looks like the code on the left. That causes different issues… it makes it more challenging to tell which value maps to which key (espeically if you have some very short keys and some very long keys), it causes git churn (the spacing changes for every line when you add a longer key), and it’s harder for new developers (if you don’t have your editor set up that way, you are trying to fix it manually). (just for reference because I keep forgetting, this rule is And vs. &&While this particular case is valid, most cases require the use of Parenthesizing Ternary ConditionsI think that, when you get to the point where your ternary is hard to read, you should maybe consider not using a ternary. (again, for reference, this rule is Method Parameter IndentationTBH I am not sure which rule is causing this. We use Use of ".()" as an Abbreviation for ".call("I’m going to again strongly disagree here. That is a big mental jump if it’s unfamiliar. And calling 4 characters verbose is a bit of a stretch. And this isn’t just us… it’s also the rubocop default as well https://github.com/rubocop-hq/ruby-style-guide#proc-call/ Replacing do/end with {}You can see some of the conversation in the past here (#109). This is our own custom SemanticBlock rule. Prohibiting Spaces for {}For background, we decided to eliminate spaces for hash literals and require spaces for blocks to make it easier to tell the difference between the two at a glance. See #26 cop: Enforcing Double QuotesThe point of standard is consistency, not options. Upside, you can type out all of your strings as single quotes and standard will autocorrect. Easypeasy. It's definitely easier for most people than having to remember when you need double quotes otherwise. Expression Continuation IndentationI am not following what the issue is here at all. |
Opened #216 and working on a fix for that. |
Here are some thoughts I had when I recently ran
standardrb --fix
on a code base. They are not equal in importance to me, but I mention them all anyway. There's some personal preference here, but there are a lot of objective observations as well. For example, I mention some concepts of user interface design that I believe we as developers are entitled to expect, or at least be permitted to write, in the source code with which we work. In general, I'd love to see more application of these user interface design principles and less of "what I'm used to" in the formation of standards to which we will all be required to conform if we work on a project that uses this tool.Horizontal Alignment of Values and Blocks
This rule violates basic user interface design principles. Horizontally aligning the values allows the reader to spot the values with a minimum of eye movement and effort. There are many instances in code of repeating sets of values and it can be useful to easily scan them. Would any of us ever implement text fields in a web form in this way, where labels are horizontally aligned, but the input fields are horizontally scattered? Why aren't we developers worthy of the same consideration as our users? I understand not requiring alignment, but why prohibit it?
And vs. &&
I realize that
and
is often used erroneously, but the prevailing wisdom, as I understand it, is that&&/||
should be used for logical operations but thatand/or
should be used for flow control (see Avdi Grimm's article at https://avdi.codes/using-and-and-or-in-ruby/). Soand/or
should be permitted. Also, the two pairs differ in precedence rules; I wonder if this could result in runtime errors in some cases as well.Parenthesizing Ternary Conditions
It is often helpful to the reader to wrap a ternary expression's condition in in parentheses, to explicitly show its beginning and end. This is especially true when the condition is more complex than the one in this example. I suggest permitting parentheses.
Method Parameter Indentation
The example in this image is not so bad, but I've seen cases where horizontal space is limited, the method name is longer, and this formatting style pushes parameters way to the right, sometimes out of view. Does standardrb treat long lines the same as the ones in this example?
Use of ".()" as an Abbreviation for ".call("
Callables are objects that respond to a
call
method, and can include Proc's (lambdas and non-lambda procs), and other objects, i.e. instances of other classes that define acall
method. Procs/lambdas are first class objects in Ruby, and the addition to the language of the stabby lambda and.()
notations were nods to the importance of functional programming in Ruby. Prohibiting the use of this abbreviation undoes a Ruby feature that was put there to help us. I realize that it is unfamiliar at first, but the mental translation of.()
to.call()
is a small jump. Using.call
seems innocuous when it is only occasional, but as it is used more, the verbosity becomes distracting and annoying.Replacing do/end with {}
This was the most surprising one to me. I would have expected the reverse to be enforced, since using
do/end
for multiline blocks has been a convention for many years now.Prohibiting Spaces for {}
This one was surprising to me too. I suppose one could justify it by saying we do the same thing with parentheses in method calls, but it's a very uncommon pattern, at least in my experience. If we are to ask developers to break old habits to learn new ones, I think it should be for issues with more objective usability merits than this one.
Enforcing Double Quotes
There have been many times when I have had to input many quoted strings, and I've long appreciated being able to avoid the Shift key when doing so. In addition, I find single quotes more readable (i.e. it is easier to visually distinguish between the quote marks and the text they delimit), especially when there are many of them. It would be nice to continue to have the option to use single quotes.
Expression Continuation Indentation
A somewhat related issue is that this single-indentation-level rule makes it impossible to distinguish (by spacing at least) expression continuations from changes in nesting levels. A convention that has been used by many, for many years, is continuation indentation, which is typically two indentation levels (as shown on the left). My editor, RubyMine, has a separate setting for continuation indents, and possibly many others do as well.
It helps because when scanning the code one can easily spot the horizontal distance between left margins of the first and subsequent lines, and immediately know, before even looking at any text, whether it is a change in nesting level or an expression continuation. This communicates information to the reader that would otherwise not be available before mentally parsing the code itself.
This image is not a great example but was the only one I noticed; a better one would have line continuation and nesting level changes in the same block of code.
The text was updated successfully, but these errors were encountered: