what about allowing to specify, which end belongs to which start?

B

Bob Nadler

Of course, the example was made up to demonstrate the ends and not real
world code. I would not write it in this way, either...

This one is from existing code:

=A0module ABC

=A0 =A0# ...

=A0 =A0def abc
=A0 =A0 =A0x.each do
=A0 =A0 =A0 =A0if x

=A0 =A0 =A0 =A0 =A0case lorem

=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0end
=A0 =A0 =A0end
=A0 =A0end
=A0end

My general rule of thumb is to put long case statements (if I can't avoid
using them) into their own separate method. So I would refactor the code to=
:

module ABC

def abc
x.each do
some_method_name_that_describes_what_case_statement_does(lorem) if x
end
end

def some_method_name_that_describes_what_case_statement_does(value)
case lorem

when :ipsum
# ...
when :ipsum
# ...
when :ipsum
# ...
when :ipsum
# ...
else
# ...
end
end

end

As an aside, take a look at Refactoring: Ruby Edition[1]. It covers ways of
dealing with long methods, case statements, and other situations that
cause difficulties trying
to match up "end"'s.

[1] http://www.amazon.com/Refactoring-Ruby-Jay-Fields/dp/0321603508
 
M

Mark T

In favour of:
*) Keeping with designer's intentions. Least surprise being one of.
*) Keeping understandability as a premium.
*) Using inline documentation where self.doco is not enough.
*) Readability is key, using less verbosity being part of this.
*) Refactoring can be from different perspectives, one's own code, an
imported library or maintenance.
Refactoring a previous post I would use hanging indents to 'indicate'
methods, compress trailing ends on lastline.
Also, I use one space for each indent, this gives me an indicator at
the end of a .code-block. as to having correct closing(s).
I like to read the code, not the end markers.
Different approaches for different code.

module ABC
def abc
x.each do
some_method_name_that_describes_what_case_statement_does(lorem)
if x end end

def some_method_name_that_describes_what_case_statement_does(value)
case lorem
when :ipsum
# ...
when :ipsum
# ...
when :ipsum
# ...
when :ipsum
# ...
else
# ... end end end
 
R

Robert Klemme

2010/7/12 Jan Lelis said:
Of course, the example was made up to demonstrate the ends and not real
world code. I would not write it in this way, either...

This one is from existing code:

=A0module ABC

=A0 =A0# ...

=A0 =A0def abc
=A0 =A0 =A0x.each do
=A0 =A0 =A0 =A0if x

This is probably a different criterion, otherwise the "if" statement
would be superfluous.
=A0 =A0 =A0 =A0 =A0case lorem

=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0end
=A0 =A0 =A0end
=A0 =A0end
=A0end

If you have lengthy case statements you should consider a different
approach because they are relatively slow and awkward to read (as you
noticed already). State and strategy pattern come to mind. Often it
is also advisable to do something like this:

class Foo
MY_OPERATION =3D {
String =3D> lambda {|x| Integer(x)},
Fixnum =3D> lambda {|x| x},
IO =3D> lambda {|x| Integer(x.gets.chomp)}
}

def work(obj)
MY_OPERATION.fetch(obj.class) do |cl|
raise ArgumentError, "Dunno what to do with #{obj.inspect}"
end[obj]
end
end

irb(main):014:0> f =3D Foo.new
=3D> #<Foo:0x10297f34>
irb(main):015:0> f.work 123
=3D> 123
irb(main):016:0> f.work "456"
=3D> 456
irb(main):017:0> f.work $stdin
789
=3D> 789
irb(main):018:0> f.work [1,2,3]
ArgumentError: Dunno what to do with [1, 2, 3]
from (irb):10:in `block in work'
from (irb):9:in `fetch'
from (irb):9:in `work'
from (irb):18
from /opt/bin/irb19:12:in `<main>'
irb(main):019:0>

You can view this as a way to add methods to types without modifying them.

Kind regards

robert

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

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top