question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Readability rule - Operator placement for wrapped expressions

See original GitHub issue

(EOL = End of Line, BOL = Beginning of Line)

I think StyleCop should dictate (or at least try to dictate) the position of operators. An example:

this is on one line, and it’s ok…

if (true && false) 
{ 
} 

But on two lines?

if (true && 
false) 

or

if (true 
&& false) 

the same for +, -, *, /

And then there are ? (with 😃 and ??.

I think that these operators should be put at End of Line, so that if the second operand is a brace the line begins with the brace.

last but not least is the “.” operator. Here I think it’s easier. It should go on the new line for the same reason as the “this” that is necessary. When you press “.” the vs shows you the list of the properties/methods/fields of the object.

So it should be:

var temp3 = new int[5]
.Cast<long>()
.Cast<int>();

Originally posted by xanatos http://stylecop.codeplex.com/workitem/6791

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
nahk-ivanovcommented, Sep 16, 2020

In our team we ran in to this issue due to assignment operator. Half of the legacy code has it placed on the next line like so:

var myVariable
    = "Hello world";

We would definitely like to invest in StyleCop to have a rule (+auto-fix for this), so that we can quickly clean in up. Was there ever a consensus on a particular set of operators and how majority of people would want them to be formatted?

From what I understand current preference is:

fooBar
   .Method()
   .SomeOtherMethod();

fooBar
    ?.Method()
    ?.SomeOtherMethod();

var myVariable =
    "Hello world";

Maybe we can start with this set and expand it later, once/if we reach an agreement?

As to the name, since we want some operators on the previous line and some on the next - I think we have two options:

  1. Add one rule Binary operators should be placed correctly (or remove binary, as Elvis, aka ?:, is not binary and we may include it in the future)
  2. Add two rules:
    • one for Binary operators should be placed on the line with first operand
    • two for Binary operators should be placed on the line with second operand

My vote is for Operators should be placed correctly.


I understand people may have different preferences, but I thinking allowing people to choose how it should be formatted is defeating the purpose of common coding styles. Yeah, it works if you only care about your project, but if you work on multiple projects it doesn’t help much with readability. Another problem is when you want to integrate two different code-bases together and they use different styles.

In my opinion, we want coding styles to be attached to the language, rather than the project.

2reactions
Przemyslaw-Wcommented, Oct 19, 2015

Why does

int foo = Bar
    ?.Baz
    ?.Blech;

look wrong to you? It immediately gives you proper info that Baz and Blech are called conditionally. No need to scan end of previous line to see this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rules.md - nicklockwood/SwiftFormat
Wrap braces in accordance with selected style (K&R or Allman). Option, Description. --allman, Use allman indentation style: "true" or "false" (default) ...
Read more >
Chapter 16. DRL (Drools Rule Language) rules
This tag determines the position of a declared fact type attribute or field in a positional argument, overriding the default declared order of...
Read more >
Proposed rule: Outsourcing by Investment Advisers
ACTION: Proposed rule. SUMMARY: The Securities and Exchange Commission (“Commission” or “SEC”) is proposing a new rule under the Investment ...
Read more >
Python Style Guide — CSCI 134: Introduction to Computer Science
Rule 2.3: Use “camel case” to improve readability of multiword identifiers. Rule 2.4: Only use underscores to indicate privacy or special variables. Rule...
Read more >
PEP 8 – Style Guide for Python Code
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found