Each time you code 'unless... else'...

Discussion in 'Ruby' started by Phlip, Jan 28, 2008.

  1. Phlip

    Phlip Guest

    unless File.exist? CONF_PATH
    Ultrasphinx.say "configuration file not found for #{RAILS_ENV.inspect}
    environment"
    Ultrasphinx.say "please run 'rake ultrasphinx:configure'"
    else
    begin
    lines = open(CONF_PATH).readlines

    sources = lines.select do |line|


    ....Satan waterboards a kitten.
     
    Phlip, Jan 28, 2008
    #1
    1. Advertising

  2. Phlip

    Phlip Guest

    > unless File.exist? CONF_PATH
    > Ultrasphinx.say "configuration file not found for
    > #{RAILS_ENV.inspect} environment"
    > Ultrasphinx.say "please run 'rake ultrasphinx:configure'"
    > else
    > begin
    > lines = open(CONF_PATH).readlines
    >
    > sources = lines.select do |line|
    >
    > ...Satan waterboards a kitten.


    Just a note - we have debugged the second bug in this library, in as many days,
    and it also happened in the immediate vicinity of _another_ unless... else.

    I'm picking a longhair tabby for this one.

    --
    Satan
     
    Phlip, Jan 29, 2008
    #2
    1. Advertising

  3. On 29.01.2008 21:26, Phlip wrote:
    >> unless File.exist? CONF_PATH
    >> Ultrasphinx.say "configuration file not found for
    >> #{RAILS_ENV.inspect} environment"
    >> Ultrasphinx.say "please run 'rake ultrasphinx:configure'"


    Isn't this rather a case for an exception?

    raise "please run 'rake ultrasphinx:configure'" unless File.exists?
    CONF_PATH

    >> else
    >> begin
    >> lines = open(CONF_PATH).readlines
    >>
    >> sources = lines.select do |line|
    >>
    >> ...Satan waterboards a kitten.

    >
    > Just a note - we have debugged the second bug in this library, in as
    > many days, and it also happened in the immediate vicinity of _another_
    > unless... else.
    >
    > I'm picking a longhair tabby for this one.


    Uh...

    robert
     
    Robert Klemme, Jan 30, 2008
    #3
  4. Phlip

    Phlip Guest

    Robert Klemme wrote:

    > Isn't this rather a case for an exception?
    >
    > raise "please run 'rake ultrasphinx:configure'" unless File.exists?
    > CONF_PATH


    (I didn't write it, but) absolutely. That would have prevented me debugging down
    to it.

    There's a reason professors of BDUF techniques used to say a routine should have
    a single entry and a single exit. The reason is not the exit count itself.
    Applying the rule forces you to examine and clean up your control flow, between
    that entry and exit.
     
    Phlip, Jan 30, 2008
    #4
  5. On 30.01.2008 20:27, Phlip wrote:
    > There's a reason professors of BDUF techniques used to say a routine
    > should have a single entry and a single exit. The reason is not the exit
    > count itself. Applying the rule forces you to examine and clean up your
    > control flow, between that entry and exit.


    I am not a big fan of that rule. Multiple exits can come in handy, for
    example when doing this:

    # superfluous in Ruby of course
    def find(enum, x)
    enum.each do |elm|
    return elm if x == elm
    end
    nil
    end

    Avoiding multiple exits can lead to increased indentation because you
    might need more control constructs.

    Especially in programming languages with exceptions the "single exit"
    rule is pretty useless - unless you do not count exceptions.

    Kind regards

    robert
     
    Robert Klemme, Jan 31, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bengt Richter
    Replies:
    16
    Views:
    557
    Steven D'Aprano
    Jan 21, 2006
  2. Replies:
    25
    Views:
    694
    MonkeeSage
    Oct 1, 2006
  3. kj
    Replies:
    15
    Views:
    591
    Lawrence D'Oliveiro
    May 23, 2009
  4. A
    Replies:
    8
    Views:
    827
    James Kanze
    Aug 28, 2010
  5. Gábor SEBESTYÉN

    Unless unless

    Gábor SEBESTYÉN, Jun 17, 2005, in forum: Ruby
    Replies:
    3
    Views:
    171
    Gábor SEBESTYÉN
    Jun 17, 2005
Loading...

Share This Page