Any comments? Programming style, protocol for methods (2?) 
Author Message
 Any comments? Programming style, protocol for methods (2?)

        Sorry to bug you again - I sent this post, but nobody responded
        - I'm afraid that nobody received this.

        Am I wrong?

                Michal

-------------------------------------------------------------------------
Hello everybody,

        I'd like to ask you if you're going to (love|hate) this approach.

        What it is about?

Introduction:
        You have lib. in 'C' that have struct BN (BigNums) and some
functions for it in lib. There are funcs for BN *BN_new(),
hex2bn(BN *, char *), dec2bn(BN *, char *) and so on.
        I'd like to wrap this lib to Ruby, but I'd like to make 'smart' :-)

Solution 1 - Traditional:

        BN::new(*) - in 'C', WrapsStruct and BN_new()
        bn.initialize(str, type=DEC) - in 'C', handles all in code, complex

        (+) easy
        (+) fast
        (-) cannot be enhanced (enhanced only via aliasing old init and
        making new one (?))

Solution 2 - General (Smalltalk's (?))

        BN::new(*) - in 'C', WrapsStruct and BN_new()

        class BN
          def initialize(arg=nil, type=DEC)
            send("from_#{type.class.name.downcase}", arg, type)
          end
          def from_fixnum(arg, type=DEC)
            from_string(arg.to_s, type)
          end
          def from_string(arg, type=DEC)
            send("from_#{type.downcase}", arg)
          end
        end

        bn.from_dec(str) - in 'C' - stupid
        bn.from_hex(str) - in 'C' - stupid

        (-) complex (?) - breaks KISS (?) (- breaks POLS (???))
        (-) slower
        (+) very easy to add own 'from_...' method

So are you going to love it or 'Traditional' approach is better
from your point of view?

                Michal

PS: Better initialize like this:

def initialize(arg=nil, type="dec")
  return if arg.nil?
  t = arg.class
  while t
    t.name.downcase =~ /(\S*::)*(\S+)/ #Take class name (without modules)
    method = "from_#{$2}".intern
    return send(method, arg, type) if respond_to?(method, true) #including private
    t = t.superclass
  end
  raise "Don't how to init #{self.class.name} from #{arg.class}"
end

-------------------------------------------------------------------------

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Michal Rokos                           Czech Technical University, Prague

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



Sun, 04 Jul 2004 02:47:20 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Any comments? Programming style, protocol for methods

2. parser API style - methods take path or file vs sep methods for path and file

3. Static method and class method comments

4. Static method and class method comments

5. PROPOSAL: method binding protocol

6. Comments on style?

7. please provide your comments on coding style

8. LRM style comment

9. New LRM style comments

10. books on Fortran design, commenting, and style?

11. request comments on Prolog style

12. question about method and style

 

 
Powered by phpBB® Forum Software