Case sensitivity and method arguments

  1. 2 weeks ago

    Garry P

    Jan 13 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    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. :)

  2. Kem T

    Jan 13 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Answer Connecticut

    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. :)

  3. Gavin S

    Jan 13 Pre-Release Testers, Xojo Pro, Forum Moderators, MVP UK

    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.

  4. Kem T

    Jan 13 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    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.

  5. Norman P

    Jan 13 Pre-Release Testers, Xojo Pro outside

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

  6. Markus R

    Jan 13 Pre-Release Testers, Xojo Pro Europe / Germany
    Edited 2 weeks ago

    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.

  7. Bob K

    Jan 13 Pre-Release Testers, Xojo Pro, Third Party Store Kansas City

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

  8. Markus R

    Jan 13 Pre-Release Testers, Xojo Pro Europe / Germany

    @Bob K The correct usage is the one you use every single time. :)

    the debate starts if you work in a team :)

  9. Bob K

    Jan 13 Pre-Release Testers, Xojo Pro, Third Party Store Kansas City

    @Markus R the debate starts if you work in a team

    Sometimes it's nice to be the boss.

  10. Garry P

    Jan 13 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    Thanks for the clarification Kem.

  11. Tim H

    Jan 13 Pre-Release Testers Portland, OR USA

    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."

  12. Norman P

    Jan 13 Pre-Release Testers, Xojo Pro outside

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

or Sign Up to reply!