Relying on tests too much keeps me from getting a better feeling for what my code does
Posted by home_tech_guy@reddit | learnprogramming | View on Reddit | 11 comments
I recently noticed that I am forgetting what my code does too quickly. If something doesn't work, I run the test and then debug through it. But I feel like this approach keeps my knowledge of my own code superficial. I forget more quickly what my code does and how everything relates.
Have you dealt with anything similar?
I feel like without the tests I am loosing a lot of security but that with them I become inert and lose motivation to keep the connections in my head.
JaleyHoelOsment@reddit
if you become a developer you will be writing a shit ton of code, no way you’ll remember all of it.
sometimes i’m building or fixing some feature, get confused, git blame the code and bam… it was me who wrote the his garbage 5 years ago.
you’re not required to remember your code. writing tests is 100% required
iOSCaleb@reddit
I doubt the tests are really a problem. Without tests, you’d still forget exactly how a given function work, but you’d lack a straightforward way to debug it.
Lonely-Foundation622@reddit
This is really common if you work on a big monolith it would be weird if this doesn't happen, if you understand the code you've written though you should be able to read it again.
Also your former self needs to start adding comments in your code to make it easier for your current self but overall this is completely normal dw about it.
Frolo_NA@reddit
that's a good thing, not a bad thing.
it keeps your code simple
RajjSinghh@reddit
This is why functions having descriptive names is so important, or comments and documentation. You should know what code does or at least be able to easily find out.
You shouldn't really worry about how the code does that which is subtly different. With enough tests you should be pretty happy your code is correct. You should also make an effort to make sure your code is readable enough that if you needed to you could go back and figure it out again. But you don't need to remember exactly what every line of code does all the time. That's the concept of abstraction.
home_tech_guy@reddit (OP)
I understand but it surprises me how much stuff I just forget and find out by rerunning a test.
For example, when I'm adding modifications, I immediately run the tests and realize there's another place that should implement that change. And then maybe there's another one. It's all nice, but I feel like I've become too lazy and "allowed" myself to forget details because there are always tests to find everything from a typo to a logical problem.
No-Needleworker6779@reddit
This is the core goal of tests though, to verify the cases you aren’t actively thinking of. Even if you had an eidetic memory you’ll never have a full grasp of all aspects of the code base at all times unless you work on very small projects, and that’s perfectly fine. There’s no need to understand all aspects of it so long as it does what it’s supposed to and it’s easy to grok when you choose to expend the effort.
Dubstephiroth@reddit
For me, and I am still new to js, I find that online comments and temporary logging in code helps to remember whats been doing and needs doing. The logs can go once I feel confident in the code base, or at least the particular module. As i said i am still new, only 5 months in, but comments and temporary logs have saved me.
home_tech_guy@reddit (OP)
This is actually a good point. I haven't done propper logging since a few years ago. Maybe that will help.
onefutui2e@reddit
Kind of, but not really is the best way I can describe it.
Automated testing should be for you to assert against how your system should behave. In this way, it is its own documentation; whenever you're unsure how your system would behave for a given set of inputs, your unit tests should clue you in, either through static analysis or by failing/succeeding.
As you point out, having tests gives you a level of security knowing that new changes don't introduce unexpected behaviors.
What you say with regards to losing your "feel" for the code, that can happen and arguably is a "feature" of having excellent test coverage and architecture. If you're soloing a project by yourself, you might not see the immediate benefit. But if you work on an incredibly complex code base with multiple contributors, it's actually a great thing for you not to just rely on your feel. In the "real world", imagine you're pulled off a project and someone else takes your place. This person will have no "feel" and will rely on test coverage to ensure they're not breaking anything.
And finally, from my experience, relying on tests hasn't necessarily diminished my feel for the code. I still know what it does and where it does it, but I have the extra validation that I have the test coverage needed to make changes.
home_tech_guy@reddit (OP)
This is true that in a sufficiently big codebase there is no other way around. Let me quote repost my comment from above.