Curious about common small coding mistakes that trip people up
Posted by Electrical_Fact7128@reddit | learnprogramming | View on Reddit | 17 comments
I was just curious so even with all the tools we have today, simple mistakes still happen in coding. For example, things like handling division by zero (Very rare these days), or modifying a function without thinking through all the callers.
Could you share small, simple examples from your experience where a seemingly “obvious” fix caused a subtle bug? I’m just trying to learn about common pitfalls.
rizzo891@reddit
I’m currently making a small JavaScript Tic tac toe game do the Odin project and I’m at the point right before I add the UI and player inputs so I decided to test my logic by looping the game ten times and just having the 2 computer opponents duke it out.
It kept hanging up and I couldn’t figure it out and it turns out I had a space in a function where my loop was looking for no space lol.
AntiDynamo@reddit
Usually the simplest things IME. Eg using in-place concatenation without properly checking types (or the types change later).
my_list += my_dict won’t error, but it also almost certainly isn’t giving you what you wanted
Set operations are also easy to mess up if you’re not paying attention when refactoring things
Candid_Difficulty236@reddit
not handling bulk operations -- writing code that works for one record and then watching it blow up when it hits a thousand at once is the mistake I see most often in production.
mattblack77@reddit
I’m just a student, but the one that tripped me up is this:
When you copy and paste snippets of code into your notes (eg a Word file) and then copy and paste them back into VSC, the quotation marks can become italicized. This makes them look fairly correct, but they become unrecognizable to the machine, so your code seems to break for no apparent reason.
MadwolfStudio@reddit
Genuine question. Why are you pasting code snippets into Word and pasting them back?
mattblack77@reddit
I think sometimes in the process of writing notes to explain the snippet, I figured out a way to make improvements and did so in the Word snippet.
Then I copied it back to VSC to try it out.
serendipitousPi@reddit
You might find markdown useful.
Pretty easy to format stuff and you can use triple backticks to add syntax highlighting for popular programming languages.
Plus there are extensions that let you convert it to a pdf if you want.
syklemil@reddit
Yeah, I think informatics student tend to gravitate towards markdown, LaTeX, or I guess these days Typst. Plays well with code editors,
git, and makes prettier output than word processors in general.Not to mention the bit where the markup languages fit a lot of programmer brains better than clicking wysiwyg editors.
mattblack77@reddit
Yeh this happened a few years ago
serendipitousPi@reddit
Ah ok lol
CapstickWentHome@reddit
Copy/pasting similar lines but forgetting to change key parts, like an X to a Y or Z in a vector calculation.
Not so common, but a spurious semicolon can easily break the code, not trip any compiler warnings and be ridiculously hard to spot when you're reading through the source.
danielt1263@reddit
The biggest issue I see in real world programming, especially among juniors and mids...
The developer needs to track some piece of information so they add a variable (usually a boolean). They do this before they have thought through how or what would change this variable over time. They then try to update that variable all over the place to keep it in sync with the already existing data that they want to track. The developer will inevitably miss something important, or there will be a situation where they can't figure out how to keep their variable up to date without adding yet another variable and going through the whole process again.
A variable is a cache, with all the attendant problems. Every time you add a variable, you are adding complexity. Before you do that, it pays to spend some time thinking through situation and seeing if there is some logic you can use with the information you already have that will be suitable.
The above is a small-scale example of u/usefulservant03's answer of course.
usefulservant03@reddit
thank you for this it's a really good way to extend what ive said, good sir
OnYaBikeMike@reddit
Forgetting to check for EINTR, EAGAIN or other retryable errors that can be returned from some system functions.
usefulservant03@reddit
Not thinking your system's design thoroughly enough at the start. I've had that happen and changing the design after I had already implemented my system around it was a massive headache.
Relying on operator precedence instead of surrounding each operation in its own ( ).
Forgetting that the compiler partially does your pointer arithmetic in C and C++ for you by adding the size of that type multiplied by whatever number you're adding to the address.
Forgetting to clean up heap allocations and other used resources in the early return paths of my function, like an error path.
Thinking that a buffer of memory has been zeroed out to prevent residual contents remaining visible to a potential adversary, when in fact the compiler has optimized away the zeroing operation because it has deduced that it's not needed and not used by anything further.
Thinking that performance bottlenecks related to memory occur at the time of allocation and not as a result of the way said memory is used at runtime.
Jonny0Than@reddit
Here's one that happened today
private void AddTimeSelectorMenuItems(TextMenu menu, Operation op)
{
var timeSelector = op.GetTimeSelector();
AddMenuItem(menu, "Schedule the burn:", null);
for (int i = 0; i < timeSelector._timeRefNames.Length; ++i)
{
var timeReference = timeSelector._allowedTimeRef[i];
var timeRefName = " " + timeSelector._timeRefNames[i];
switch (timeReference)
{
case TimeReference.X_FROM_NOW:
AddMenuItem(menu, timeRefName, () => PushMenu(BuildTimeSelectorLeadTimeMenu(op, i)));
break;
case TimeReference.ALTITUDE:
AddMenuItem(menu, timeRefName, () => PushMenu(BuildTimeSelectorAltitudeMenu(op, i)));
break;
default:
AddMenuItem(menu, timeRefName, () => ExecuteOperation(op, i));
break;
}
}
}
No-Wrap-9688@reddit
Off by one errors in loops still get me sometimes, especially when switching between languages that handle array indexing differently