Git Product home page Git Product logo

guard_clauses's Introduction


coverage ย  build

Guard Clauses

A dart port of the excellent C# Guard Clauses package by Ardalis.

A simple extensible package with guard clause extensions.

A guard clause is a software pattern that simplifies complex functions by "failing fast", checking for invalid inputs up front and immediately failing if any are found.

Usage

void processOrder(Order? order)
{
    Guard.against.nullValue(order, 'order');

    // process order here
}

// OR

class Order
{
    String _name;
    int _quantity;
    double _max;
    double _unitPrice;

    Order(String name, int quantity, double max, double unitPrice) {
        _name = Guard.against.nullOrWhiteSpace(name);
        _quantity = Guard.against.negativeOrZero(quantity);
        _max = Guard.against.zero(max);
        _unitPrice = Guard.against.negative(unitPrice);
    }
}

Supported Guard Clauses

  • Guard.against.invalidInput (throws if predicate expression returns false)
  • Guard.against.invalidFormat (throws if the input string doesn't match the provided regex)
  • Guard.against.negative (throws if the input is negative)
  • Guard.against.negativeOrZero (throws if the input is negative or zero)
  • Guard.against.nullValue (throws if input is null)
  • Guard.against.nullOrEmpty (throws if string input is null or empty)
  • Guard.against.nullOrEmptyCollection (throws if the input collection is null or empty)
  • Guard.against.nullOrWhiteSpace (throws if string input is null, empty or whitespace)
  • Guard.against.nullOrInvalidInput (throws if input is null, or predicate expression returns false)
  • Guard.against.indexOutOfRange (throws if input is not a valid index)
  • Guard.against.outOfRangeItems (throws if any values in the input collection are outside the provided range)
  • Guard.against.outOfRange (throws if the input collection is outside the provided range)
  • Guard.against.zero (throws if number input is zero)

Extending with your own Guard Clauses

To extend by creating your own guard clauses, create a new extension class for Guard.

extension GuardExtensions on Guard {
  /// Throws an ArgumentError if [input] is 'foo'.
  /// Returns [input] otherwise.
  String foo(String input, {String? name}) {
    if (input.toLowerCase()) {
      throw ArgumentError.value(input, name);
    }

    return input;
  }
}

// Usage
void sumpin(String otherSumpin) {
    Guard.against.foo(otherSumpin);
    ...
}

References

The references are C#-centric but the concepts apply well.

guard_clauses's People

Contributors

chrishibler avatar

Stargazers

Mahmoud Heretani avatar  avatar

Watchers

 avatar

guard_clauses's Issues

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.