normalexit

joined 2 years ago
[–] normalexit@lemmy.world 1 points 2 hours ago

The rhythm of TDD is to first write a failing test. That starts driving the design of your production code. To do that you need to invoke a function/method with arguments that responds with an expected answer.

At that point you've started naming things, designing the interface of the unit being tested, and you've provided at least one example.

Let's say you need a method like isEven(int number): Boolean. I'd start with asserting 2 is even in my first test case.

To pass that, I can jump to number % 2 == 0. Or, I can just return true. Either way gets me to a passing test, but I prefer the latter because it enables me to write another failing test.

Now I am forced to write a test for odd input, so I assert 3 is not even. This test fails, because it currently just returns true. Now I must implement a solution that handles even and odd inputs correctly; I know modulus is the answer, so I use it now. Now both tests pass.

Then I think about other interesting cases: 0, negative ints, integer max/min, etc. I write tests for each of them, the modulus operator holds up. Great. Any refactoring to do? Nope. It's a one-liner.

The whole process for this function would only add a few minutes of development, since the implementation is trivial. The test runtime should take milliseconds or less, and now there is documentation for the next developer that comes along. They can see what I considered (and what I didn't), and how to use it.

Tests should make changing your system easier and safer, if they don't it is typically a sign things are being tested at the wrong level. That's outside the scope of this lemmy interaction.

[–] normalexit@lemmy.world 1 points 6 hours ago (2 children)

The monkey at the keyboard thinking is what software development is. When faced with a failing test, you make it pass as simply as possible, and then you summon all your computer science / programming experience to refactor the code into something more elegant and maintainable.

In this case that is using math to check if the input is divisible by two without a remainder. If you don't know how that works, you're going to have a bad time, like the picture in this post.

TDD doesn't promise to drive the final implementation at the unit level, but it does document how the class under test behaves and how to use it.

[–] normalexit@lemmy.world 2 points 10 hours ago* (last edited 10 hours ago)

Read the article about property based testing. It is the middle ground between what you are describing and practicality.

I often pair with myself, which sounds silly but you can write failing tests by yourself, it just isn't as fun.

[–] normalexit@lemmy.world 3 points 11 hours ago (3 children)

In a world where this needs to be solved with TDD there are a few approaches.

If you were pair programming, your pair could always create a new failing test with the current implementation.

Realistically I would want tests for the interesting cases like zero, positive even, negative even, and the odds.

Another approach would be property based testing. One could create sequence generators that randomly generate even or odd numbers and tests the function with those known sequences. I don't typically use this approach, but it would be a good fit here.

Really in pair programming, your pair would get sick of your crap if you were writing code like this, remind you of all the work you need to get done this week, and you'd end up using modulus and move on quickly.

[–] normalexit@lemmy.world 10 points 12 hours ago (10 children)

TDD has cycles of red, green, refactor. This has neither been refactored nor tested. You can tell by the duplication and the fact that it can't pass all test cases.

If this looks like TDD to you, I'm sorry that is your experience. Good results with TDD are not guaranteed, you still have to be a strong developer and think through the solution.

[–] normalexit@lemmy.world 2 points 3 days ago

I'll get up and go to another room. Try to read or do something until I get sleepy and try again.

[–] normalexit@lemmy.world 3 points 5 days ago

Can you share your smb.conf?

[–] normalexit@lemmy.world 1 points 5 days ago

This feels like a flimsy solution to the problem that will fail. I guess that when Medicaid recipients don't/won't/can't sign up, the government will take away their healthcare and use slave labor from prisons to catch up.

I think in six months they will be saying: "hey we tried this idea with Medicare recipients, but they just wanted to stay home and play video games. Now our crops are in danger of not being harvested.

What if... We round up some of these bad prisoners and make them work for almost free!? That will kill two stones."

[–] normalexit@lemmy.world 11 points 5 days ago

I mean she kind of has to. Probably the only chance she has.

[–] normalexit@lemmy.world 4 points 1 week ago

The homeless people in my area accept venmo and square cash. They put their details on their signs, which is pretty smart.

[–] normalexit@lemmy.world 5 points 1 week ago

Clear concise code that reads like documentation is the ideal. Good function and variable names, formatting, and encapsulation play into this. Tests should document and describe the system.

If it still isn't clear what the code is doing, and I'm all out of ideas (or time) for refactoring, a well placed, accurate comment is fine. It needs to be kept up to date like any other artifact in the project.

It's harder to keep comments accurate than code, since code can be executed and tested. I use them sparingly; when I've otherwise failed to write clean code, or the code is just so complex that it needs to be described.

Comments are just another tool in the toolbox. If they add clarity to the situation, by all means, use them.

If you can think of an expressive variable name that lets you skip a comment eg "employeeCount", instead of "e" // number of employees, do that.

view more: next ›