New language feature: undeclare operator

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 5.0
    • Component/s: specification
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      This operator will remove the operand from the scope - any subsequent uses of the operand will be caught and flagged as errors by the compiler.

      Often when maintaining software people create alot of temporary variables that are only needed for a small section of code. However, they do not remove these temporary variables from the scope so they make the subsequent blocks of code alot more complex because the software maintainer must check to see if they are being used later or not.

      For example:

        int function_x(int b)
        {
          int x, y, z; // temporary variables

          x = 5*b;
          y = x*x;
          z = x*y;
        
          int final_answer = z*z + 5; // temporary variables x, y, and z are no longer
            // needed after this statement, but persist in the scope adding unnecessary
            // complexity...
          
          ...
            more code - maybe 1000 lines of code! In this code, final_answer is again
            modified...
          ...

          return(final_answer);
        }

      If later, this function must be changed, the person changing the function would have a much easier job if variables x, y, and z were destroyed when no longer needed. This would reduce the complexity of the subsequent code (by reducing the number of variables in the scope) and reduce the chance for errors should these temporary variables be used again inadvertently without clearing them first.

      The above block of code could be rewritten as follows:

        int function_x(int b)
        {
          int final_answer;

          {
            int x, y, z;

            x = 5*b;
            y = x*x;
            z = x*y;
          
            final_answer = z*z + 5;
          }
          
          ...
            more code - maybe 1000 lines of code!
          ...

          return(final_answer);
        }

      In this code rewrite, the local variables x, y, and z are removed from the scope (destroyed) soon after they are no longer needed. This simplifies the remainder of the code significantly. However, rewriting the code in this way was awkward. If there was an *undeclare* operator we could have written the code like this:

        int function_x(int b)
        {
          int x, y, z;

          x = 5*b;
          y = x*x;
          z = x*y;

          undeclare x;
          undeclare y;
        
          int final_answer = z*z + 5;
          
          undeclare z;

          ...
            more code - maybe 1000 lines of code! In this code, final_answer is again
            modified...
          ...

          return(final_answer);
        }

      Or, to make it more succinct, we code use ~ for undeclare:

        int function_x(int b)
        {
          int x, y, z;

          x = 5*b;
          y = x*x;
          z = x*y;

          ~x;
          ~y;
        
          int final_answer = z*z + 5;
          
          ~z;

          ...
            more code - maybe 1000 lines of code! In this code, final_answer is again
            modified...
          ...

          return(final_answer);
        }

      Now if x, y, or z are later used, the compiler will flag that as an error. Note the advantage of the undeclare operator over using the {} scope limiters: the variables x and y are killed IMMEDIATELY after their last use and then z is killed later. To do this with {} scope limiters we would have to write the function like this:

        int function_x(int b)
        {
          int final_answer;

          {
            int z;

            {
              int x = 5*b;
             
              int y = x*x;
             
              z = x*y;
            } // x and y are destroyed here.

            final_answer = z*z + 5;
          } // z is destroyed here.

          ...
            more code - maybe 1000 lines of code! In this code, final_answer is again
            modified...
          ...

          return(final_answer);
        }

      This requires more planning and thinking and more code than if there were an undeclare operator.

      This proposal has hopefully demonstrated the benefit of an undeclare operator for removing local variables from the scope.

      JUSTIFICATION :
      Reduces code complexity.
      Helps with software maintenance.
      ###@###.### 11/2/04 00:39 GMT

            Assignee:
            Alex Buckley
            Reporter:
            Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: