1395
Tests are code too (lemmy.world)
submitted 10 months ago by jroid8@lemmy.world to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] Alexc 57 points 10 months ago

This is why you write the test before the code. You write the test to make sure something fails, then you write the code to make it pass. Then you repeat this until all your behaviors are captured in code. It’s called TDD

But, full marks for writing tests in the first place

[-] oce@jlai.lu 73 points 10 months ago

That supposes to have a clear idea of what you're going to code. Otherwise, it's a lot of time wasted to constantly rewrite both the code and tests as you better understand how you're going to solve the task while trying. I guess it works for very narrowed tasks rather than opened problems.

[-] nic2555@lemmy.world 4 points 10 months ago

TDD doesn't imply that you write all the tests first. It just mean you have to write a test before you write a line of production code.

The idea is to ask yourself "what is the first step I need, where am I going to begin?". You then write a test that validate this first step and fail. Then you write the code to make it pass. Once your done with that, you ask yourself: "what's the next step? ". You, then, repeat the process for that step.

This is a process you are going to do anyway. Might as well take the time to write some test along with it.

[-] Lifter@discuss.tchncs.de 3 points 10 months ago

That leads to focusing on the nitty gritty details first, building a library of thing you think you might need and you forget to think about the whole solution.

If you come up with another solution half way through, you will probably throw away half of the code you already built.

I see TDD as going depth first whereas I prefer to go breadth first. Try out a solution and skip the details (by mocking or assuming things). Once you have settled on the right solution you can fill in the details.

load more comments (7 replies)
load more comments (7 replies)
this post was submitted on 02 Nov 2023
1395 points (98.5% liked)

Programmer Humor

32054 readers
1663 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS