Forums

Var with _ or not

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Var with _ or not

Postby relez » Sun Oct 26, 2008 9:19 am

I'm trying to go deeper....
which is the real difference between

var _y as int

and

var x as int

why can i access to x only by using a point?

may be silly but... not for me.....
relez
 
Posts: 69

Re: Var with _ or not

Postby Charles » Sun Oct 26, 2008 2:29 pm

# In Cobra, member access requires a dot...
print obj.foo
assert obj.bar > 0
print .foo

# ...with the exception that underscored members of "this" can be accessed directly:
print _foo
# rather than:
print ._foo

Now here's the important part that goes with that:

Arguments and local variables can never start with an underscore and are never accessed via a dot. Consequently, when you look at someone's code, you can instantly tell what's a local vs. what's a member.

The exception of not having to put a dot in front of _foo is just for brevity.
Charles
 
Posts: 2513
Location: Los Angeles, CA

Re: Var with _ or not

Postby gauthier » Sun Oct 26, 2008 3:29 pm

Distinction of local variable to member is a great thing, enforcing it with language syntax is handy.

However, I'm a bit aversed with the underscore prefix because underscore is not easy to type on every keyboard layout (I mean not qwerty), on my layout it's on the 8 key which I often miss when I touch type.

The solution I use when using the underscore thing for members is to suffix rather than prefix, this allow me to just type the variable name and autocomplete with the editor, I don't have to recall to type underscore when the first thing that get to my mind is the member name (omitting the _)

Is it possible to support _ both as prefix and suffix or are you against that/is it too complex?
gauthier
 
Posts: 116

Re: Var with _ or not

Postby Charles » Mon Oct 27, 2008 4:15 am

That's interesting, what keyboard layout is that? I don't use Qwerty either; I'm on Dvorak which puts the underscore just to the right of my right pinky on the home row.

I've been using the trailing _ for args and local vars that conflict with a keyword. But those are just asides. In response to the suggestion:

Regarding the 8 key, people have to hit it all the time for multiplication (*) in a variety of programming languages (both on Qwerty and modern Dvorak). No one has told me before this that they have trouble hitting Shift-8 which makes me think this is very specific to your situation. Is it something physical like the key is bad? Or the physical layout is weird?

Regarding prefix, you'll have to consider it in Cobra anyway because you have to choose between _foo or .foo

In other words, putting the _ after only rids you of contemplating the prefix a portion of the time. Increasingly in my code, I've been using .foo and only accessing _foo on ever rarer occasions. Even if you forgo properties entirely, a leading dot is still needed for methods.

Sorry, but I'm against adding foo_ as an alternative to the current _foo. Cobra will remain with the rule:
Self member access starts with . or _
Charles
 
Posts: 2513
Location: Los Angeles, CA

Re: Var with _ or not

Postby gauthier » Mon Oct 27, 2008 1:35 pm

Thanks for the feedback, I'm using french layout

No one has told me before this that they have trouble hitting Shift-8


tracking the shift leave time to adjust the other hand, with french layout it's straight 8 key without shift required, and I'm also left-handed which imply I'm somewhat less precise when finding 8 (I hit 7 or 9 frequently), I just need to practice (well, using the same keyboard for years now, so I guess it's here to stay)

Regarding prefix, you'll have to consider it in Cobra anyway because you have to choose between _foo or .foo


I will use the dot but require me to put "is protected" after the declaration to match the underscore trick

Cobra will remain with the rule: Self member access starts with . or _


That's ok, thanks for considering the option.
gauthier
 
Posts: 116

Re: Var with _ or not

Postby relez » Mon Oct 27, 2008 2:56 pm

Chuck wrote:
# In Cobra, member access requires a dot...
print obj.foo
assert obj.bar > 0
print .foo

# ...with the exception that underscored members of "this" can be accessed directly:
print _foo
# rather than:
print ._foo

Now here's the important part that goes with that:

Arguments and local variables can never start with an underscore and are never accessed via a dot. Consequently, when you look at someone's code, you can instantly tell what's a local vs. what's a member.

The exception of not having to put a dot in front of _foo is just for brevity.


Ok, now it's ok for me too... i hope ;)
Thanks!
relez
 
Posts: 69

Re: Var with _ or not

Postby Charles » Mon Oct 27, 2008 7:51 pm

gauthier wrote:I will use the dot but require me to put "is protected" after the declaration to match the underscore trick


I hope to remedy that soon with:
class MyClass

protected
var foo_
var bar_
var baz_

def init
pass

def foo
pass
Charles
 
Posts: 2513
Location: Los Angeles, CA

Re: Var with _ or not

Postby relez » Thu Oct 30, 2008 4:16 pm

Sorry but i'm still in a little trouble :(

Code: Select all
class Punto
   var _x as int is public
   var x as int is public

class Program
   def main is shared
      p = Punto()
      p._x = 3
      print p._x
      p.x = 4
      print p.


Which is the difference between _x and x? They have the same behaviour....
relez
 
Posts: 69

Re: Var with _ or not

Postby Charles » Thu Oct 30, 2008 4:50 pm

(1) By default, "var _foo" is protected and "var foo" is public.

(2) When accessing the members (whether vars, methods, etc.) of the current object, you don't need the dot for underscored members:
class X
var _i as int
def print
print _i


Furthermore, the typical style is to declare vars with underscores, leave them protected and wrap them in properties--often read only properties, but depends on your needs.
class Player

var _score as int

get score from var

def incScore(change as int)
_score += change

class GameEngine

def tallyScore
.currentPlayer.incScore(.lastKill)
print .currentPlayer.score
Charles
 
Posts: 2513
Location: Los Angeles, CA

Re: Var with _ or not

Postby relez » Thu Oct 30, 2008 4:52 pm

Ok, now i'm fine. :)
Thx, Chuck.
relez
 
Posts: 69


Return to Discussion

Who is online

Users browsing this forum: No registered users and 11 guests

cron