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
Here is how I approached analyzing the stated problem and asisted the fellow engineer:
- Read and understand the problem description
- Understand whose problem it is and why from the description if the detail is available
- Know the Programming Language used
- Read the code and understand its flow
- Looked for the debug statements
- What is declared and how, why, and what does it hold?
- Check if any state exists and what it is
- Logic check for condition and flow
- Check on maxAttempt count
- Syntax check for condition and flow
- The operator used in condition with the operands
- Declaration
- Assignment
- Updating
- Referring
- The operator != should not be a problem in this case
- Though JS also has !== operator, != should work in this context; !== also works if used
- Could be the status is not yet COMPETED or COMPLETED
- But, the maxAttempt will not be less than 5 in the first check
- maxAttempt is incremented
- And, the same having this IF and ELSE block is called
- Doing so, the maxAttempt will be reset to zero
- Note that maxAttempt was incremented before resetting it to zero
- In this method, maxAttempt will always be zero that is less than 5
- 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
No comments:
Post a Comment
Please, do write your comment on the read information. Thank you.