I'm not sure how practical this question is, but I'm curious: What traits do the programmers in your office or among your friends possess that make them particularly good at handling logical errors? What unique skills set them apart? Specifically, what abilities or knowledge or IQ enable them to think independently and resolve errors quickly without relying on tools like ChatGPT?
I want to know about solving logical errors within better way.
In order to solve a logical error you need to be able to answer "what do I want to happen?" and compare it to "what is actually happening?"
For example, when I have an input of 1
and 2
to my program, I'd expect to get out a
and b
. However, I'm actually getting back b
and c
. This is just a toy example to demonstrate the point.
Now that I know what my desired outcome is, vs my actually outcome is, I need to read my code more carefully and see how all the pieces of code that exist deterministically encode the logic that I expect to happen. The logic error is somewhere in the deterministic way you wrote your code to solve your issue.
Sometimes the code is small enough that you can return to it with a fresh set of eyes and just catch your issue. Usually that's not the case. In larger code bases, you want to apply normal search principles - optimize finding the issue with the fewest number of checks. To do this: [1] make sure you know the entire code path (tree) that your input leads to your output. [2] Add print statements at relevant intervals: find the "middle" of your code - whatever that means in your situation - and add a print statement "NAME: Got here 1". Then find the mid between there and the start of your code and add a print statement "Name: Got here 2". Do the same from the mid to the end of the code. Also, print out any variables you'd expect to be interesting. Do this binary searching via logs a few times. Execute your program under the same circumstances that you're having your issue. Easily find your debugging logs because they have your name in them. Then look at the variable data and see if it matches what you'd expect.
Find the section of code between where "data was what I was expecting" and "data was not what I was expecting". Somewhere in there, is my issue. Add more print lines in the binary search pattern described above. This will help you narrow in on the issue quickly.
If finding the area of code that is problematic doesn't make it obvious what your issue is, then turn on your debugger and step through the code and take a live look at all the variables.
If you're still stuck - get another person involved. By this point in time, just trying to explain to your colleague what you're trying to solve will make something click in your head and viola! you figured out the issue. If not, they may be able to see the issue after you gave them context.
There's so much more to say on the topic, but this is an instructable approach that can be described and followed. I hope this helps.