Announcement

Collapse
No announcement yet.

When to LOCAL

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • When to LOCAL

    Another thing I always wondered about ...

    In a procedure, I always put my Local statement at the top and include every variable I might use in the procedure. I like having them all in one place.

    Code:
    Sub Test
       Local x,y as Long
       x = 5
       y = 6
    End Sub
    I've seen other folks often put the Local statement just before the code where it is used, I assume because the write it as they go, or they like to have the declaration next to the code where the function is used.

    Code:
    Sub Test
       Local x as Long
       x = 5
    
       ... lines of separating code 
    
       Local y as Long
       y = 6
    End Sub
    Other than personal preference is there any technical reason to use distributed Local statements?

  • #2
    Other than personal preference is there any technical reason to use distributed Local statements?
    Not for PB, but e.g. in VB.NET...
    Code:
    If a = b Then
       Dim c As String
       [stuff...]
    Else
       Dim c As Int32
       [stuff...]
    End If
    
    If c = a Then ... ' results in compile error, "Undefined var 'c'"
    ... is perfectly valid, as a variable's scope is limited to the code block it's declared in.

    Typically I also put my declares at the top of the method, but there's ocasssions were this gets messy. Then I start to move the declarations close to wher the usage starts.

    Comment


    • #3
      Originally posted by Gary Beene View Post
      Another thing I always wondered about ...

      In a procedure, I always put my Local statement at the top and include every variable I might use in the procedure. I like having them all in one place.

      Other than personal preference is there any technical reason to use distributed Local statements?
      In some languages, as Knuth said, scope may be limited to blocks, so variables may be
      declared with the same name and yet be separate memory space and even disparate types.
      POWER Basic local is by main code block, so any declaration within the code block is the
      only declaration allowed. Having them all at the top of the block organizes the layout.
      Having them declared where they are used works just fine, but it distracts from the flow
      of the code when maintaining or examining. It can also lead to having duplicate declarations
      that cause compile errors, since you might not remember every declaration and miss a few
      when examining them if they are distributed according to use.
      Having them all at the top of the local scope block aids in organization and readability.
      It doesn't do anything for program functionality, but aids programming functionality.
      The world is strange and wonderful.*
      I reserve the right to be horrifically wrong.
      Please maintain a safe following distance.
      *wonderful sold separately.

      Comment


      • #4
        I will often put the declaration close to new code I am writing. That was I don't have to hit page up 17 times to get a reminder of what variable I'm using/have newly created. Once I get the first clean compile with the new code, the declarations go up to the top of the function/sub block.
        Real programmers use a magnetized needle and a steady hand

        Comment


        • #5
          I will often put the declaration close to new code I am writing. That was I don't have to hit page up 17 times to get a reminder of what variable I'm using/have newly created. Once I get the first clean compile with the new code, the declarations go up to the top of the function/sub block.
          This is exactly why I use an include file and put the $INCLUDE statement where the variables usually go. Having the $INCLUDE file in the next tab makes it quick and simple to see what's what. Bookmarks work as well, but one has a tendency to go well beyond the marker. Well, this one does!
          Rod
          "To every unsung hero in the universe
          To those who roam the skies and those who roam the earth
          To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

          Comment


          • #6
            Bob Zale once enthused about putting LOCAL declarations among the logic to save scrolling around the source code. I can't remember whether he meant just during debugging. I had already been programming for over three decades before I encountered PowerBASIC and the languages I used mostly insisted on declarations preceding code, so it was natural for me to declare first.

            Comment


            • #7
              Originally posted by Rodney Hicks View Post
              This is exactly why I use an include file and put the $INCLUDE statement where the variables usually go. Having the $INCLUDE file in the next tab makes it quick and simple to see what's what. Bookmarks work as well, but one has a tendency to go well beyond the marker. Well, this one does!
              That works fine for Globals, but as a general rule, I avoid them (unless you strictly enforce a rule of only changing a variable's value in one place, you can get into all sorts of problems and debugging becomes much more difficult). It's not practical to have a variables Include file for every sub/function and the vast majority of my variables are Local.

              Comment


              • #8
                That works fine for Globals,...
                It works fine for any kind of variables. $INCLUDEs can be positioned anywhere in code, provided that the code is appropriate for the position. It is also very much a user option. I too rarely use globals, but I often have horrendously long CALLBACK FUNCTIONs. I don't use it for supporting functions/subs as most of the variables in them are parameters that arrive there via a call from the CALLBACK and these functions/subs are quite short.
                Rod
                "To every unsung hero in the universe
                To those who roam the skies and those who roam the earth
                To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                Comment

                Working...
                X