Question: why use static for Objects?

Other than saving few bytes of memory, why would one use ::static for methods of Object?

I found that if I use ::static, then I have to add prefix _self:- each time an object data member is referenced anywhere, making the code very hard to read.

Imagine having to write  _self:-x + 3* _self:-x^2 + 2* _self:-y^3 and so on all the time, instead of just x+3*x^2+2*y^3. (where it is assumed here that x,y happened to be object private data members and not local variables for a proc inside the module).

But then I found if I remove  ::static now _self:- no longer needed and can still get the benefit of using the object and the code works as before, but the code is now much more readable. 

I know that by not using static, then a copy of each method is made for each new object.

I am OK with that. As I do not use that many objects any way (few at a time before GC cleans the ones I used).

But the benefit of much more readable code far outweights the little extra memory needed, and memory is cheap these days anyway. I got lots of RAM. An extra few MB's is not a big deal.

What Am I missing here? Why does all the Maple help and documenation say that one should use static for object methods then? But do not mention that by not using static:: then the code will become more readable since _self:- is not needed to be appeneded to each variable or method name.

Here is an example below to compare. 

First example uses ::static methods, and the second does not.

One can see the difference The code is more clear in the second.   Is there something else I am overlooking by not using ::static . I am still learning OOP in Maple, and could be overlooking something else. I definitly do not want to code using _self:-variable_name all the time if I have to use OOP in Maple as it makes the code hard to read. 

Notice that in both examples, and for the exported methods, I used _self as first argument. This is OK. This is meant to allow client of the object to call it using object:-method() syntax which is what I prefer instead of method(object,....). syntax.

I am talking about the execssive use of _self internal to the module/object code when having to use ::static. methods.

restart;

person_class_STATIC:=module()
   option object;
   local age:=5;   

   export set_age::static:=proc(_self,age,$)      
     _self:-age:=age:
   end proc:      

   export update_age::static:=proc(_self,age,$)      
      do_the_update(_self)
   end proc:      

   local do_the_update::static:=proc(_self,$)
      _self:-age:=_self:-age+1;
      _self:-age:=sqrt(_self:-age^2+3);
   end proc;

   export get_age::static:=proc(_self,$)      
     return _self:-age;
   end proc:      

end module:

o:=Object(person_class_STATIC);
o:-set_age(100);
o:-get_age();
o:-update_age();

o2:=Object(person_class_STATIC);
o2:-get_age();

_m1982588380672

100

100

2*2551^(1/2)

_m1982698669216

5

person_class_NO_STATIC:=module()
   option object;
   local age:=5;   

   export set_age:=proc(_self,_age,$)      
     age:=_age:
   end proc:      

   export update_age:=proc(_self,$)      
      do_the_update()
   end proc:      

   local do_the_update:=proc()
      age:=age+1;
      age:=sqrt(age^2+3);
   end proc;

   export get_age:=proc(_self,$)      
     return age;
   end proc:      

end module:

o:=Object(person_class_NO_STATIC);
o:-set_age(100);
o:-get_age();
o:-update_age();

o2:=Object(person_class_NO_STATIC);
o2:-get_age();

_m1982698652256

100

100

2*2551^(1/2)

_m1982698629312

5

 

Download OOP.mw

Please Wait...