Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > programming best practices

Reply
Thread Tools

programming best practices

 
 
swille
Guest
Posts: n/a
 
      11-07-2005
I have a couple of standard programming questions. The first is that
I often like to return true or false in a method when no other return
value is really needed. Is that bad form? I was talking to someone
the other day who said said something that made me think that maybe it
was. For example:

def login()
rc =3D false
begin
perform action
rescue
rc =3D false
some error
end

return rc
end

I like that because it reads well when you use it

if login
do_stuff
else
complain
end

My second question is this. I occasionally see stuff like
if -1 =3D=3D result

rather than
if result =3D=3D -1

What, if any, is the benefit of one over the other?

Thanks


 
Reply With Quote
 
 
 
 
jwheeler1@gmail.com
Guest
Posts: n/a
 
      11-07-2005
>> Is that bad form?

I do the same thing. Not taking return values into account could have
adverse effects on clients when you release a new version of your API.

>> What, if any, is the benefit of one over the other?

no benefit, just stylistic preference.

 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      11-07-2005
swille wrote:
> I have a couple of standard programming questions. The first is that
> I often like to return true or false in a method when no other return
> value is really needed. Is that bad form? I was talking to someone
> the other day who said said something that made me think that maybe it
> was. For example:
>
> def login()
> rc = false
> begin
> perform action
> rescue
> rc = false
> some error
> end
>
> return rc
> end
>
> I like that because it reads well when you use it
>
> if login
> do_stuff
> else
> complain
> end


Whenever it makes sense, do it. If you do not have a use case
like above there is no point in returning a value here it would
seem to be fine. Because in Ruby we can use '?' as a method suffix,
there is really no source of confusion whether you are testing if
someone has logged in (you would use 'login?' for that). In Java,
I imagine, you would use something like 'try_login' instead to
convey the idea of an attempt to do something and a subsequent
test.

Then again, I may be biased. I just tried to suggest Kernel.puts
return true

> My second question is this. I occasionally see stuff like
> if -1 == result
>
> rather than
> if result == -1
>
> What, if any, is the benefit of one over the other?


I think this habit is from C and so on; if instead of typing
'if result == -1' you typed 'if result = -1', a possible error
condition would ensue. Many languages and compilers do not issue
a warning about this (lack of a) conditional.

However, when you write 'if -1 == result' there is no possibility
of confusion because the interpreter/compiler would immediately
complain when seeing 'if -1 = result' (can not assign to a literal).

> Thanks



E



 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      11-07-2005
Hi --

On Mon, 7 Nov 2005, swille wrote:

> I have a couple of standard programming questions. The first is that
> I often like to return true or false in a method when no other return
> value is really needed. Is that bad form? I was talking to someone
> the other day who said said something that made me think that maybe it
> was. For example:
>
> def login()
> rc = false
> begin
> perform action
> rescue
> rc = false
> some error
> end
>
> return rc
> end
>
> I like that because it reads well when you use it
>
> if login
> do_stuff
> else
> complain
> end


But the way you've written it, rc is always false. Did you mean true
in the first line?

Also, it seems a little bit like you've got two error-reporting
systems going on. Maybe you could combine them:

def login
perform action
end

begin
login
rescue
...
end

etc.

> My second question is this. I occasionally see stuff like
> if -1 == result
>
> rather than
> if result == -1
>
> What, if any, is the benefit of one over the other?


As another answerer said, the first one (if -1 == result) protects you
from accidentally writing = instead of ==. In Ruby it's less of an
issue than in other languages because you'll get a warning:

if a = 1; end
(irb):1: warning: found = in conditional, should be ==

It's considered worthy of warning because there would never be any
"if" about a case like this: 1 is true, so the expression "a = 1" is
true.

The cool thing about this is that you *don't* get the warning when you
might actually want to do it:

b = 1
if a = b # no warning


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Lyndon Samson
Guest
Posts: n/a
 
      11-07-2005
