# When clever is stupid

**URL:** <https://www.ruby-forum.com/t/when-clever-is-stupid/67273>\
**Category:** Ruby\
**Created:** [September 1, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273 "2006-09-01T19:41:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/1 "2006-09-01T19:41:12Z")

</div>

Some time in the past, I wrote a line of code.

It looked like this:

if pu : pu = pu.url.to\_s != ‘’ ? pu : nil end

There is sat, working fine, for a long time. Today, I was editing the  
code, doing a variation on it for a new site.

I came to that line.

I looked at it.

I called it stupid.

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?

Not much.

Sometimes clever is good, when there is a reason for it. However, that  
time, clever was just plain stupid.

Kirk H.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/2 "2006-09-01T20:25:16Z")

</div>

On 01/09/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/3 "2006-09-01T20:33:17Z")

</div>

On 1/9/2006, at 19:37, [khaines-at-enigo.com](http://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?

```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/4 "2006-09-01T20:57:21Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I came to that line.
> 
> I looked at it.
> 
> I called it stupid.
> 
> 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?

Here, have a cookie. A Cookie of Clarity. It has extra doses of golf  
urge repellent.

Usually, the problem is the “clever” solution was stupid since the  
start. I see nothing clever about just randomly stuffing some logic into  
as few characters as possible - (ab)using the syntax flexibility makes  
sense when you can achieve let’s say a reordering of the terms that’s  
more natural (not necessarily quick) to read than the more common would  
be. That example line of code just requires me to keep the whole ternary  
conditional operator in my head as I try to mentally evaluate the  
condition.

Ternary operators are inherently evil whenever nested, although a bit  
more terse in the good term when used as a single expression. Putting  
the “else” result on a newline and indenting the ? with the : considered  
sexy, I find it very easy to visually separate the condition and the  
different results then using the quadrants split up by the operator.

David V.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/5 "2006-09-01T21:15:23Z")

</div>

David V. wrote:

> condition.
> 
> Ternary operators are inherently evil whenever nested, although a bit  
> more terse in the good term when used as a single expression. Putting  
> the “else” result on a newline and indenting the ? with the : considered  
> sexy, I find it very easy to visually separate the condition and the  
> different results then using the quadrants split up by the operator.

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/6 "2006-09-01T21:23:25Z")

</div>

On Sat, 2 Sep 2006, David V. wrote:

> Here, have a cookie. A Cookie of Clarity. It has extra doses of golf urge  
> repellent.

But is it a tasty cookie? Maybe with a little chocolate (preferably  
dark)? Or maybe some almond and vanilla? Hmmm.

> Usually, the problem is the “clever” solution was stupid since the start. I

Yeah. That’s my point, using my own stupid code to demonstrate. It was  
bad code from the very beginning, both because it’s not immediately  
clear  
what it does and because, as a couple other people demonstrated, it’s  
not  
even as clever as it could be for all its ugliness. I am sure I felt a  
moment of self satisfaction when I wrote it, though. A passing thought  
of, “Yeah, that’s great. Look at that.” I don’t know. Maybe the  
oxygen  
level was a little low in my office that day, maybe the winter winds  
were  
howling a bit too much, or maybe I had skipped lunch that day. Who  
knows.

But it sure was a horrible piece of code to come across this morning.

Kirk H.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/7 "2006-09-01T21:27:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/8 "2006-09-01T21:33:27Z")

</div>

“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.cfcl.com/rdm](http://www.cfcl.com/rdm) Rich M.  
[http://www.cfcl.com/rdm/resume](http://www.cfcl.com/rdm/resume) removed\_email\_address@domain.invalid  
[http://www.cfcl.com/rdm/weblog](http://www.cfcl.com/rdm/weblog) +1 650-873-7841

Technical editing and writing, programming, and web development

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/9 "2006-09-01T22:03:30Z")

</div>

On 9/2/06, Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 9/2/06, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/10 "2006-09-01T21:59:30Z")

</div>

On 9/2/06, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 1, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/11 "2006-09-01T23:25:36Z")

</div>

On 9/1/06, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 12:47am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/12 "2006-09-02T00:47:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 3:01am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/13 "2006-09-02T03:01:31Z")

</div>

David V. wrote:

> 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…

I take my hat off to you good sir. Would you please kindly illustrate  
this to my co-worker? (I’ve tried, but if it isn’t at least three levels  
deep it “isn’t cool!”)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 1:43am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/14 "2006-09-02T01:43:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 4:26am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/15 "2006-09-02T04:26:00Z")

</div>

Daniel W. wrote:

> I take my hat off to you good sir. Would you please kindly illustrate  
> this to my co-worker? (I’ve tried, but if it isn’t at least three levels  
> deep it “isn’t cool!”)

Try a brick. _duck_

Strangely enough, I had assembly / call processing people to work with  
on my last project team (in Java), and I actually managed to bend their  
style to my will over the course of those months. I seem to recall a  
certain thread about the value of complete novices in a field sometimes  
yielding surprising results.

([Iwantoneofthose.com](http://Iwantoneofthose.com) stocks styrofoam bricks. I severely need one for  
the office.)

David V.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 3:09am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/16 "2006-09-02T03:09:53Z")

</div>

On Sat, Sep 02, 2006 at 10:01:55AM +0900, Daniel W. wrote:

> David V. wrote:
> 
> > 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…
> 
> I take my hat off to you good sir. Would you please kindly illustrate  
> this to my co-worker? (I’ve tried, but if it isn’t at least three levels  
> deep it “isn’t cool!”)

I think that’s one of the reasons that long-time C, Java, and Fortran  
programmers (among others) tend to view languages like Perl and Ruby as  
“only a scripting language”: the code doesn’t look complex enough to be  
considered “real programming”. The fact you can do the same things with  
less code complexity (much of the time) is the salient factor that gets  
overlooked.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 10:08am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/17 "2006-09-02T10:08:03Z")

</div>

On 2/9/2006, at 3:07, Chad P. wrote:

> I think that’s one of the reasons that long-time C, Java, and Fortran  
> programmers (among others) tend to view languages like Perl and  
> Ruby as  
> “only a scripting language”: the code doesn’t look complex enough  
> […]

While it may have something to do with it, it is, compared to C,  
?only a scripting language? because you can only use it for a subset  
of programming problems.

For example you wouldn?t write a parser, compiler, 3D engine,  
something like zlib, openssl, or similar in Ruby (because of  
performance), you can?t write device drivers, you can?t interface to  
a lot of things (w/o writing bindings in, you guessed it, C) etc.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 10:38am UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/18 "2006-09-02T10:38:06Z")

</div>

Allan Odgaard wrote:

> For example you wouldn?t write a parser, compiler, 3D engine, something  
> like zlib, openssl, or similar in Ruby (because of performance), you  
> can?t write device drivers, you can?t interface to a lot of things (w/o  
> writing bindings in, you guessed it, C) etc.

You can’t or shouldn’t really do those things in Java or Fortran either.  
C was a _systems_ programming language first, and only evolved into an  
application programming language later. It was designed for writing  
operating systems and compilers and text processing tools. It was almost  
15 years before C compilers were competitive with Fortran compilers for  
number crunching.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/19 "2006-09-02T19:40:52Z")

</div>

On Sat, Sep 02, 2006 at 05:36:49PM +0900, M. Edward (Ed) Borasky wrote:

> > 
> 
> number crunching.  
> . . . and I wouldn’t use C or Fortran for certain classes of complex  
> application programming, either.

I wouldn’t use Java at all, if I could help it, but that’s another  
story.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 2, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/when-clever-is-stupid/67273/20 "2006-09-02T20:34:57Z")

</div>

Chad P. wrote:

> . . . and I wouldn’t use C or Fortran for certain classes of complex  
> application programming, either.  
> There are a lot of things I wouldn’t use Fortran for, although when it  
> was the only high-level language my colleagues were used to, I did. 🙂  
> But despite its original intent as a systems programming language, I  
> can’t think of a single application I wouldn’t write in C if that’s what  
> I was paid to do.

There are tools and programming styles that can make C programming as  
easy as programming in a dynamic language like Ruby or Perl or Python.  
And the whining about the edit/compile/link/test cycle being less  
efficient than the edit/test cycle of a dynamic language I think is just  
that – whining. If your complex application is properly modularized,  
that’s just not a big deal.

C++, on the other hand, I consider a gross abomination. 🙂

> I wouldn’t use Java at all, if I could help it, but that’s another  
> story.  
> Well … I liked Java at one time well enough to choose it over the  
> protestations of management for a project … as an excuse to learn the  
> language. I find Ruby to be a happy blend of all that’s good in Java  
> (objects, classes, methods, garbage collection) and Perl (regular  
> expressions, system administration primitives built in, arrays and  
> hashes, simplified syntax), with a few other nice touches of its own  
> (lambdas, continuations, open classes).

[Next page](https://www.ruby-forum.com/t/when-clever-is-stupid/67273.md?page=2)
