4 ms·
Ask HN: How do I quickly re-understand the code I wrote? I am a beginner btw
- groffee 4y agoComment your code. Not just the what it does, but the why as well. Basically document everything in your comments, approaches you tried, what worked, what didn't etc. Also if you get some code from Stack Overflow etc, then leave yourself a comment of the link you found the code/library too.
- WalterGR 4y ago100%. Think of comments not as some additional hassle dumped on you, but as being kind to your future self for when you look at the code again.
- bckr 4y agoName things properly. Don't use the old-fashioned naming conventions that try to minimize the length of names. good: doc_file_parser bad: x bad: dcprsr good: getFlightString() bad: flightstr() bad: myfun() Writing code is writing. Write things in a thoughtful order. Each line is like a sentence, containing a complete idea. Don't write run-on sentences, with too many ideas in one sentence. In other words, if a single line of code is doing too much, break it into multiple lines. String lines together into thoughtful "paragraphs" that are grouped together in a way that makes sense. It's as much (if not more) about communicating with yourself and other humans as with the computer. Find some recent blog posts on code readability, variable naming, organizing code, and breaking code into functions. Next, for whichever language you're using, figure out how to use an automatic formatter and linter. These tools will standardize your code, making it easier to pattern match. Finally, just write a lot of code and continuously learn and get better. Eventually you'll become great at using your chosen language(s) and understand programming in general, and you'll be able to read code just like you can read natural language.
- forgotmypw17 4y agoGood variable names Add comments explaining exactly what you needed to figure out this time around Pretty soon you'll get the hang of what to put in the comments right away
- pizza 4y agoto understand a new codebase, unit tests are the first thing i read. hopefully things are tested in isolation to a sufficient degree that you don't have to spend a lot of effort keeping track in your head very much to follow the flow of code.
- rufus_foreman 4y agoYou can't. You'll have to rewrite it.
- michaelsalim 4y agoIn addition to comments and descriptive descriptive variable names, document everything! I constantly wish i wrote more documentation for my old code. Especially those that "i probably won't need to touch this anymore in the future"
- xupybd 4y agoThis is the trick to writing good code. It reads like a novel. I've never written code that well but the closer it gets to that the better I'm doing. I'm also assuming performance and correct code as goals. I'd recommend reading a few opinionated books on code. Some you might want to look at. Clean code Pragmatic programmer Domain Modeling Made Functional Anything that gets you thinking about code structure. Explore the ways that work for you. Don't take them as gospel but explore the thinking.
- wbsss4412 4y ago- Be thoughtful with the names you use, variables should have names that represent what they hold, functions/classes should have names that represent their behavior. - Be thoughtful in how you segment you code into functions/classes/modules. Designing well contained and intuitive interfaces is the foundation of computing. - When something isn’t intuitive, consider adding a comment.
- t-3 4y agoIf you don't quickly understand the code, reread it and look at appropriate documentation until you do. With practice the patterns and idioms will become second nature, but you must practice. It takes years of reading to get to a high level in natural language, and programming languages are no different.
- anon115 4y agoposted question on Qoura too. Link:https://www.quora.com/It-takes-me-a-while-to-re-understand-the-code-I-wrote-How-do-I-get-quicker-at-reunderstanding-it https://www.quora.com/It-takes-me-a-while-to-re-understand-t...