[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

When clever is stupid

khaines

9/1/2006 5:38:00 PM

32 Answers

Paul Battley

9/1/2006 6:23:00 PM

0

On 01/09/06, khaines@enigo.com <khaines@enigo.com> wrote:
> if pu : pu = pu.url.to_s != '' ? pu : nil end
> ...
> Sure, it's concise, but what did I gain by making it so concise over
> making it so that it was clearly readable a year and a half later?
> ...
> Sometimes clever is good, when there is a reason for it. However, that
> time, clever was just plain stupid.

Perhaps it's just not clever enough! ;-) It's concise, but it could be
more concise and maybe a little clearer at the same time:

pu = nil if pu && pu.url.to_s.empty?

... although I think I'd be more inclined to write it out using the
multi-line if form.

Paul.

Allan Odgaard

9/1/2006 6:27:00 PM

0

On 1/9/2006, at 19:37, khaines-at-enigo.com wrote:

> [...] It looked like this:
>
> if pu : pu = pu.url.to_s != '' ? pu : nil end
>
> [...] Sure, it's concise [...]

> concise: giving a lot of information clearly
> and in a few words; brief but comprehensive

Seeing how it assigns pu to pu (in one case) I would classify that
part as redundant :)

Here’s my 20% more concise version:

pu = nil if pu && pu.url.to_s.empty?



Gavin Kistner

9/1/2006 7:20:00 PM

0

> Ternary operators are inherently evil whenever nested, although a bit
> more terse in the good term when used as a single expression.

The one exception I would list is something like a chained comparison.
For example, a JavaScript implementation of the spaceship operator:

result = a>b ? 1 : a<b ? -1 : 0

As with all things, beauty is in the eye of the beholder, but I find
that "readable" and certainly nicer than:

result = if a>b
1
elsif a<b
-1
else
0
end

Especially when chaining fallbacks (like comparing on more than two
values).

khaines

9/1/2006 7:23:00 PM

0

Rich Morin

9/1/2006 7:32:00 PM

0

"The Elements of Programming Style", although written decades
ago, holds up as an excellent guide to general practice. I'd
love to see a version of it (and Perl Best Practices) for Ruby.

-r
--
http://www.cf... Rich Morin
http://www.cf.../resume rdm@cfcl.com
http://www.cf.../weblog +1 650-873-7841

Technical editing and writing, programming, and web development

Martin DeMello

9/1/2006 7:53:00 PM

0

On 9/2/06, Phrogz <gavin@refinery.com> wrote:
> > Ternary operators are inherently evil whenever nested, although a bit
> > more terse in the good term when used as a single expression.
>
> The one exception I would list is something like a chained comparison.
> For example, a JavaScript implementation of the spaceship operator:
>
> result = a>b ? 1 : a<b ? -1 : 0
>
> As with all things, beauty is in the eye of the beholder, but I find
> that "readable" and certainly nicer than:
>
> result = if a>b
> 1
> elsif a<b
> -1
> else
> 0
> end

I like the "case true" idiom here:

result = case true
when a == b: 0
when a < b : 1
when a > b : -1
end

martin

Martin DeMello

9/1/2006 7:54:00 PM

0

On 9/2/06, Martin DeMello <martindemello@gmail.com> wrote:
> On 9/2/06, Phrogz <gavin@refinery.com> wrote:
> > The one exception I would list is something like a chained comparison.
> > For example, a JavaScript implementation of the spaceship operator:
> >
> > result = a>b ? 1 : a<b ? -1 : 0
>
> I like the "case true" idiom here:

Oops - missed the "javascript" bit.

martin

Austin Ziegler

9/1/2006 9:16:00 PM

0

On 9/1/06, Phrogz <gavin@refinery.com> wrote:
> The one exception I would list is something like a chained comparison.
> For example, a JavaScript implementation of the spaceship operator:
>
> result = a>b ? 1 : a<b ? -1 : 0

I would do that as:

resul = (a > b ? 1 : (a < b ? -1 : 0))

That's clearer to me, at least.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Joel VanderWerf

9/1/2006 10:38:00 PM

0

Martin DeMello wrote:
> I like the "case true" idiom here:
>
> result = case true
> when a == b: 0
> when a < b : 1
> when a > b : -1
> end
>
> martin

Going further OT, but you can omit the "true":

result = case
when a == b: 0
when a < b : 1
when a > b : -1
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

David Vallner

9/1/2006 11:43:00 PM

0

M. Edward (Ed) Borasky wrote:
> I'm not a big fan of nested conditionals -- there's usually an elegant
> refactoring that can be done when you have them. In fact, I'm not a big
> fan of nested anything. :)
>

With the usual messy codebase to maintain, I thought I was the only one
left whose method / function / whatever extraction finger gets itchy
around any and all control structures with more than one statement in
them...

David Vallner