Giter Site home page Giter Site logo

explicit-return's Introduction

explicit-return

Have your methods return nil by default.

module Foo

    include ExplicitReturn

    def self.bar
        123
    end

    def self.baz
        return explicit 123
    end

end

foo.bar #=> nil
foo.baz #=> 123

Why?

Implicit returns are one of Ruby's nicest features. So why throw them out the window?. Let's say, one day I sit down with a headache and hack up the following.

def self.delete_3(array)
    array.each{|e|
        array.delete(e) if e == 3
    }
end

#somewhere else

no_three_array = Foo.delete_3([1,2,3,4,5])

A few months later someone else with a slightly more lucid state of mind comes along and decides to fix my method.

def self.delete_3(array)
    array.delete(3)
end

The problem is that now no_three_array #=> 3. One might argue that the real problem is lack of documentation, counter-intuitive method naming, TDD, etc. ExplicitReturn does not pupport to be a big picture solution, rather a simple saftey guard to avoid shooting yourself in the foot.

How?

Upon inclusion of the ExplicitReturn module, ExplicitReturn will listen to new definintions of methods and will wrap them so that it can check the results and esure they are ExplicitResult objects. ExplicitResult objects are created with the explicit() method. Hence:

return explicit my_obj
#can be rewritten
return explicit(my_obj)

explicit-return's People

Watchers

 avatar  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.