Saturday, March 5, 2022

The Never Ending Execution and Code Smell

 

I write this post to share my learning with all Test Engineers and not just be confined to Testers in a group.  I read the below question in the Telegram group - Testing Mini Bytes.  

A fellow Test Engineer in the group asked this question by sharing the method where he experienced a problem.

Question:

This is going on infinite loop can someone let me know why the loop the condition is not working



Pic: The method whose execution is not ending


Here is how I approached analyzing the stated problem and asisted the fellow engineer:

  1. Read and understand the problem description
  2. Understand whose problem it is and why from the description if the detail is available
  3. Know the Programming Language used
  4. Read the code and understand its flow
  5. Looked for the debug statements
  6. What is declared and how, why, and what does it hold?
  7. Check if any state exists and what it is
  8. Logic check for condition and flow
  9. Check on maxAttempt count
  10. Syntax check for condition and flow

Now, I look at how the condition check is being done:
  1. The operator used in condition with the operands
  2. Declaration
  3. Assignment
  4. Updating
  5. Referring

In this case, the condition has IF and ELSE blocks.  The IF block has a check on the status and maxAttempt count.  The IF condition has logical AND between these two.  And, the ELSE block has a check on maxAttempt count alone.

What I infer is:
  1. The operator != should not be a problem in this case
  2. Though JS also has !== operator, != should work in this context; !== also works if used
  3. Could be the status is not yet COMPETED or COMPLETED
  4. But, the maxAttempt will not be less than 5 in the first check
  5. maxAttempt is incremented
  6. And, the same having this IF and ELSE block is called
    1. Doing so, the maxAttempt will be reset to zero
    2. Note that maxAttempt was incremented before resetting it to zero
    3. In this method, maxAttempt will always be zero that is less than 5
  7. Irrespective of whether the status is COMPLETED or not, this execution will run into a loop


Not having the debug prints or console log for status and maxAttempt, it won't be obvious why this execution is going on & not stopping.  It is a kind of recursion experienced that is not intended to be here.  Unless short of system resources, this recursion continues.

Though I see the condition check logic can be written in other approaches, I did not get into how it can be done.


Learnings

  • Use the debug statements or console logs; it helps in debugging and understanding what's happening
  • Code Smells
    • Multiple Point of Failure
      • The place where the maxAttempt is declared and initialized
      • How the method is being called within it
    • Long Method
      • Though the method is simple in what it does, it involves multiple operations
      • Also, the method calls itself within it
        • It complicates by calling itself; breaking this method is good
        • One method one task
The method looks simple but how it flows within by resetting maxAttempt to zero.  And, a recursion kind of nature makes it uneasy to follow when there are no debug statements.




No comments:

Post a Comment

Please, do write your comment on the read information. Thank you.