Skip to main content

question regarding the good practice guidelines

hi, 
 
the guidelines found in the help tab of IDEA outlines the following guideline (among many): 

  • Do not hard code your macro from scratch. When possible, have IDEA generate a macro for you by recording and then manually edit the macro to replace hard-coded constants such as file names, equations, and other variables. 

I am relatively new to scripting and this leaves me wondering whether to follow my gut and refactor the code or to follow this guideline.The script i am currently dealing with is rather lengthy, but its basic task is separating a big databased, based on a set of eqautions, into a set of smaller databases
 
<code>
Sub Main
    Call Nr01   
    Call Nr02
    Call Nr03
    '.... some more function calls 
 
    Client.RefreshFileExplorer
    Client.CloseAll
    
End Sub
 
Function Nr01
    Set db = Client.OpenDatabase("Hovedbok.IMD")
    Set task = db.TableManagement
    Set field = db.TableDef.NewField
    field.Name = "MVA"
    field.Description = ""
    field.Type = WI_NUM_FIELD
    field.Equation = ""
    field.Decimals = 0
    task.ReplaceField "MVA", field
    task.PerformTask
    Set task = Nothing
    Set db = Nothing
    Set field = Nothing
End Function
 
 
'beregner momskomp beløp
Function Nr02
    Set db = Client.OpenDatabase("Hovedbok.IMD")
    Set task = db.TableManagement
    Set field = db.TableDef.NewField
    field.Name = "BEREGNET_MVA_KOMP"
    field.Description = ""
    field.Type = WI_VIRT_NUM
    field.Equation = "@If( MVA = 51.OR. MVA =61.OR. MVA =71; BELØP  *0,25;@If (MVA = 52 .OR. MVA  =62;BELØP *0,15; @If(MVA  = 53.OR. MVA  = 63;BELØP *0,12; @If(MVA  = 44 ;BELØP *0,06;0))))"
    field.Decimals = 2
    task.AppendField field
    task.PerformTask
    Set task = Nothing
    Set db = Nothing
    Set field = Nothing
End Function
 
'beregner ordinær moms
Function Nr03
    Set db = Client.OpenDatabase("Hovedbok.IMD")
    Set task = db.TableManagement
    Set field = db.TableDef.NewField
    field.Name = "BEREGNET_ORDINR_MVA"
    field.Description = ""
    field.Type = WI_VIRT_NUM
    field.Equation = "(@If (MVA = 15 ;BELØP *0,15;(@If(MVA =  2.OR. MVA =   2 ;BELØP*0,12;@If( MVA = 5.OR. MVA =  7;BELØP*0,25;0)))))"
    field.Decimals = 2
    task.AppendField field
    task.PerformTask
    Set task = Nothing
    Set db = Nothing
    Set field = Nothing
</code>
 
As you can see it is rather verbose, and as it is all mostly autogenerated by the 'record macro' functionality in IDEA. It is also repeating itself, where it opens up the same database several times and assignes it to a varible, and then reassigns it to nothing. As far as I am aware this is not in compliance with the DRY principle (do not repeat yourself). So i am wondering whether to follow the guideline as closely as possible or in this instance it would be better to rewrirte the code for easier readability and shorter code. 
I also wonder if the way IDEA autogenerates the code is a standard, you would recommend to follow. What i am specifically talking about is this pattern: 
<code>
Set db = Client.OpenDatabase("databasename")
Set task = db.SomeTask
'some more code ...
Set db = Nothing
Set task = Nothing
</code>
 
My problem with this pattern is that I find it harder to read than excplicitly stating the full object and method for the task and db as there might be a lot of code between when it is firtst assigned to a variable and until it is used. But as i am new to scripting i really cannot tell if breaking this standard set by the 'record macro' functionality would be a bad idea. 

Brian Element Tue, 07/05/2022 - 08:30

Hi Huslatun, welcome to the site.

You have to remember that these instructions are really for auditors who are not programmers and would have difficulty doing the refactoring, so it is easy to leave it as is and just modify what you need to modify instead of making the code more efficient.  If you have the coding experience to do what you want then there is no problem with you doing it, it won't cause any problems with the code.

huslatun Wed, 07/06/2022 - 04:04

In reply to by Brian Element

Hi Brian, 
Thank you for your answer. Seems like my formatting in the last post didn't really work as i intended (the code tags just sitting there as plain text), but i hope it was readable nonetheless.

The website encountered an unexpected error. Try again later.