Results 1 to 5 of 5
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    Efficiency of code execution vs code readabliity?

    Hello All,

    I have a VBA program for producing Access Documentation which has a lot of statements like this:
    Code:
      
          With .ParagraphFormat.TabStops
                .ClearAll
                .Add Position:=oWordApp.InchesToPoints(2.25), _
                       Alignment:=0, Leader:=0 'wdAlignTabLeft wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(3.7), _
                       Alignment:=0, Leader:=0 'wdAlignTabLeft wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(5.5), _
                       Alignment:=2, Leader:=0 'wdAlignTabRight wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(6.4), _
                       Alignment:=2, Leader:=0 'wdAlignTabRight wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(7.4), _
                       Alignment:=2, Leader:=0 'wdAlignTabRight wdTabLeaderSpaces
    
            End With   '.ParagraphFormat.TabStops
    I find this makes the code rather hard to read. I was thinking of replacing those lines with a call to the following subroutine:
    Code:
    Option Explicit
    
    Sub FMTDoc()
    
    'Sample calling sequence
    
          SetTabStops Array(1.5, 0, 0, 3.5, 1, 0, 6.75, 2, 0)
      
    End Sub
    
    Sub SetTabStops(vTStops As Variant)
    
       Dim iCntr  As Integer
       Dim iLoop  As Integer
       Dim iExtra As Integer
       Dim zWds(1 To 2, 1 To 2)
       
       zWds(1, 1) = "was "
       zWds(1, 2) = "argument."
       zWds(2, 1) = "were "
       zWds(2, 2) = "arguments."
       
       iLoop = UBound(vTStops)
       iExtra = (iLoop + 1) Mod 3
       
       
       If iExtra <> 0 Then
         MsgBox "Passed array requires arguments in sets of 3" & vbCrLf & _
                "1 - Tab Position" & vbCrLf & _
                "2 - Tab Alignment" & vbCrLf & _
                "3 - Tab Leader" & vbCrLf & vbCrLf & _
                "There " & zWds(iExtra, 1) & Format(iExtra) & _
                " extra " & zWds(iExtra, 2) & vbCrLf & vbCrLf & _
                "Please Correct your calling statement.", _
                vbCritical & vbOKOnly, _
                "Error: Incorrect number of arguments."
         Exit Sub
       End If
       
       'With oWordApp.Selection.ParagraphFormat.Tabstops 'Word Object
       With ActiveDocument.Paragraphs.Format.TabStops    'In Word
        
           .ClearAll
           For iCntr = 0 To iLoop Step 3
              .Add Position:=InchesToPoints(vTStops(iCntr)), _
                   Alignment:=vTStops(iCntr + 1), _
                   Leader:=vTStops(iCntr + 2)
           Next iCntr
       End With   '.Format.Tabstops
       
    End Sub    'SetTabStops
    I'm sure making a bunch of calls is inefficient but I think the increase in readability of the code makes up for it. The subroutine will be called at max of about 50 times depending on selections made by the user at run time. So what do y'all think?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    I'm not sure I agree that the original code is any less readable and it could be made more so by declaring 'wdAlignTabLeft' and 'wdTabLeaderSpaces' as constants (with their corresponding values) so that you could continue to use 'wdAlignTabLeft' and 'wdTabLeaderSpaces' in the code. This also facilitiates the transition between early & late binding.

    For example:
    Code:
    Const wdAlignTabLeft As Long = 0, wdAlignTabCenter = 1, wdAlignTabRight = 2
    Const wdAlignTabDecimal = 3, wdAlignTabBar = 4, wdAlignTabList = 6
    Const wdTabLeaderSpaces As Long = 0, wdTabLeaderDots = 1, wdTabLeaderDashes = 2
    Const wdTabLeaderLines = 3, wdTabLeaderHeavy = 4, wdTabLeaderMiddleDot = 5
    
            With .ParagraphFormat.TabStops
                .ClearAll
                .Add Position:=oWordApp.InchesToPoints(2.25), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(3.7), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(5.5), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(6.4), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
                .Add Position:=oWordApp.InchesToPoints(7.4), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
            End With
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Paul,

    Thanks. I didn't think VBA would allow you to redeclare the constants in the object model.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Personally I would use the additional routine, though I'd probably also pass a tabstops object to it. I doubt there is much overhead to it and if it makes it easier for you to read, it's worth it.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    New Lounger
    Join Date
    Dec 2009
    Location
    Winnipeg, Manitoba, Canada
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post
    I would go for readability. Don't forget to add a comment to the Sub to document its purpose and usage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •