Underscore variables?

BlitzMax Forums/BlitzMax Programming/Underscore variables?

BLaBZ(Posted 2010) [#1]
Is it just a naming convention or does creating variables beginning with an underscore do something?

Field _variable:int

As seen in this topic...

http://blitzbasic.com/Community/posts.php?topic=81626#922601


therevills(Posted 2010) [#2]
Its just a naming convention, normally for private fields. (I dont use it)


Czar Flavius(Posted 2010) [#3]
It lets you know that field isn't meant to be used outside that type's methods. If you ever do, you will put myobject._variable and the ._ should be a warning sign for you. Unlike other languages, Blitz has no real private protection unfortunately.

Private variables are useful to hide particular ways of doing things from other types and functions that don't need to know about it. Other things need to interact with the type through its methods only. For example, you could have a type that stores data using an internal array. You would set that array as a private variable, and use add, retrieve and remove methods for other things to use the array.

In these methods you could also put any extra checks or preperations that might be needed. For example, the data could be in two different formats (inches and meters?) and you convert them into just one format internally. Then you could have methods to get the data as either meters or inches, and convert if required.

What's the point of this? What if you store them as inches, but decide it would be better to store the data internally as meters? What if you want to replace the array with a map? If all the other types are accessing the array directly and performing their own conversions and checks, you have a lot of work to do. If you have hidden these things and only allow types to work with the data store through certain methods, you can change these internal workings without having to modifiy ANY other code outside that one type! No matter how many things use this type.

The power of data hiding is flexability. How often have you changed your mind about something half-way through?


Who was John Galt?(Posted 2010) [#4]
What Czar said. I think it's a convention Mark uses in some of the modules, and a bunch of us appropriated the idea.


Czar Flavius(Posted 2010) [#5]
If he was using private variables, why he didn't add it as a proper feature I don't know!

Seriously, I really hope it is added in a future update. Those who don't like it don't have to use it. Those who do like it, will :)