Case sensitivity and method arguments

Let us say I have a class (Class1). It has a single property (X). It has one method - SetX():

Sub SetX(x As Integer) Self.X = x End Sub

Given this code:

Var c As new Class1 c.X = 10 c.SetX(20)

The value of c.X after the call to SetX() is 20 which is what I expect.

My question is, how “safe” is this? The compiler seems able to figure out that I want to use the lowercase variable x to assign to the instance property X. I’m assuming that because it is shadowing the instance property and the direct call to Self.X explains to the compiler my intention. Is this a correct assumption?

Before you all shout at me that this is code smell - I know it is. I am porting a huge Java library (which is case-insensitive) and I’m finding it less error prone to port by preserving the variable names rather than changing them.

You’ve specified the scope via the Self keyword, so this is safe. Without it, the local variable x would have priority.

I do this intentionally sometimes, create a local variable with the same name as a property:

dim x as Something = self.X

This protects my property from inadvertent changes later in the code.

Case-sensitive code is evil, btw. :slight_smile:

Just to add to Kem’s reply, I capitalise my properties (and I make it explicit by referring to them with Self) and I make my variables camel case. But this is just for me, Xojo doesn’t care about case sensitivity in this way.

As convention, I initial-cap my properties and initial-lower my local variables, and don’t use Self unless I need or want to be explicit.

MyName <-- property
myName <-- local

Everyone has their own style though.

kComeConstant <- constant
mSomePropertyName <- property
someLocalVarName <- local var

the “correct” use you find here
docs.xojo.com/UserGuide:Coding_Guidelines

often i skip the use of k and m because it looks ugly or i dislike it at autocomplete.

The correct usage is the one you use every single time. :slight_smile:

the debate starts if you work in a team :slight_smile:

Sometimes it’s nice to be the boss.

Thanks for the clarification Kem.

One of the best compliments I ever got was when my boss told me, “When looking at the code, I can’t tell whether I wrote it or you did.”

that could be good or bad :slight_smile:
good - boss writes great code
bad - boss writes spaghetti