On 11/7/05, swille <(E-Mail Removed)> wrote:
> I have a couple of standard programming questions. The first is that
> I often like to return true or false in a method when no other return
> value is really needed. Is that bad form? I was talking to someone
> the other day who said said something that made me think that maybe it
> was. For example:
>
> def login()
> rc =3D false
> begin
> perform action
> rescue
> rc =3D false
> some error
> end
>
> return rc
> end
>
> I like that because it reads well when you use it
>
> if login
> do_stuff
> else
> complain
> end
>


Ruby doesnt have procedures, so every method call returns a value. Its
just nil by default.

Also, error conditions are usually best signalled with exceptions,
which sort of make checking return values redundant.




> My second question is this. I occasionally see stuff like
> if -1 =3D=3D result
>

Poor programmers do this to avoid accidental assignment

> rather than
> if result =3D=3D -1
>
> What, if any, is the benefit of one over the other?
>



> Thanks
>
>



--
Into RFID? www.rfidnewsupdate.com Simple, fast, news.


 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      11-07-2005
swille <(E-Mail Removed)> writes:

> [ ... ]
>
> My second question is this. I occasionally see stuff like
> if -1 == result
>
> rather than
> if result == -1
>
> What, if any, is the benefit of one over the other?
>
> Thanks


Well, the traditional reason is because by putting the number to the
left of the comparison, you're protected against the case where you
might accidentally type a single equal sign instead of a double:

#!/usr/bin/ruby -W0
result = 0
if result = 1 # programmer meant to type "result == 1"
puts "one"
else
puts "not one"
end

This will print "one", even though "not one" is what is expected.

However, if you do the following, the compiler will give you an
error:

#!/usr/bin/ruby -W0
result = 0
if 1 = result # programmer meant to type "1 == result"
puts "one"
else
puts "not one"
end

The first case can lead to subtle, very hard to find bugs. The second
case won't even compile, so you know right away that there's a problem.

Of course, without -W0, you will get a warning in the first case, which
helps you see the potential problem. Nonetheless, many people have
gotten in the habit of using this little trick, just to be on the safe
side.


--
Lloyd Zusman
(E-Mail Removed)
God bless you.



 
Reply With Quote
 
swille
Guest
Posts: n/a
 
      11-07-2005
>
> But the way you've written it, rc is always false. Did you mean true
> in the first line?
>
> Also, it seems a little bit like you've got two error-reporting
> systems going on. Maybe you could combine them:
>
> def login
> perform action
> end
>
> begin
> login
> rescue
> ...
> end
>
> etc.
>


Ah, yes, that was a typo. My example possibly wasn't the greatest...
I couldn't think of exactly when I had used it last in Ruby. It's
actually something I've been using in Java quite a bit because I've
been writing interfaces into JavaScript and there's no way (in
JavaScript) to handle the exceptions from Java, so I use a boolean
return value. I decided at some point that I really like the
aesthetics of it. After a colleague questioned that behavior in
another context though, I thought twice about whether it was a good
idea. Basically, I suggested he use a boolean return value to a
method and he said something to the effect that it would be more
proper to return int-based return codes because "some_method true?"
didn't sound right. I wondered if maybe I was going to regret not
going to college :O)


 
Reply With Quote
 
zdennis
Guest
Posts: n/a
 
      11-07-2005
swille wrote:
> I have a couple of standard programming questions. The first is that
> I often like to return true or false in a method when no other return
> value is really needed. Is that bad form? I was talking to someone
> the other day who said said something that made me think that maybe it
> was. For example:
>
> def login()
> rc = false
> begin
> perform action
> rescue
> rc = false
> some error
> end
>
> return rc
> end


Not really pertinent to your question, but another way to do your example code is to utilize the
rescue clause at the method level which gets rid of the need for a temporary variable and makes your
method shorter, which IMO can go towards better readability.

def login
perform action
true
rescue
false
end

