From: "David A. Black" Date: 2005-11-07T12:18:27+09:00 Subject: Re: programming best practices 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 dblack@wobblini.net