GithubHelp home page GithubHelp logo

anuviswan / scanf Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 1.0 927 KB

SCANF (Static Code Analysis aNd Fixes) is a code analysis package build using .Net Compiler SDK. The development is still in early stages and this document would be updated soon.

C# 87.59% PowerShell 2.75% JavaScript 1.78% HTML 1.40% CSS 2.64% Vue 3.71% Shell 0.13%

scanf's Introduction

Anu Viswan

Hey there I'm Anu Viswan, as Principal Engineer at Beo Software, India . Prior to my current profile, I ....

  • Cofounded Ecsolvo Technologies, where I guided and developed applications in Education and Tourisim domain.
  • used to be Project Manager with Cascade Revenue Management, where I managed, mentored and co-developed solutions in HealthCare Industry
  • was a Principal Technical Lead with Calpine Group, where I worked on developing solutions in Life Science domain

You can find more details on my professional career in my Linkedin Profile.

Contributions

Anu's github stats

Recent Blogs

Reach me

scanf's People

Contributors

anuviswan avatar

Watchers

 avatar  avatar

Forkers

bubdm

scanf's Issues

Proposal: Sealed Class should not have protected members

๐Ÿ”–[Tags],[Tags]

Category : Code Smell

Description : Sealed Class should not have protected members

Detailed description of the problem

Non-Complaint Code

// non-complaint code here

Code Fix

Code Fix with [FixName] raised.

// Fix code here

Proposal : Reminder for TODO Tags

๐Ÿ”–Tags

Category : Code Smell

Description : TODO Tags allows developers to track to be done items via the Task List Window. However, these quite often end up not-tracked.

Detailed description of the problem

Non-Complaint Code

// non-complaint code here

Code Fix

Code Fix with [FixName] raised.

// Fix code here

Proposal : Avoid empty methods

Avoid Empty Methods

๐Ÿ”–Methods, Clean Code

Category: Code Smell

Description: Empty Methods should be avoided.

Empty methods in the code base could be avoided

Non-Complaint Code

public void Foo()
{
}

Code Fix

Code Fix with NotImplementedException raised.

public void Foo()
{
    throw new NotImplementedException();
}

Proposal : Use .Any() instead of Count == 0

๐Ÿ”–Linq, Collection

**Category: Code Smell

**Code: SF1006

Description : Use .Any() instead of Count == 0

Use .Any() instead of Count == 0

Non-Complaint Code

if(myList.Count == 0)
{
}

Code Fix

if(myList.Any())
{
}

Proposal : Constructor should not invoke any virtual methods

๐Ÿ”–[Tags],[Tags]

Category : Bug
RuleID : 1009

Description : Constructor should not invoke any virtual methods

Constructor should not invoke any virtual methods

Non-Complaint Code

public class Base
{
	protected int InternalValue;
	public Base()
	{
		InternalValue = -10;
		Foo();
	}

	public virtual void Foo()
	{
		InternalValue++;
	}
	
	public void Print()
	{
		Console.WriteLine(InternalValue);
	}
}




public class Child:Base
{
	public Child()
	{
		InternalValue = 10;
	}
	
	public override void Foo()
	{
		InternalValue+=2;
	}
}

Code Fix

Code Fix with [FixName] raised.

// Fix code here

Create Rule Description Item

Create a rule description item. Each of the item will be a Markdown Viewer. The actual data would be fetched from the generic webstore.

The story would only concern itself with the mock object. It would not integrate Web Function, which would be done as a separate task

Proposal : Exceptions should be "public"

๐Ÿ”–Exception

Category : Code Smell
ID SF1010

Description : Custom exceptions should be defined as "public"

Non-public exception would conceal the exception from clients, forcing them to use generic or closest exception. This would defeat the purpose of having custom exception and custom information associated with it.

Non-Complaint Code

internal class CustomException:Exception
{
}

Code Fix

public class CustomException:Exception
{
}

Create Rule List Container

Create the Rule List container. The Rule List container would contain a collection of Rule Summary Item. Each of the Rule Summary Item would contain

  • Rule Title
  • Rule Category

This Story is only concerned about mock item. The integration with the web function would be a separate task

Proposal : Async method names should have a suffix 'Async'

๐Ÿ”–async, naming-conventions

Category : Naming Conventions
Id : SF1008

Description : Async method should be named with a suffix Async.

Async method should be named with a suffix Async whenever possible. The Async suffix convention would communicate to the client that the method is awaitable.

Non-Complaint Code

public Task GetData()
{
}

Code Fix

Code Fix with method named.

public Task GetDataAsync()
{
}

Proposal : Initialize Collection Capacity whenever possible

Initialize Collection Capacity whenever possible

๐Ÿ”–Collection, Memory Management

Category: Code Smell

Code: SF1001

Description: Initialize Collection with a likely capacity

C# Collections like List<T> is implemented with an array in the background. The size of array would grow (array is resized) as items are added in the collection. Following code shows typical implimentation of the List.Add() method.

