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

P

Phlip

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

Phlip

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

Robert Klemme

Isn't this rather a case for an exception?

raise "please run 'rake ultrasphinx:configure'" unless File.exists?
CONF_PATH
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
 
P

Phlip

Robert said:
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.
 
R

Robert Klemme

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top