idiom: if obj = get_client()

22 vues
Accéder directement au premier message non lu

Jedrin

non lue,
4 août 2016, 13:25:4404/08/2016
à Ruby on Rails: Talk

 Someone once tried to convince me that something like the following is bad form:

 if obj = get_client
  ..
end

 Not sure why it matters that much or if it should always be avoided .. any thoughts ?

This used to be very typical in C


Dave Aronson

non lue,
4 août 2016, 13:56:1604/08/2016
à rubyonrails-talk
Yes, and it was considered bad form there too. In both cases, it
leads one to wonder, did the author *mean* to assign, or just forget
the extra = in ==? If the code structure is such that you pretty much
HAVE to do that, then put a comment to that effect. I've also seen
many people slap parens around the assignment, i.e., "if (obj =
get_client)", in *both* languages -- that will shut up the warnings
some C compilers would otherwise (rightfully) generate.

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message