public void Add(T item)
{
    if (_size == _items.Length) EnsureCapacity(_size + 1);
    _items[_size++] = item;
    _version++;
}

private void EnsureCapacity(int min)
{
    if (_items.Length < min) {
        int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;

        if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
        if (newCapacity < min) newCapacity = min;
        Capacity = newCapacity;
    }
}

public int Capacity
{
    get {
        Contract.Ensures(Contract.Result<int>() >= 0);
        return _items.Length;
    }
    set {
        if (value < _size) {
            ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
        }
        Contract.EndContractBlock();

        if (value != _items.Length) {
            if (value > 0) {
                T[] newItems = new T[value];
                if (_size > 0) {
                    Array.Copy(_items, 0, newItems, 0, _size);
                }
                _items = newItems;
            }
            else {
                _items = _emptyArray;
            }
        }
    }
}

Every time the size of array hits the capacity, the capacity is doubled and the Array is recreated with the new Capacity. All the elements of the previous copy of array is then copied over. This is an expensive operation, if occured too many times. Also, since the capacity is being doubled, there is every possiblity that the array could end up with a little less than half its assigned memory unallocated.

The better way to make use of collections would be to intialize it with a likely size during construction.

Non-Complaint Code

var persons = GetPersonList();
var studentList = new List<Student>();
foreach(var person in persons)
{
    studentList.Add(new Student
    {
        FirstName = person.FirstName,
        LastName = person.LastName
    });
}

Fix

var persons = GetPersonList();
var studentList = new List<Student>(persons.Count());
foreach(var person in persons)
{
    studentList.Add(new Student
    {
        FirstName = person.FirstName,
        LastName = person.LastName
    });
}

Proposal : Pure methods should be return value

๐Ÿ”–Methods

Category : Code Smell

Description : Pure Methods should return value

Methods decorated with the PureAttribute should return value and should not be a void method.

Non-Complaint Code

[Pure]
void Foo()
{
}

Code Fix

Code Fix with [FixName] raised.

[Pure]
int Foo()
{
}

void Foo()
{
}

Proposal : Enums with FlagAttribute should have its values power of 2.

๐Ÿ”–[Tags],[Tags]

Category : Code Smell

Description : Enums with Flag Attribute should have its values as the power of 2

The FlagsAttribute enables members of an Enumeration to be meaningful combined or in other words be treated as a set of flags. For successful and meaningful combining of the enum members, the values of each member should be set such that a bitwise operation can be performed on the numeric value. Defining enumeration with powers of two ensures that the combined enumeration constants do not overlap.

Non-Complaint Code

[Flags]
public enum Days
{
  Monday = 1,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

Code Fix

Code Fix with [FixName] raised.

[Flags]
public enum Days
{
  Monday = 0,
  Tuesday = 1,
  Wednesday = 2,
  Thursday = 4,
  Friday = 8,
  Saturday = 16,
  Sunday = 32
}

Proposal : Enums should have unique value

๐Ÿ”–[Tags],[Tags]

Category : [Category]

Description : Short description of the problem

Detailed description of the problem

Non-Complaint Code

// non-complaint code here

Code Fix

Code Fix with [FixName] raised.

// Fix code here

Proposal : Classes with IDisposable Fields should be disposable

๐Ÿ”–[Tags],[Tags]

Category : Bug

Description : Classes that implement disposable fields should ensure the fields are disposed. Hence, the classes themselves should implement IDisposable

Detailed description of the problem

Non-Complaint Code

// non-complaint code here

Code Fix

Code Fix with [FixName] raised.

// Fix code here

Support for .Net Standard 2.1

The analyzer needs to be upgraded to support .Net Standard 2.1. This is currently a blocker, considering inability to upgrade the vsix project to .Net 5.

Create Rules Portal

Create a portal which would provide details of the rules implemented in Scanf. The application would contain a simple interface with 20-80 template.

LHS Side (20% side)

  • Display List of all rules.
  • Should be searchable by Title.
  • Can be grouped by Category

RHS Side (80% Side)

  • Should display the selected Rule
  • Should read and display the rule definition from an markdown file.

Proposal: Async method should not return void

Async method should not return void

๐Ÿ”– Async

Category : Code Smell

Rule Id : SF1005

Description : Async method should not return void

When an exception is thrown in an async Task method, the exception is captured and placed in the Task object. When an asynchronous method returns void, the absence of Task object would lead to the exception to be thrown directly on the active SynchronizationContext. Furthermore, async void methods are difficult to test.

The sole purpose of async void should be in the eventHandlers

Non-Complaint Code

public async void Foo()
{
  await DoBar();
}

Code Fix

Code Fix with [FixName] raised.

public async Task Foo()
{
  await DoBar();
}

Proposal : Enum Flags should always declare a NONE

๐Ÿ”–[Tags],[Tags]

Category : Code Smell

RuleID : 1011
Description : Enum Flags should always declare a NONE

Detailed description of the problem

Non-Complaint Code

// non-complaint code here

Code Fix

Code Fix with [FixName] raised.

// Fix code here

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.