How to commit more things to memory when programming
Posted by scuffedProgrammer@reddit | programming | View on Reddit | 4 comments
I feel like when I’m programming in React I write the code line by line, but when tasks get a bit bigger, they aren’t suited to be solved this way. How can I commit more bits and bobs of the system I’m working on to memory? Right now I have to program a frontend and backend to solve a task, and I want to get rid of the tendency I have of writing one part of the system at a time and get a better overview of the system I’m working on. How should I go about doing this?
falconfetus8@reddit
I think it's better to design your code to avoid needing to have so many things in your head at once. Or at least, that's what I do. Every facet of my workflow serves that purpose in some way.
I make small, frequent commits that can be summarized in one sentence. This acts as a frequent "mental reset", dumping everything that was in my brain into the version control system. If I ever get lost or forget what I was doing, I have a big trail of history right there in my commit graph to help me find my way again. This part technically isn't about your code, but it's such a big part of keeping my head clear that I had to put it first.
I try to minimize dependencies between parts of my code, so I don't need to worry about breaking one thing when I change another. There's no good advice I can give on how to do this, because it's all dependent(heh) on your specific codebase. You mostly build an intuition for it with experience.
I try to limit the scope of variables as much as possible. I don't declare variables until right before their first usage, so I know they can't affect (or be affected by) any of the code before that point.
I use early-returns(aka "guard clauses") instead of nested if/else blocks. Not only does this narrow the amount of states that are possible at any point in the function, it also makes that narrowing visual so I don't need to keep that reasoning in my head. I can just look at it and know that
fooisn't null, because it's below the part where I wroteif (foo == null) returnI only use languages with static typing---C#, typescript, etc. I avoid languages without it(like JavaScript or Python) like the plague. It may not seem like it at first, but it's insane how much your cognitive load increases when any variable can be anything at any time. JavaScript makes you constantly keep in your head "this guy is a number, this other guy can either be a string or an object with X, Y, and Z properties depending on if the Q flag is set...". With typescript, though, you just add some annotations and then the compiler does all of the remembering for you.
So...I guess the overall answer is...don't try to keep more things in your head at once. Instead, write cleaner code so you don't need to.
MadKian@reddit
It’s funny how different minds work.
I don’t really like early returns. I much rather have a single return that I can easily debug if needed.
Ruben_NL@reddit
Doesn't that lead to "indentation hell"?
MadKian@reddit
No, the way I do it is by initializing a result var and then just assigning its value when needed.