Zach


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-07-2005
swille wrote:
>> But the way you've written it, rc is always false. Did you mean true
>> in the first line?
>>
>> Also, it seems a little bit like you've got two error-reporting
>> systems going on. Maybe you could combine them:
>>
>> def login
>> perform action
>> end
>>
>> begin
>> login
>> rescue
>> ...
>> end
>>
>> etc.
>>

>
> Ah, yes, that was a typo. My example possibly wasn't the greatest...
> I couldn't think of exactly when I had used it last in Ruby. It's
> actually something I've been using in Java quite a bit because I've
> been writing interfaces into JavaScript and there's no way (in
> JavaScript) to handle the exceptions from Java, so I use a boolean
> return value. I decided at some point that I really like the
> aesthetics of it. After a colleague questioned that behavior in
> another context though, I thought twice about whether it was a good
> idea. Basically, I suggested he use a boolean return value to a
> method and he said something to the effect that it would be more
> proper to return int-based return codes because "some_method true?"
> didn't sound right. I wondered if maybe I was going to regret not
> going to college :O)


<soapbox>
IMHO using int's as return values is as bad as using boolean return values
in a language that has exceptions. Exceptions are a far more elegant way
of handling errors and also often code will be shorter and cleaner. One
of the reasons for this is that you can throw an exception somewhere deep
down in an application or lib and catch it several layers above - at the
level where it makes most sense. If you want to do that with boolean /
int return values you'll have checking and returning code in all
intermediate layers - this is bloated, ugly and unflexible.
</soapbox>

Kind regards

robert

 
Reply With Quote
 
Lyndon Samson
Guest
Posts: n/a
 
      11-07-2005
On 11/7/05, Robert Klemme <(E-Mail Removed)> wrote:
> swille wrote:
> >> But the way you've written it, rc is always false. Did you mean true
> >> in the first line?
> >>
> >> Also, it seems a little bit like you've got two error-reporting
> >> systems going on. Maybe you could combine them:
> >>
> >> def login
> >> perform action
> >> end
> >>
> >> begin
> >> login
> >> rescue
> >> ...
> >> end
> >>
> >> etc.
> >>

> >
> > Ah, yes, that was a typo. My example possibly wasn't the greatest...
> > I couldn't think of exactly when I had used it last in Ruby. It's
> > actually something I've been using in Java quite a bit because I've
> > been writing interfaces into JavaScript and there's no way (in
> > JavaScript) to handle the exceptions from Java, so I use a boolean
> > return value. I decided at some point that I really like the
> > aesthetics of it. After a colleague questioned that behavior in
> > another context though, I thought twice about whether it was a good
> > idea. Basically, I suggested he use a boolean return value to a
> > method and he said something to the effect that it would be more
> > proper to return int-based return codes because "some_method true?"
> > didn't sound right. I wondered if maybe I was going to regret not
> > going to college :O)

>
> <soapbox>
> IMHO using int's as return values is as bad as using boolean return value=

s
> in a language that has exceptions. Exceptions are a far more elegant way
> of handling errors and also often code will be shorter and cleaner. One
> of the reasons for this is that you can throw an exception somewhere deep
> down in an application or lib and catch it several layers above - at the


Well if you were really against Exceptions you could allways use
continuations to do this more elegantly than nested returns.


> level where it makes most sense. If you want to do that with boolean /
> int return values you'll have checking and returning code in all
> intermediate layers - this is bloated, ugly and unflexible.
> </soapbox>
>
> Kind regards
>
> robert
>
>
>



--
Into RFID? www.rfidnewsupdate.com Simple, fast, news.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Programming Best Practices clusardi2k@aol.com Java 16 07-23-2012 01:34 AM
Best practices for Ruby Meta-programming harshal Ruby 9 03-11-2008 05:11 PM
Best Codeplex sample for showing best coding practices? John Dalberg ASP .Net 3 11-16-2006 12:07 PM
Survey - Common Programming bugs and Bad practices vipindeep Java 1 10-22-2004 12:25 AM
Best sample app for learning best practices, OO & asp.net? karim ASP .Net 0 07-13-2003 04:26 AM



Advertisments