From: Eero Saynatkari Date: 2005-11-07T12:02:50+09:00 Subject: Re: programming best practices 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