Code Monkey home page Code Monkey logo

agodaanalyzers's People

Contributors

amitashanand avatar asuwansantis avatar chrissx avatar dicko2 avatar ensecoz avatar jean avatar kanokgan avatar mafil avatar manupsunny avatar methaveepa avatar mikechamberlain avatar mpakpinyo avatar raidenyn avatar richardyft avatar robkeim avatar samtade avatar susakpol avatar susakpol-kunvuttivanit avatar szaboopeeter avatar thanavardl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

agodaanalyzers's Issues

AG0022: When designing a class or interface, do not expose both sync and async versions of methods

When designing our own interfaces/classes, we should not expose both synchronous and asynchronous versions of methods.

For example, if methods named MyMethod and MyMethodAsync are declared, then fail.

Fail

class Downloader
{
    public byte[] DownloadFile() { ... } // <-- disallow this declaration, because async version exists
    public Task<byte[]> DownloadFileAsync() { ... }
}

Pass

class Downloader
{
    public Task<byte[]> DownloadFileAsync() { ... }
}

Code fix

  • Delete the synchronous version

https://github.agodadev.io/pages/standards-c-sharp/code-standards/async/expose-async-method.html

Test only public surface for your API

Could we remove the "Test only public surface for your API" rule?

It is not a good rule.

There is nothing wrong with mocking internal methods or using internal classes and unit test them. You should always try to break down complex behavior and test each part separately. If the parts work fine then you know the whole will probably work.

Allowing the testing of only public endpoints is limiting and complicates the test setup.

This rule also encourages using public which is not good.

AG0018: Ensure that publicly exposed IEnumerable types are on our whitelist

If public class/interface method/property return value implements IEnumerable, then it can only be declared as one of the following:

  • IEnumerable<T>
  • ISet<T>
  • IList<T>
  • IDictionary<K, V>
  • IReadOnlyDictionary<K, V>
  • KeyedCollection<K, V>
  • byte[] (special case for raw binary data)
  • string (which happens to implement IEnumerable<char>)

Fail

public List<string> GetStrings() { ... }

Pass

public IList<string> GetStrings() { ... }

AG0034: Check if method parameters are being mutated by reference

I would like to prevent people from misusing the parameters as a way to return some value from a method.
For example, for following code:

public static void Method(A a){
	a.B = new B();
	a.B.C = new C();
}

both assignments are mutating the input parameter and treating it like a return value. The rule would not be applied for ref and out type params, as they imply explicitly that the parameter is / might be mutated.

WebAPI controller that return void break iOS

When a webapi controller returns void, aspnet make it return a 204 instead of 200. iOS/Safari treat this differently and prompt the user for a document download blocking off the website the instant the API method is called from ajax.

We should create a rule that forces all webapi controller methods to return data and not a void.

AG0032: Prevent use of Task.WaitA*

  • Task.WaitAll
  • Task.WaitAny

Code fix

  • rewrite as await Task.WhenAll or await Task.WhenAny
  • add async modifier to method declaration if it does not exist

AG0021: Do not use synchronous version of method when async version exists

When calling external code, if we use method TheirMethod(), but there also exists a method named TheirMethodAsync(), then fail.

Say we have an external library that exposes both sync and async versions of the same method:

// external lib
namespace AwesomeSoft
{
    public class Downloader
    {
        public byte[] DownloadFile() { ... }
        public Task<byte[]> DownloadFileAsync() { ... }
    }
}

Fail

// our code
var client = new Downloader();
var bytes = client.DownloadFile(...); // should not use synchronous version when async version available

Pass

// our code
var client = new Downloader();
var bytes = await client.DownloadFileAsync(...)

Code fix:

  • Transform method name by appending Async
  • Add await
  • Add async modifier to method declaration if it does not exist

https://github.agodadev.io/pages/standards-c-sharp/code-standards/async/consume-async-method.html

AG0020: Rule pickup wrong statement.

Look at this example.

public static List<SomeThing> GetList()
{
    var object = isSomeCondition ? GetObject() : null;

    var someThings = new List<SomeThing>();
    somThings.Add(object);
    
    return someThings;
}

GetObject only return Object of SomeThing but this rules also report line var object = isSomeCondition ? GetObject() : null; as it violate the rule.

Improve CodeFixVerifier error reporting

When writing test case for CodeFixer, CodeFixVerifier.cs helps check if fixer introduce any new diagnostics:

https://github.com/agoda-com/AgodaAnalyzers/blob/master/src/Agoda.Analyzers.Test/Helpers/CodeFixVerifier.cs#L383

The error report, showing only new diagnostics, is quite misleading.

Example:

Before a fix:

using System;
using System.Collections.Generic;

namespace Agoda.Analyzers.Test
{
    public class TestClass
    {
        public int[] GetValuesForId(int id)
        {
            return null;
        }
    }
}

After a fix:

using System;
using System.Collections.Generic;

namespace Agoda.Analyzers.Test
{
    public class TestClass
    {
        public int[] GetValuesForId(int id)
        {
            return Array.Empty<int>();
        }
    }
}

Error report:

Message:   Fix introduced new compiler diagnostics:
Test0.cs(7,18): warning CS1591: Missing XML comment for publicly visible type or member 'TestClass'
Test0.cs(9,22): warning CS1591: Missing XML comment for publicly visible type or member 'TestClass.GetValuesForId(int)'

Actual diagnostics report:
image

Before a fix: using System; is unused.
After a fix: using System; is used, and this cause a different number of diagnostics returned.
We should display both before a fix and after a fix diagnostics.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.