Subroutines

Pieces of code that are needed more than once should be turned into subroutines. This makes later corrections less risky and the code more structured.

Blocks of lengthy code should be split into subroutines to allow a better overview of the main logic.

The label of the subroutine should correspond with its function. Do not use numbers as names because it makes the code unreadable. An exception is subroutines mapped to an integer parameter, but make clear with a comment which case of which parameter the subroutine handles.

Style:

dict _point, _origin
_point  = cube.points[1]
_origin = cube.center
gosub "GetDistance" ! returns _distance

gosub iShape

! ==============================================================================
end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! end !
! ==============================================================================


! ==============================================================================
"GetDistance":
! ------------------------------------------------------------------------------
! Gets distance of point from origin by specified coordinates.
! input:
!   _origin        origin coordinates
!       .x .y
!   _point         coordinates of specified point
!       .x .y
! output:
!    _distance     distance of _point from _origin in 2D
! ==============================================================================

    __dx = (_origin.x - _point.x)
    __dy = (_origin.y - _point.y)
    _distance = sqr(__dx **2 + __dy **2)

return


! ==============================================================================
100:    ! iShape = SHAPE_CUBE
! ------------------------------------------------------------------------------
...
  • Write the body of the subroutine indented by one tabulator field to the right.
  • Leave two empty lines before and after the subroutine.
  • List all variables the subroutine uses and returns in the header comment. List dictionary-type variables with all relevant keys (unless listed and used the same way in a subroutine before).
  • Check all incoming parameters for validity and/or declare the restriction in the header comment.
  • When the number of transformations/dels, cuts/cutends, or puts/gets is not equal, declare it in the header comment.
  • Subroutines should not contain END / EXIT commands.