A question of style

When you write something like

def foo(var)
  if test(var)
    bar(...)
    log(...)
  else
    bar(...)
    log(...)
  end
end

tools like reek complain (two duplications, long method,...), and
somehow push you to write

def foo(var)
  test(var) ? do_this : do_that
end
def do_this ... end
def do_that ... end

I still do prefer the first variant which shows more clearly the
parallel intentions.

What do you people think ?
_md

···

--
Posted via http://www.ruby-forum.com/.

I think the conditional is completely superfluous. Or are there any
differences in the two branches? I can't see any...

What does the real code look like?

Kind regards

robert

···

On Thu, Apr 26, 2012 at 1:33 PM, Michel Demazure <lists@ruby-forum.com> wrote:

When you write something like

def foo(var)
if test(var)
bar(...)
log(...)
else
bar(...)
log(...)
end
end

tools like reek complain (two duplications, long method,...), and
somehow push you to write

def foo(var)
test(var) ? do_this : do_that
end
def do_this ... end
def do_that ... end

I still do prefer the first variant which shows more clearly the
parallel intentions.

What do you people think ?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1058456:

What does the real code look like?

One example :

      def create_report
        report = @recipe.run_with_logger(@parameter_values)
        if report
          @report = report
          J2R.logger.info("report created")
          bottom_message "Nombre de lignes #{@report.size}"
        else
          J2R.logger.error("error creating report")
          bottom_message "Erreur à la création du rapport"
        end
      end

_md

···

--
Posted via http://www.ruby-forum.com/\.

Michel Demazure wrote in post #1058458:

Robert Klemme wrote in post #1058456:

There is even a static analyser for which "else" is a smell.

_md

···

--
Posted via http://www.ruby-forum.com/\.

Looks perfectly OK to me. Creating two additional methods seems like
a lot of overkill which hinders readability. My 0.02 EUR...

Kind regards

robert

···

On Thu, Apr 26, 2012 at 1:47 PM, Michel Demazure <lists@ruby-forum.com> wrote:

Robert Klemme wrote in post #1058456:

What does the real code look like?

One example :

 def create\_report
   report = @recipe\.run\_with\_logger\(@parameter\_values\)
   if report
     @report = report
     J2R\.logger\.info\(&quot;report created&quot;\)
     bottom\_message &quot;Nombre de lignes \#\{@report\.size\}&quot;
   else
     J2R\.logger\.error\(&quot;error creating report&quot;\)
     bottom\_message &quot;Erreur à la création du rapport&quot;
   end
 end

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1058460:

···

On Thu, Apr 26, 2012 at 1:47 PM, Michel Demazure <lists@ruby-forum.com> > wrote:

    J2R.logger.info("report created")
    bottom_message "Nombre de lignes #{@report.size}"
   else
    J2R.logger.error("error creating report")
    bottom_message "Erreur la cration du rapport"
   end
  end

Looks perfectly OK to me. Creating two additional methods seems like
a lot of overkill which hinders readability. My 0.02 EUR...

Kind regards

robert

Glad you agree ! reek can be overstuffed.

(I guess you are somewhere farther on the Loire. Sun in Tours for the
time being...).

_md

--
Posted via http://www.ruby-forum.com/\.