User Tag List

Results 1 to 2 of 2
  1. #1
    Senior Member
    Join Date
    Feb 2008
    Post Thanks / Like
    0 Post(s)
    0 Thread(s)

    Rafactoring code and generic functions

    I refactor my code like crazy, once I have it working. I try to make my code as absolutely generic as possible, identifying frequently-used code blocks and refactoring them into generic functions. I have several of these functions that I call from multiple places within my script. The problem I noticed is that when a script runs and generates an error, the error frequently occurs in those generic functions, rather than at the source of the function call. So, I took to passing in a GUID with some "context" strings to my generic functions. I simply spit out the context and GUID in the log file, and do a quick "copy/find" in the code to find out where the error was generated. The GUID became necessary because some of my context strings are exactly the same from function to function, and I don't want to write a novella every time I'm catching an error condition.

    For example, I often have "while" loops running that wait for a specific condition to occur. Rather than make those loops specific to their context, I generalize them and pass the context *in* to the function. Like this:

    <font class="small">Code:</font><hr /><pre>
    while (&lt;my loop controller&gt
    WaitForExitCondition("Waiting for Master Method Template GUI to exist. {E7483356-130E-46f4-AC8A-94AD79F9DE22}");
    </pre><hr />

    The actual WaitForExitCondition function looks like this:

    <font class="small">Code:</font><hr /><pre>
    function WaitForExitCondition(Context)
    LoopTimeLimit = LoopTimeLimit + 500;
    if (LoopTimeLimit &gt; MaxLoopIterationTime)
    </pre><hr />

    That's a function that I use to simply poll for a specific event every half-second. So, whenever I want to just sit and poll for something every 500 milliseconds, I just use that generic function. The problem happens if my event never occurs. I need an elegant way to break out of the loop, so I use the above. However, this doesn't tell me *which* of my while loops was a problem (there are numerous places where I am "Waiting for Master Method Template GUI to exist..."). So I pass in a short context and a GUID. I use the GUID to locate the specific while loop that timed out.

    I use the above in addition to a couple of script-scoped variables to control the entire thing. You can see I've even generalized the actual error logging [img]/images/graemlins/smile.gif[/img]

    The "ResetLoopTimeLimit()" function is pretty self-explanatory. It simply resets my script-scoped LoopTimeLimit variable so that I can easily use it again. I open nearly all of my functions with a call to that routine, so that I know the variable is reset when the function begins executing.

    - Nat

  2. #2
    Moderator tristaanogre's Avatar
    Join Date
    Mar 2006
    Pennsylvania, USA
    Post Thanks / Like
    0 Post(s)
    0 Thread(s)

    Re: Rafactoring code and generic functions

    You beat me to it, Nat. That's precisely how we do our log file names although we add one other parameter. We have multiple machines working in parallel. So, each project has a folder (going trom the Project.Variables.Name property) and each machine creates it's own file. Here's our routine in DelphiScript:

    <font class="small">Code:</font><hr /><pre>
    procedure ExportTestLog;
    Exports the current test log of a project to a shared drive directory. The file exported is
    the MIME HTML format of the test log and is named using the machine on which the
    project was run, the date the project completed, and the time the project completed


    if not DoCreateTestLog then Exit;
    lFileName := LOG_OUTPUT_DIR + Project.Variables.ProjectName + '\' + Sys.HostName + '_'+Utilities.FormatDateTime('yyyymmdd_hhnn',Utili ties.Now)+'.MHT';
    ProcessError('Could not export the test log because of the following error: ' + ExceptionMessage,FALSE);
    end;</pre><hr />
    -Robert Martin
    Automated Testing Evangelists
    TestComplete "expert"
    Definition expert - noun - Unknown drip under pressure



Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 11:21 PM.

Copyright BetaSoft Inc.