# Can this code be more succinct

**URL:** <https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739>\
**Category:** Ruby\
**Created:** [April 29, 2009, 3:50am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739 "2009-04-29T03:50:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 29, 2009, 3:50am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/1 "2009-04-29T03:50:25Z")

</div>

Hi All,

I like to use the following form for handling error situations:

( puts “ERROR: Long msg”;  
exit) if some\_boolean \_expression

I want the multiple form because the error messages are often long and  
sometimes multi-line.

Is there any way to improve it by getting rid of the parentheses?

Thanks in advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 29, 2009, 4:05am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/2 "2009-04-29T04:05:38Z")

</div>

RichardOnRails wrote:

> Is there any way to improve it by getting rid of the parentheses?  
> if some\_boolean\_expression  
> puts “ERROR: Long msg”  
> exit  
> end #SCNR

Seriously, though, what about this:

abort "ERROR: Long msg " +  
“more message” if true

or

abort %{  
line 1  
line 2  
} if true

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 29, 2009, 4:05am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/3 "2009-04-29T04:05:44Z")

</div>

RichardOnRails wrote:

> Hi All,
> 
> I like to use the following form for handling error situations:
> 
> ( puts “ERROR: Long msg”;  
> exit) if some\_boolean \_expression
> 
> I want the multiple form because the error messages are often long and  
> sometimes multi-line.
> 
> Is there any way to improve it by getting rid of the parentheses?
> 
> Thanks in advance,  
> Richard

Why use such a tortured construct in the first place? This is much  
clearer:

if condition  
puts \<\<-ENDOFSTRING  
warning error warning error warning error warning error  
warning error warning error warning error warning error  
warning error warning error warning error warning error  
warning error warning error warning error warning error  
warning error warning error warning error warning error  
ENDOFSTRING

exit  
end

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 29, 2009, 5:40am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/4 "2009-04-29T05:40:59Z")

</div>

On Apr 28, 9:45 pm, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way to improve it by getting rid of the parentheses?
> 
> Thanks in advance,  
> Richard

Thank you both for your generous, expert advice.

Here are two statements I now have in my app instead of my former,  
convoluted and heavily parenthesizes statements:

[http://www.pastie.org/462055](http://www.pastie.org/462055)

With gratitude,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 29, 2009, 12:36pm UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/5 "2009-04-29T12:36:43Z")

</div>

I can’t help but feel that instead of getting more succint it seems to  
become more and more verbose … 😉

Except for the one-liner abort, which looks quite nice.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 30, 2009, 12:16am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/6 "2009-04-30T00:16:46Z")

</div>

I can’t help but think this could be wrapped up in a nice, neat case  
statement that is perhaps not more succinct but more readable.

case arg = ARGV[0]  
when /^os$/  
sym = arg.downcase  
else  
abort “ERROR: invalid arg #{arg}”  
end

but I’m not sure whether the number of args check can be handled  
inside the case. I tried a ‘when ARGV.size \> 1’ but you can’t put a  
full expression in a when statement.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 30, 2009, 1:33am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/7 "2009-04-30T01:33:22Z")

</div>

On 4/30/09, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but I’m not sure whether the number of args check can be handled  
> inside the case. I tried a ‘when ARGV.size \> 1’ but you can’t put a  
> full expression in a when statement.

there’s another form for case.  
try this (untested),

arg = ARGV[0]  
case  
when ARGV.size \> 1  
abort “too many args ;-)”  
when /^os$/ =~ arg  
sym = arg.downcase  
else  
abort “ERROR: invalid arg #{arg}”  
end

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 29, 2009, 8:01am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/8 "2009-04-29T08:01:01Z")

</div>

On 29.04.2009 05:38, RichardOnRails wrote:

> > sometimes multi-line.
> 
> [http://www.pastie.org/462055](http://www.pastie.org/462055)

Frankly, I find this bit still quite convoluted, especially because of  
the assignment in “unless”. How about:

arg = ARGV.shift || ‘os’  
abort “ERROR:…” unless /\Aos\z/i =~ arg  
abort “ERROR: multiple…” unless ARGV.empty?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 30, 2009, 8:31am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/9 "2009-04-30T08:31:21Z")

</div>

botp wrote:

> On 4/30/09, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > but I’m not sure whether the number of args check can be handled  
> > inside the case. I tried a ‘when ARGV.size \> 1’ but you can’t put a  
> > full expression in a when statement.
> 
> there’s another form for case.  
> try this (untested),
> 
> arg = ARGV[0]  
> case  
> when ARGV.size \> 1  
> abort “too many args ;-)”  
> when /^os$/ =~ arg  
> sym = arg.downcase  
> else  
> abort “ERROR: invalid arg #{arg}”  
> end

…but then instead of having to look up the format of a case statement,  
you could just write:

val = “red”

if val == “blue”  
puts “blue”  
elsif val == “red”  
abort “code red”  
else  
puts “not recognized”  
end

puts “program terminating normally”

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 30, 2009, 8:33am UTC](https://www.ruby-forum.com/t/can-this-code-be-more-succinct/166739/10 "2009-04-30T08:33:46Z")

</div>

7stud – wrote:

> …but then instead of having to look up the format of a case statement,  
> you could just write:
> 
> val = “red”
> 
> if val == “blue”  
> puts “blue”  
> elsif val == “red”  
> abort “code red”  
> else  
> puts “not recognized”  
> end
> 
> puts “program terminating normally”

…but then it is easier to type “when” than  
“elseifif”. 🙂
