GithubHelp home page GithubHelp logo

anthrax3 / nullguard-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haacked/nullguard

0.0 1.0 0.0 354 KB

An experiment in guarding against null arguments

License: MIT License

PowerShell 3.09% C# 96.91%

nullguard-1's Introduction

NullGuard

The null reference has been called "my billion dollar mistake" by its inventor, Tony Hoare. Guarding against null arguments and return values ends up resulting in a lot of boilerplate code.

This library attempts to remove the repetitive work of all that argument null checking by rewriting IL to check all nullable arguments. For more details, check out the blog post announcing this library.

Install this library.

Install-Package NullGuard.PostSharp

You can apply the EnsureNonNullAspect attribute to individual classes, methods, or an entire assembly. You'll also need to install the PostSharp Visual Studio Extension. This library should work fine with the free community edition.

Usage Examples

using NullGuard;

[assembly: EnsureNonNullAspect]

public class Sample 
{
    public void SomeMethod(string arg) {
        // throws ArgumentNullException if arg is null.
    }

    public void AnotherMethod([AllowNull]string arg) {
        // arg may be null here
    }

    public string MethodWithReturn() {
        // Throws InvalidOperationException if return value is null.
    }
   
    // Null checking works for automatic properties too.
    public string SomeProperty { get; set; }

    [AllowNull] // can be applied to a whole property
    public string NullProperty { get; set; }

    public string NullProperty { 
        get; 
        [param: AllowNull] // Or just the setter.
        set; 
}

Note

For this specific aspect, the code generated by PostSharp Professional Edition 2.1 will be much cleaner and faster than the one generated by PostSharp Starter Edition 2.1 because of the optimizer included in the paid edition. The implementation with PostSharp 3 will be even more better.

nullguard-1's People

Contributors

gfraiteur avatar haacked avatar

Watchers

 avatar

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.