From: Marek Janukowicz Date: 2003-06-03T02:07:24+09:00 Subject: Re: Advantages of Symbols over constants On Sun, 1 Jun 2003 22:51:41 +0900, Jim Freeze wrote: >> I wonder what are the advantages of using symbols over constants. For >> example: I want to define representation of SQL comparison operators. If >> I use constants - I have to define them, but I don't have to do explicit >> check when using them (because exception will be raised on non-existing >> constant). It is also easy to see which operators are defined when >> looking at the source code. Another advantage - constants have >> namespaces: they can be defined in the class scope. If I use symbols - I >> don't have to define them, but I need to verify whether the symbol is >> valid. I have also to write additional documentation to indicate valid >> Symbols for operators. >> >> I find kind of general tendency on this group to recommend using symbols >> rather then constants. From what I wrote above I'd rather use constants. >> Could you please explain me why should I use symbols? Am I missing some >> important symbols advantage? > One advantage is that symbols are faster than constants. I don't use constants too much, so speed is not a problem. > Regarding the namespace issue, it may be ok to ask yourself > if it really is needed. Yes, I think so (in this particular case, of course). I have Qualifier class representing "attribute operator value" and placing the operator constants in this class seems good to me (frmo the design point of view). > It may be that if an invalid symbol > is passed, then it just falls off the end of some if/else or case > statement and throws an error. > If, on the other hand you can't do without the name space, create > a hash with the valid symbols as the key, and use #has_key? > to validate the symbol. I're right, but like I wrote before: when using constants I don't have to do explicit validation - undefined constants will raise an exception. -- Marek Janukowicz