TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How to make fewer mistakes?

67 pointsby bmombover 7 years ago
I&#x27;m a developer for almost three years, i (like) to think that i&#x27;m good, i have deep knowledge of my stack and i can design and implement almost any feature that the seniors at my team can implement, but i have i problem with the final quality of my code.<p>My code is clean and follow the guide-lines, but i feel that i make to much mistakes, there is always a corner case that i don&#x27;t think about or some test that is trivial to the QA team but i didn&#x27;t do, and my code end up being broke.<p>This &#x27;behavior&#x27; also happen with maths, i know the theorems and the content that i need to know to solve questions but i always miss calculate something and end up with the wrong result, one clear example of this was my last &quot;calculus 2&quot; exam, all the steps were right, but all the results were wrong;<p>TL, DR: I know my stack and the tools but i make silly mistakes that end up breaking my code; how can one get better at this?

33 comments

0xfadedover 7 years ago
When I was getting my pilot&#x27;s license, multiple times I started up without unchaining the tail. I joked about it with another instructor, and he basically said that it had literally never happened to him and that I should be doing a final walk around. I&#x27;d done even more embarrassing things, sitting at the runway ready to go and the tower tells me my baggage door is open.<p>Since making that final walk around a habit, nothing. I started doing final everythings, one last check of the radios, one last check of the instruments, etc, etc. It&#x27;s amazing how many times you pick up on something so obvious that is wrong.<p>The key is to change your mindset before doing the final check. Acknowledge you are done, go get a tea, and go through the motions.<p>Somethings I do related to code.<p>Use `git add -p` to go through changes one by one. Remove stuff that shouldn&#x27;t be there.<p>Do a self code review before submitting the code for others to review.<p>Obvious one, but always run tests.<p>For what it&#x27;s worth, I find the same things with maths. I&#x27;ve been working on SLAM systems, so lots of differential geometry and matrix calculus. I&#x27;ll spend a morning with my notebook trying to figure something out, go to lunch, then come back and go over what I&#x27;ve done.<p>In university I was horrible at maths exams, because like you I knew everything but made so many stupid mistakes. I think the problem was that during an exam, I could never dial down my mindset from OMG I need to triple check everything right now!
评论 #16222837 未加载
评论 #16223302 未加载
评论 #16223100 未加载
评论 #16222811 未加载
评论 #16222999 未加载
评论 #16223764 未加载
评论 #16223090 未加载
itamarstover 7 years ago
Gary Klein wrote a great book about how to learn from experience, based on research in naturalistic decision making, kinda stuff firefighters and NICU nurses do, but also e.g. design engineers for many decisions (<a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Power-Intuition-Feelings-Better-Decisions&#x2F;dp&#x2F;0385502893" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Power-Intuition-Feelings-Better-Decis...</a>).<p>His advice for learning from mistakes is basically to look for the cues or patterns you missed that caused your mistake. That way you find the hints that would&#x27;ve helped you catch it in time.<p>I&#x27;ve been doing this myself, and writing up results in a weekly email (<a href="https:&#x2F;&#x2F;codewithoutrules.com&#x2F;softwareclown&#x2F;" rel="nofollow">https:&#x2F;&#x2F;codewithoutrules.com&#x2F;softwareclown&#x2F;</a>) and it really improves how much I learn from mistakes. As in, I go from &quot;well that was stupid of me&quot; to &quot;ohhhhh, I should&#x27;ve noticed <i>that</i>.&quot;<p>Example: early in career I submitted code with bad variable names (foo, bar) to customer who requested I write a library. She pointed that out as bad practice, I was super embarrassed over my bad code. My takeaway in one of initial emails was obvious one of &quot;write readable code.&quot;<p>But later I revisited using the cues&#x2F;patterns approach, and realized deeper mistake was not listening to what she wanted—in this case, source code. If deliverable wasn&#x27;t source code then variable names wouldn&#x27;t have mattered and it would&#x27;ve been fine.
评论 #16223059 未加载
psycover 7 years ago
I believe my #1 defense against mistakes is the faint unease in the back of my mind that arises when I don&#x27;t completely understand the problem. If I become aware of this nagging doubt, and follow it, it leads to the part I don&#x27;t completely understand, where mistakes are likely. You can spend quite a long time seemingly immersed in a problem space without really understanding all of it. I think I may unconsciously flinch away from specific areas I understand least, because it&#x27;s more comfortable to re-analyze the parts I have a good grasp on. I find once I understand a problem well enough that I have any business checking in code, and there&#x27;s none of that nagging unease, then it&#x27;s usually mostly correct.
评论 #16223931 未加载
dundercoderover 7 years ago
This may not be what you want to hear.<p>Making mistakes properly is progress. Make the exact same mistake multiple times? You might need to write them down as you find them.<p>At one startup we had a motto: Measure once, cut twice. Being fix the bug, then write code to mitigate that risk in the future. Fix it twice.<p>Now in critical systems like traffic lights and the like, mistakes kill people, so you test the everloving crap out of everything, then another person does, and another.<p>If your code doesn’t kill people, ease up on yourself a bit.
评论 #16222665 未加载
评论 #16222383 未加载
评论 #16227909 未加载
in9over 7 years ago
I have the exact same problem as you. The reason why I behave like that is always due to some sort of anxiety in finishing the task at hand, together with a flawed protocol to design and review it.<p>So what seems to help me is:<p>- slow down, more time equals more time to think<p>- write specifications as neatly as possible and review it with the requester (even if it is only your self, review it many times). The more bullet points (checks, and tasks) the better. (Come to think of it, the number of checkable bullet points in an specification may be a good proxy for how much you are thinking on corner cases and etc)<p>- write extensible tests, read some nice TDD books (I haven&#x27;t done that myself, but having some canon way of thinking about tests, together with reviewing tests can help you)<p>- incorporate the expectation that there will be mistakes onto yourself, onto the client and into your planning<p>- practice practice practice
评论 #16222415 未加载
sclangdonover 7 years ago
In addition to what everyone else has said about just having more experience, my suggestion is slow down and think... and to use assert a lot!<p>I&#x27;ve noticed that some developers rely way too much on tools to do their job and they just don&#x27;t spend a lot of time <i>thinking</i>. Think about the big picture as well as the details. Think about how your code interacts with its surroundings. Think about what you know to be true at any given line (and write an assertion), and whether or not that thing may ever change. Think about what every line means in isolation, and what it means to the surrounding code. Think about the names you give things.<p>Maybe it&#x27;s because of my age, but when I started there were barely any tools to help. Even syntax highlighting wasn&#x27;t readily available. Debuggers were pretty crap, or didn&#x27;t exist at all. The best thing we could do to prevent bugs was to really question every bit of code. &quot;What does this line do if x is not what we think it is?&quot;, &quot;Can x ever not be what we think it is?&quot;, etc<p>I feel like a lot of developers today don&#x27;t think enough about what they&#x27;re doing, and care too much about their stack and their tools. Instead of moving fast and breaking things, perhaps instead try moving carefully, and try not to break things.<p>&quot;The most effective debugging tool is still careful thought, coupled with judiciously placed print statements&quot; - Brian Kernighan (replace print with assert for my interpretation!)
评论 #16224389 未加载
henrik_wover 7 years ago
Not sure if this is exactly what you have in mind, but I have had good success with my method of &quot;learning from my bugs&quot;. Whenever I encounter a difficult bug, I write a short entry in my bugs file. I detail what happened, what I overlooked, where I should have found the bug (coding, testing). I also write down lessons learned from it, and I try to regularly review the lessons.<p>More info here: <a href="https:&#x2F;&#x2F;henrikwarne.com&#x2F;2016&#x2F;04&#x2F;28&#x2F;learning-from-your-bugs&#x2F;" rel="nofollow">https:&#x2F;&#x2F;henrikwarne.com&#x2F;2016&#x2F;04&#x2F;28&#x2F;learning-from-your-bugs&#x2F;</a><p>and here: <a href="https:&#x2F;&#x2F;henrikwarne.com&#x2F;2016&#x2F;06&#x2F;16&#x2F;18-lessons-from-13-years-of-tricky-bugs&#x2F;" rel="nofollow">https:&#x2F;&#x2F;henrikwarne.com&#x2F;2016&#x2F;06&#x2F;16&#x2F;18-lessons-from-13-years-...</a>
noxTokenover 7 years ago
I&#x27;m not quite sure this would apply to you, but maybe it will help someone else: Slow down! You&#x27;ll likely add more value by being 10% slower with more correctness than 10% faster with more mistakes.<p>If you go by average time spent on a given task, I&#x27;m one of the slowest developers in my shop. I&#x27;m pretty sure I&#x27;m in the bottom 25% for speed. What I lack in speed I make up for in correctness and readability. Rarely is anything bounced back to me for a bug, defect, or missing edge case. If I start getting sloppy, I have no leverage, because now I&#x27;m one of the slowest developers who make just as many (or more) mistakes than everyone else.<p>Start by fully understanding the problem. If you ever have any doubts about anything, stop and ask. As you&#x27;re breaking down the problem, create a set of abstract notes. You could write it in paragraph form, a set of steps with checkboxes, sticky notes[0] - whatever helps you get the problem from plain business requirements to something a bit more concrete and actionable.<p>Then start writing code. Try to stick finishing in the order of your notes. It doesn&#x27;t have to be pretty, but make sure it works according to spec. Constantly execute your code to test, because it&#x27;s possible to add a conditional that renders one of your passing scenarios as a failure. If you can get a specific lists of tests that will be ran, use that as your framework. Your number one priority would be ensuring that the entire list passes deterministically.<p>After you&#x27;ve got working code, clean it up. Make sure your variables and functions have good names. getValue() is a very generic name, while getTotalCost() indicates much more without looking at another line of code. Use a linter, formatter, and static analysis tools to search for common bugs. If there are any glaring optimizations, make them.<p>[0]: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16212374" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16212374</a>
mattgreenrocksover 7 years ago
One way that helps me is to read the code I wrote in several different contexts.<p>Most of my code reading is in my editor. I&#x27;m in a certain mode of thought there, mostly detail-oriented as I piece things together. When committing code, I use git diff to inspect everything going into the commit, making sure I agree with it. I do this in a terminal (I don&#x27;t use a terminal-based editor) with a different color scheme. Lastly, I&#x27;ll re-read it again (especially if I&#x27;m not 100% sure) on a web-based git server (Github&#x2F;Gitlab) as a way to get some closure on the day&#x2F;commit.<p>This works well for me because:<p>* Most committed code is read at least twice<p>* Each reading context is different, at least visually<p>* Time passes between each reading, which gives your brain space to surface doubts<p>Doubt is your friend here. With enough experience, you&#x27;ll know when you&#x27;ve nailed something.
matt_sover 7 years ago
Knowing edge&#x2F;corner cases is experience based. You have to understand the business domain to really understand how those types of things come up. As a senior dev starting at a new company less than 2 years ago, I still don&#x27;t recognize edge cases as often as others that have been there longer.<p>I would suggest checking your work might help. Put it down and do something else, then come back to it and pretend you are doing a code review of it.<p>When you state final quality of code, your work doesn&#x27;t need to be perfect before another human looks at it. That is why we work in teams. I&#x27;m not saying be sloppy on purpose but if QA is coming up with trivial test additions, just add them. People should have feedback on code reviews.<p>If the mistakes you are making are trivial, we&#x27;ve all made them and all still make them. Syntax errors and mis-typed variables trip me up all the time. Those are usually easy to find because the code doesn&#x27;t work. If the mistakes are more design related then get some software design reviews from a senior dev before going deep into having code written.
Annatarover 7 years ago
1. run the code through a debugger as soon as you finish it;<p>2. run the code and purposely try to break it with all the edge cases you can think of;<p>3. run the code as if you are an end user who knows absolutely nothing about it;<p>4. have your colleagues try to break your code before you commit it.<p><a href="https:&#x2F;&#x2F;wiki.illumos.org&#x2F;display&#x2F;illumos&#x2F;On+the+Quality+Death+Spiral" rel="nofollow">https:&#x2F;&#x2F;wiki.illumos.org&#x2F;display&#x2F;illumos&#x2F;On+the+Quality+Deat...</a><p><a href="https:&#x2F;&#x2F;davepacheco.github.io&#x2F;se411&#x2F;release&#x2F;fcs-quality-all-the-time.html" rel="nofollow">https:&#x2F;&#x2F;davepacheco.github.io&#x2F;se411&#x2F;release&#x2F;fcs-quality-all-...</a>
评论 #16223343 未加载
stinosover 7 years ago
Like others said: experience. Training for a correct mindset.<p>For me, for basically every line of code written, and also for bigger pieces of code, I&#x27;ll try to think of all possible corner cases which might prevent the the pre- and postconditions from being established. And make as few assumptions as possible.<p>It takes a while to develop that mindset, and it takes even longer to learn to write code which doesn&#x27;t have problems in the first place. And I wouldn&#x27;t even consider myself being really good at it.<p>Note that once you get a hold of it, you&#x27;ll automatically start to write code which adheres to most &#x27;best practices&#x27; like DRY, proper naming, short functions, you name it. These go hand in hand with fewer mistakes. In the beginning this will cost you time. But that is not wasted, as it&#x27;ll teach you to do the right thing in the first place, and once you do that you&#x27;ll have to spend less time debugging and the code will be clearer in the first place so less time lost reading and understanding it.<p>As a simple example take code from a person who does lack that mindset (despite having programmed decades more than I have) which I reviewed just today: if you just write bar[1].Foo() and don&#x27;t have a strategy to either make sure bar does have enough elements, or else a strategy to properly deal with the error (as in, not just segfault, unless it has been established that is the way to deal with it) which will occur if you try to access that element anyway, the code is not good. So when I write even the simplest thing like bar[1].Foo() I <i>automatically</i> will wonder about that, ask myself &#x27;can this occur&#x27;? If it&#x27;s supposed to never happen I&#x27;ll likely insert an assert. If it can be totally acceptable, wrap it in an if statement. If it&#x27;s a user error, complain. Etc. But never ever just assume it&#x27;ll be ok. And to do that, realize you must always reflect about <i>everything</i> you can think of.
blanche_over 7 years ago
1. Have an IDE that points out the most obvious mistakes. 2. Write test (unit and functional), when you forget about corner cases writing code in TDD style may help, because it makes you think about them before writing the actual code. 3. Write your mistakes down and why you made them. 4. Have githooks that prevent you from pushing code that doesn&#x27;t pass test 5. Don&#x27;t feel bad about your mistakes, that&#x27;s how we learn.<p>You mind find this interesting: <a href="https:&#x2F;&#x2F;routley.io&#x2F;tech&#x2F;2017&#x2F;11&#x2F;23&#x2F;logbook.html" rel="nofollow">https:&#x2F;&#x2F;routley.io&#x2F;tech&#x2F;2017&#x2F;11&#x2F;23&#x2F;logbook.html</a>
muzaniover 7 years ago
IMO improve in the small things. In many cases, developers take on large projects but have little opportunity to see the small things.<p>Pick a very small but important part to improve in. The most effective training I did for myself was to practice pressing shift, semicolons, and brackets accurately.<p>Wherever you have the most trouble with, narrow it down as small as possible. Practice writing tests. Practice your algorithms, or working with a very specific part of the stack. Things like Rx can be quite a lot to grasp.<p>It&#x27;s not enough to know something once. You should be achieving fluency in all the little parts.
tomerbdover 7 years ago
I would like to sum up the comments + my thoughts:<p>1. The more time you put on a task, more mistakes you find and fix.<p>2. When finished with a task, come back to it after some time you will find more mistakes with fresh thoughts.<p>3. This means you have other good qualities like imagination and creativity, which is a side effect of mistakes (internal mind random mistakes).<p>4. Find mistakes ancestors - patterns - write them down in a short list, my top 10 mistakes patterns. When finished with a task review that you didn&#x27;t repeat those 10 mistake patterns.
fredstedover 7 years ago
I think it&#x27;s something you learn the more experience you have. I think the more mistakes, errors and bugs you see through your career, the better you get at recognizing pitfalls when you write code.<p>Also, I&#x27;m a big fan of and practice &quot;defensive&quot; programming. There&#x27;s lots of articles written about that, but in short it kind of changes your mindset a little and steers it towards more robust code. What can fail? How can I limit and validate parameters most efficiently?
评论 #16223314 未加载
snarf21over 7 years ago
You already part of the way there just by asking. Wanting to get better and knowing you can learn more is the first and hardest step. I&#x27;d also suggest that you force yourself to become more introspective. When you make a silly mistake that breaks your code, spend time thinking about <i>why</i> it broke and <i>why</i> you didn&#x27;t see it the first time. Find a mentor that is smarter and more talented. Talk to them, look at their code, etc.
jjpover 7 years ago
You say that &quot;there is always a corner case that i don&#x27;t think about or some test that is trivial to the QA team but i didn&#x27;t do, and my code end up being broke&quot;. Is the corner case that you missed a technical issue or is it that the QA team has a better understanding of the business problem that is being solved and that hasn&#x27;t been translated into a requirement&#x2F;user story that you would even know to code against.
评论 #16223303 未加载
ThomPeteover 7 years ago
Make notes on paper of your mistakes. Keep them next to you.
thisisitover 7 years ago
How about going easy on yourself and give it time? People, even the best of coders, make silly mistakes. No one is above that.
markferover 7 years ago
Outside of code, I don&#x27;t think there is anything with making mistakes. It&#x27;s a path towards progress, learning, and growth.<p>It is important that you figure out <i>why</i> the mistake was made, and <i>how</i> to not recreate it in the future.<p>I tell my employees that creating 1000 individual mistakes is fine, so long as none of them are repeats.
reacwebover 7 years ago
Find another developer like and try to find mistakes in his code while he tries to find mistakes in your code. Trying to find mistakes is not the same mindset as trying to produce good code. Maybe you can go as far as writing the unit test of each other. I have used this trick mainly to test GUI.
canduover 7 years ago
If the senior developers around you are worthy of that title, they&#x27;ve reached that point by making more mistakes than you have - and learning from them. Here are some techniques I use to learn from mistakes:<p>Double-checking. To take an example: say you take the integral of `sin x` and get `cos x` instead of `-cos x`. You can double-check this by differentiating `cos x`, which will give you `-sin x`. Not only does this tell you you&#x27;re slightly off, it suggests exactly how to fix the problem. Many problems can be double-checked: you get a result, then run the steps backwards to see if you can get back to the starting point.<p>Incremental testing. I&#x27;ve recently been learning basic robotics &#x2F; electronics as part of a side job. One thing I found enormously helpful was to build up progressively larger circuits on a breadboard - I&#x27;d even test the breadboard itself (e.g. do the two halves of each row connect? Well, there&#x27;s an easy way to find out...) to make sure I had the correct mental model of how my components were working.<p>Isolation. When confronted with a problem, try to find the minimal example that reproduces the problem. I do this a lot when building out interfaces &#x2F; applications, where I need to add some non-trivial piece into a larger application. I&#x27;ll build a test page that contains just that piece, get it working, then figure out how to integrate that working example with the application as a whole. As a side benefit, I&#x27;ve broken my seemingly hard problem down into two smaller, easier problems.<p>Informed questioning. You will inevitably hit something beyond your level of expertise. Half of experience is learning when to identify that this has happened, and knowing how to coherently explain your mental model of the problem. Your mental model doesn&#x27;t have to be right, but it should exist, and you should know how to describe it to someone who can pick it apart and improve it. Part of this is a sort of &quot;egolessness&quot; mindset: having the humility to admit you don&#x27;t understand everything, and to seek expert help once you&#x27;ve tried what you can. (We&#x27;re all bad at this last step, myself included; developing this mindset is itself a learning process.)<p>Finally, and IMHO most importantly: failure normalization. Failure is normal when learning a new tool, framework, language, skillset, etc. In this situation, one of the first things I do is to explore what failure looks like. I&#x27;ll deliberately introduce syntax &#x2F; logic errors: what sorts of errors does the compiler &#x2F; toolchain spit out? How can I use the debugger to step into the problem, and what does that process look like? Do my tools give me any kind of visual &#x2F; tactile &#x2F; etc. feedback on my error? (If not: where can I find better tools?)
PhilipAover 7 years ago
If you aren&#x27;t embarrassed by the code you wrote 6 months ago, you haven&#x27;t progressed enough.
评论 #16222609 未加载
comstockover 7 years ago
Confirm results through orthogonal means where possible.<p>If it comes to it write two solutions (possibly one optimized, one not) that solve the same problem in different ways, and confirm the results against each other.<p>Don’t count on just picking good edge cases.
Protostomeover 7 years ago
Consider adopting TDD (Test driven development). Be your own QA.<p>After finishing writing a functional piece of code, spend half an hour or so (maybe more?) trying to break it.
kleer001over 7 years ago
Take it existential, imagine that there&#x27;s more at stake than just this task, project, or your job. I&#x27;m not sure exactly what, be creative.
segmondyover 7 years ago
Read these books, &quot;Talent is overrated&quot; &amp; &quot;The power of habits&quot; &amp; &quot;Checklist manifesto&quot;
bookofjoeover 7 years ago
When typing, go more slowly.
chvidover 7 years ago
No omelette without breaking a few eggs ...
cnkkover 7 years ago
make more mistakes, so you can learn from them.
Tomteover 7 years ago
Checklists.
kldfjghover 7 years ago
How to make &lt;i&gt;fewer&lt;&#x2F;i&gt; mistakes.
评论 #16222630 未加载
评论 #16222506 未加载