נקודות יציאה מרובות
קל מאוד לשלוט בנקודה בה תסריט הבדיקה נכנס ל-Action / פונקציה – אחרי הכל, יש רק אופציה אחת – מלמעלה. שליטה בנקודות היציאה, זה כבר סיפור שונה לחלוטין - ל-Action יכולה להיות נקודת יציאה אחת, שתיים, או עשר, הכל בהתאם ללוגיקה הפנימית והמבנה שלה. היום נבחן איך ניתן לשלוט בנקודות יציאה מרובות ב-Actions ופונקציות, ולהפוך את הקוד שלנו לקל יותר לתחזוקה ותיקון.
כשהתסריט מתקדם כצפוי, זרימת הקוד פשוטה יחסית. גם כשיש בקוד בלולאות, תנאי IF ובחינת מקרים באמצעות Select Case, הקוד מתבצע בקו ישר יחסית, ומסתיים בשורה האחרונה בתסריט. במקרה כזה יש רק שער יציאה אחד, והכל טוב ויפה. אבל, במקרים בהם התסריט נתקבל בבעיות, כמו במקרה של שגיאת מערכת, או כשל בלוגיקה העסקית של האפליקציה, אין מנוס מיציאה מיידית מהקוד הנוכחי. אחרי הכל, אין שום טעם להמשיך נסות להזין 20 שדות, אם הטופס שמכיל אותם בכלל לא נפתח.
למזלנו, האנשים הנחמדים ב-Mercury (והיום HP) הבינו את הצורך ביציאה מיידית מ-Action, והכינו עבורנו את פקודות ExitAction ו-ExitActionIteration. לרוב השימוש בהן נראה כך:
'….Action Code
If CritialCondition = False Then ExitAction
'….Continue Action
אבל זאת דרך מאוד לא ברורה לצאת באמצע ביצוע משימה. רצוי שנוסיף כתיבה ללוג:
If CritialCondition = False Then
Reporter.ReportEvent MicFail, "Something bad has happened", "Aborting"
ExitAction
End If
בעצם, יש לנו גם כמה אובייקטים שצריך לנקות מהזכרון:
If CritialCondition = False Then
   Reporter.ReportEvent MicFail, "Something bad has happened", "Aborting"
   oFile.Close
   Set oFile = Nothing
   ExitAction
End If
ורגע, יש מידע שצריך לשתול ב-DataTable:
If CritialCondition = False Then
   Reporter.ReportEvent MicFail, "Something bad has happened", "Aborting"
   oFile.Close
   Set oFile = Nothing
   DataTable("out_EntityID", dtlocalsheet) = sEntityID
   'More here
   ExitAction
End If
אני מניח שתפסתם את הרעיון. במקום פקודה פשוטה, יש לנו קטע קוד מורכב למדי, שמופיע שוב ושוב בכל נקודות היציאה. המשמעות היא שיש לנו קוד כפול שנצטרך לתחזק באופן שוטף. קוד כפול הוא התגלמות זרע השטן בעולם הדיגיטאלי, ואתם ממש לא רוצים משהו כזה בקוד שלכם. באופן רציני יותר, כפילויות קוד כאלו הן מתכון בטוח לבאגים קשים לאיתור, עבודה מיותרת ותסכול, וצריך להמנע מהן בכל מחיר. אחת הדרכים להמנע מכפילויות הקוד היא לארוז אותו במרוכז בפונקציית יציאה. זוהי הפונקציה היחידה ש"מותר לה" (לפחות כפי שאני כותב קוד) להיות בתוך Action, ולא בקובץ חיצוני. להלן דוגמה לשימוש בפונקציה, כמודגם ב-Action Template שלי:
Dim sResult 'I store result data and values to be reported up the action-call chain
Dim sActionReport
'Instead of flooding the log with inner-action messages, I store them, and report all of them at the exit gate.
'Errors are still reported on-the-fly
'Action code goes here
'All the exit gates execute only one command : ActionEnd.
'It receives two parameters: Boolean for Pass/Fail, string for the exit reason
If CriticalCondition = False Then Call ActionEnd(False, "Reason for exiting")
'Rest of action
'Even the normal successful action exit is managed through ActionEnd, so the last line in every action is:
Call ActionEnd(True, "Action successful")
Sub ActionEnd(bStatus, sReason)
   'Report details
   Reporter.ReportEvent MicGeneral, "Inner Action Logs", sActionReport
   If bStatus = True Then
      Reporter.ReportEvent MicFail, "An error has occurred", sReason
   Else
      Reporter.ReportEvent MicPass, "Action successful", "See inner logs for details"
   End if
   'Plant datatable info for action-call chain
   DataTable("out_Status", dtlocalsheet) = bStatus
   DataTable("out_Result", dtlocalsheet) = sResult
   'More if needed
   'Close objects and set to nothing here
   'Other needed exit code
   ExitActionIteration
End Sub
כפי שראינו, שימוש בפונקציית יציאה מרכזית מאפשר לשמור על קוד נקי ופשוט, ולמנוע הצפות לוג. טיפלנו בנקודות יציאה מ-Action, אבל מה לגבי יציאה מפונקציה? כמובן שאי אפשר לעשות פונקציית יציאה לפונקציה, אבל ישנו פתרון אלטרנטיבי לבעיה. הפתרון פחות יפה וקשה יותר לתחזוקה מהפתרון ל-Action, ולכן אני ממליץ להשתמש בו רק בפונקציות מסובכות במיוחד, המכילות הרבה נקודות יציאה, וקוד יציאה "עבה". הפתרון מתבסס על פקודת Execute, ולכן אם היא לא מוכרת לכם, אני ממליץ לקרוא עליה בקבצי העזרה של ה-QTP. בשורה התחתונה, פקודת Execute מקבלת מחרוזת, ומבצעת את התוכן שלה כאילו שמדובר בפקודות VBScript. כך שהפקודה Execute "msgbox(2)" תקפיץ msgbox עם המספר 2. להלן דוגמה לשימוש בפתרון בפונקציה ComplexFunc:
Function ComplexFunc
   Dim sExitCode
   Dim sResult
   Dim oFile 'will be FSO textstream
   'separate code lines by vbcrlf or ":"
   sExitCode = "oFile.Close" &          vbcrlf & _
         "Set oFile = Nothing" & vbcrlf & _
         "ComplexFunc = sResult"
      'More exit code
   'Function code goes here
   'Exit Gate
   If CriticalCondition = False Then
      sResult = "False, No Connection"
      Execute sExitCode
      Exit Function
   End if
   'More function code
   'Successful exit
   Execute sExitCode
End Function
אין תגובות:
הוסף רשומת תגובה