Proper use of private?

BlitzMax Forums/BlitzMax Programming/Proper use of private?

Tibit(Posted 2005) [#1]
When I try to use it thing that is private is private from stuff even in the same file.
Is the only use of private to allow a type to access certain methods (of itself) that other types or functions outside it can't access?

Have anyone used private/public in any useful way?


Scott Shaver(Posted 2005) [#2]
While I haven't used it yet in BMax I can tell you the best reason (IMO) to use it is this:

Make the fields in your type private and provide accessor methods for them like GetFoo() SetFoo(). The idea behind this is to hide the implementation. So for instance let's say you have a Taxes type and one of the fields is percent:Float, instead of doing

Local answer:Float = total * mytaxes.percent

you would do

Local answer:Float = total * mytaxes.GetPercent()

the upside of this is if for some reason you decide that percent needs to be a calculation instead of just a value you can change the GetPercent method and none of the rest of your code needs to be changed.


nawi(Posted 2005) [#3]
I dont like using GetPercent(), percent is faster and more clear.


Scott Shaver(Posted 2005) [#4]
Well it is of course a matter of personal preference when you are talking about hobby projects but IMO professional projects should really use it. As for it being slower/faster I wouldn't considered that an issue if I were selling a library for instance, I would gladly take any performance penalty to avoid hosing my users when I had to change something.

What would be nice is if the language supported a Property type field. The compiler could automatically notice this and inline the method code for the getter/setter. Also it could be implemented just like C#. IIRC when you don't supply specific code for a Property field the getter/setter are created for you by the compiler automatically. Also you can set the field to read-only/write-only/read-write.


FlameDuck(Posted 2005) [#5]
Have anyone used private/public in any useful way?
Not in BlitzMAX. No.

Make the fields in your type private and provide accessor methods for them like GetFoo() SetFoo().
If only that where possible.

I dont like using GetPercent(), percent is faster and more clear.
But less thread-safe (aka. not at all).

I agree with Scott. More or less.