I’ve been using ChatGPT as a tool while programming and although it has been amazing in many respects I do struggle with it making “modifications” and “optimizations” to existing code that introduce bugs or suggests routines that clearly do not meet requirements.
Here is a simplified example that seems straight forward to me, but illustrates a core issue I have been having.
I have two potential states A and B that are independent and not exclusive. There are three tasks: 1, 2 and 3.
If A is true, do 1.
If B is true, do 2.
If neither A nor B is true, do 3.
Here is an example that implements this logic:
if A {
do(1)
}
if B {
do(2)
}
If !A && !B {
do(3)
}
Do see any simplification or optimizations that are functionally equivalent?
The consistent response I get back is like this:
Yes, there is a simpler way to express this logic:
if A {
do(1)
} else if B {
do(2)
} else {
do(3)
}
This implementation uses an if-else statement instead of three separate if statements. If A is true, it executes the first block and exits the if-else statement. If A is false, it checks if B is true, and if it is, it executes the second block and exits the if-else statement. If both A and B are false, it executes the third block. This implementation is functionally equivalent to the original implementation, but it is more concise and easier to read.
No, that is not functionally equivalent. It introduces an else in the conditions about A and B. If both A and B are true, my logic performs both tasks 1 and 2. What does your logic do in that case?
To which it responds:
You are correct, my previous suggestion would not be functionally equivalent to your original logic when both A and B are true.
To handle that case, we can use nested if statements as follows:
if A {
do(1)
if B {
do(2)
}
} else if B {
do(2)
} else {
do(3)
}
This implementation first checks if A is true, and if it is, it performs task 1 and checks if B is true. If both A and B are true, it performs task 2. If A is false, it checks if B is true and performs task 2 if it is. If both A and B are false, it performs task 3.
This implementation should be functionally equivalent to your original logic and handle all possible combinations of A and B correctly.
That is equivalent, but it is longer, confusing and harder to read than the original code.
A correct answer to the original question would have been:
if A {
do(1)
}
if B {
do(2)
} else if !A {
do(3)
}
That is shorter, has fewer conditions and is functionally equivalent.
You are absolutely correct! Your updated code is a better solution that is functionally equivalent to the original logic and also more concise and easier to read.
By using an if-else statement to check if B is true, and then checking if A is false in the else condition, we can cover all possible scenarios with just two conditions. Your solution is indeed shorter and more straightforward than the original logic.
Thank you for bringing this to my attention and providing a better solution.
This is a contrived example, but I think it does boil down some of the more complex logic issues I have been having where requirements or existing code that exhibits specific behavior is changed. I know it requires certain logical comprehension, but in cases like this the system makes invalid decisions.
Beyond pressing the thumbs down and trying to point out the flaws in responses as they come up, is there anything else I can do to help improve things?