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

[DON'T MERGE] Review additional coding style suggestions #8347

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

Conversation

andrei-epure-sonarsource
Copy link
Contributor

No description provided.


Single variable lambdas should use `x` as the variable name (based on lambda calculus λx). Multi variable lambdas should use descriptive names, where `x` can be used for the main iterated item like `(x, index) => ...`. Name `c` can be used for context of Roslyn callback.

Short names can be used as parameter and variable names, namely `SyntaxTree tree`, `SemanticModel model`, `SyntaxNode node` and `CancellationToken cancel`.

### Method names

FIXME Avoid Get prefixes for method names. Save three characters when it only gets x.Foo.Bar.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would suggest to vote about this one in the 4th step of our process.

Unit tests for common C# and VB.NET rules should use two aliases `using CS = SonarAnalyzer.Rules.CSharp` and `using VB = SonarAnalyzer.Rules.VisualBasic`. Test method names should have `_CS` and `_VB` suffixes.

Unit tests for single language rule should not use alias nor language method suffix.

Variable name `sut` (System Under Test) is recommended in unit tests that really tests a single unit (contrary to our usual rule integration unit tests).

FIXME - Avoid names without meaning like `foo`, `bar`, `baz`. OR KISS?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would suggest to vote about this one in the 4th step of our process.


Unit test method names:
- Underscore in UT names separates logical groups, not individual words.
- FIXME: what should the name pattern be? NEEDS DISCUSSION ([many patterns](https://dzone.com/articles/7-popular-unit-test-naming) and also [Microsoft convention](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#naming-your-tests) - I'd go for MS convention)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would suggest to vote about this one in the 4th step of our process.

{ "there": 42 },
}
```
* FIXME - align on how to use collection initializers int[] x = [ 1, 2, 3 ] or old style (see [slack discussion](https://sonarsource.slack.com/archives/C01H2B58DE1/p1697103918957899?thread_ts=1696951023.295859&cid=C01H2B58DE1))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would suggest to vote about this one in the 4th step of our process.

Copy link

sonarcloud bot commented Nov 10, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link

sonarcloud bot commented Nov 10, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

{ "hey" : 1 },
{ "there": 42 },
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong sytnax.

Suggested change
{ "hey" : 1 },
{ "there": 42 },
["hey"] = 1,
["there"] = 42

Choose a reason for hiding this comment

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

It's my typo, sorry for that Andrei.
And Tim, the one I wanted to suggest is this one:

        var dict = new Dictionary<int,int>
        {
            { 1, 2 },
            { 3, 4 },
        };


## Comments

* Code should contain as few comments as necessary in favor of well-named members and variables.
* Comments should generally be on separate lines.
* Comments on the same line with code are acceptable for short lines of code and short comments.
* Documentation comments for abstract methods and their implementations should be placed only on the abstract method, to avoid duplication. _When reading the implementation, the IDE offers the tooling to peek in the base class and read the method comment._
* Avoid using comments for "Arrange, Act, Assert" in UTs, unless the test is complex.
* Use single-line comments. Exception: `Internal /* for testing */ void Something()`.
Copy link
Contributor

Choose a reason for hiding this comment

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

The naming here is confusing since this is a single line.

Suggested change
* Use single-line comments. Exception: `Internal /* for testing */ void Something()`.
* Use double-slash comments only. Exception: `Internal /* for testing */ void Something()`.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe rather "Do not use /* ... */."


* When to factorize: two is a group, three is a crowd.
* Less is more.
* Rely on Roslyn Type inference to reduce used characters.

Choose a reason for hiding this comment

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

What do you mean by "used characters"?

@@ -218,6 +285,15 @@ It can still be used when and where it makes sense. For instance, when a class h
implementing generic interfaces (such as `IComparable`, `IDisposable`), it can make sense to have regions
for the implementation of these interfaces.

## Spacing

* Avoid spaces unless they bring clarity and help the reader understand logical groups. Prefer spaces over comments.

Choose a reason for hiding this comment

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

By spaces you mean newlines?
It's a bit confusing, someone might read this as using new int[]{1,2,3} instead of new int[] { 1, 2, 3 }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, empty lines, I will update

@pavel-mikula-sonarsource pavel-mikula-sonarsource moved this from In progress to To do in Best Kanban Feb 20, 2024
Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource left a comment

Choose a reason for hiding this comment

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

Overall it looks good. Most of my comments are on adding examples (even for seemingly trivial cases), and we have a couple of topics we need to vote on before we can continue.

Keep it minimal and suggestive.
- Generic words that don't convey meaning (e.g. `Helper`) should be avoided.
- Overwordy and complex names should be avoided as well.
- Use positive naming when possible.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add an example:

  • negative: var shouldNotInclude = true;
  • positive: var shouldInclude = false;

### Principles

Keep it minimal and suggestive.
- Generic words that don't convey meaning (e.g. `Helper`) should be avoided.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Generic words that don't convey meaning (e.g. `Helper`) should be avoided.
- Generic words that don't convey meaning (e.g. `Helper`, `Manager`, `Data`) should be avoided.


Keep it minimal and suggestive.
- Generic words that don't convey meaning (e.g. `Helper`) should be avoided.
- Overwordy and complex names should be avoided as well.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Overwordy and complex names should be avoided as well.
- Overwordy and complex names should be avoided as well. e.g. `SaveAllDataToDatabase()` -> `Save()` (the rest should be understandable from the context).

Unit tests for common C# and VB.NET rules should use two aliases `using CS = SonarAnalyzer.Rules.CSharp` and `using VB = SonarAnalyzer.Rules.VisualBasic`. Test method names should have `_CS` and `_VB` suffixes.

Unit tests for single language rule should not use alias nor language method suffix.

Variable name `sut` (System Under Test) is recommended in unit tests that really tests a single unit (contrary to our usual rule integration unit tests).

FIXME - Avoid names without meaning like `foo`, `bar`, `baz`. OR KISS?
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to avoid these names then let's add some examples for what to use instead. e.g. for an analyzer that raises on methods with empty bodies:

public void Foo() // Noncompliant
{ 
} 

public void Bar() // Compliant
{
   // Some explanation
}

public void Baz()  // Compliant
{
   Console.WriteLine();
}

public override void Kiss() // Compliant
{
}

Instead use names that show how the given member is relevant to the analyzer that's being tested:

public void Empty() // Noncompliant
{ 
} 

public override void HasComment()  // Compliant
{
   // Some explanation
}

public void NotEmpty()  // Compliant
{
   Console.WriteLine();
}

public override void Overriden() // Compliant
{
}

Copy link
Contributor

Choose a reason for hiding this comment

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

If we agree not to use these names then let's add an internal analyzer that enforces it (or make it part of the Verifier).

FIXME - Avoid names without meaning like `foo`, `bar`, `baz`. OR KISS?

Unit test method names:
- Underscore in UT names separates logical groups, not individual words.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add an example (after we voted on the convention).


* When to factorize: two is a group, three is a crowd.
* Less is more.
* Rely on Roslyn Type inference to reduce used characters.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add an example for:

  • the var keyword: string name = "Joe"; -> var name = "Joe";
  • target-typed expressions for fields: private CustomType type = new CustomType(); =>private CustomType type = new();
  • target-typed expressions for parameters: Method(new CustomType()); => Method(new());

{ "there": 42 },
}
```
* FIXME - align on how to use collection initializers int[] x = [ 1, 2, 3 ] or old style (see [slack discussion](https://sonarsource.slack.com/archives/C01H2B58DE1/p1697103918957899?thread_ts=1696951023.295859&cid=C01H2B58DE1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Additional suggestions:

  • use the spread operator instead of Concat()/Append()/Prepend()
  • use [] instead of Array.Empty<T>() or Enumerable.Empty<T>()

@@ -39,6 +39,8 @@ int CallerTwo() => Leaf() + PublicMethod();
int Leaf() => 42;
```

Do not use auto-implemented private properties. Use fields instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is easy to check with an internal analyzer.

Copy link

sonarcloud bot commented May 29, 2024

Quality Gate Passed Quality Gate passed for 'Sonar .NET Java Plugin'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link

sonarcloud bot commented May 29, 2024

Quality Gate Passed Quality Gate passed for 'SonarAnalyzer for .NET'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@@ -39,6 +39,8 @@ int CallerTwo() => Leaf() + PublicMethod();
int Leaf() => 42;
Copy link
Contributor

Choose a reason for hiding this comment

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

I want to do a review once it's ready for it.

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

Successfully merging this pull request may close these issues.

None yet

5 participants