this post was submitted on 05 Aug 2023
396 points (100.0% liked)

Programmer Humor

853 readers
14 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
top 39 comments
sorted by: hot top controversial new old
[–] lobut@lemmy.ca 27 points 1 year ago (4 children)

I like TDD in theory and I spent so many years trying to get it perfect. I remember going to a conference where someone was teaching TDD while writing tic tac toe. Unsurprisingly, he didn't finish in time.

The thing that I hate is people conflating TDD with testing or unit testing. They're vastly different things. Also, I hate mocks. I spent so long learning all the test doubles to pass interviews: what's the difference between a spy, fake, stub, mock, etc. Also doing it with dependency injection and all that. I much prefer having an in-memory database than mock what a database does. Last company I worked at, I saw people write tests for what would happen if the API returned a 404 and they wrote code that would handle it and all that. In practice, our HTTP library would throw an exception not return with a statusCode of 404. Kinda funny.

You obviously can't always get replacements for things and you'll need to mock and I get that. I just prefer to not use them if I can.

Also, TDD advocates love saying, you're just not doing it well or you just don't know enough.

I get it, you love TDD and it works for you and more power to you.

I definitely believe in testing and having resilient tests that will minimize changes upon refactoring, but TDD doesn't work for me for most of the work I do. It works for some and I love it when it does, but yeah .... sorry random long ramble.

[–] tvbusy@lemmy.dbzer0.com 12 points 1 year ago

After many failed attempts at TDD, I realized/settled on test driven design, which is as simple as making sure what you're writing can be tested. I don't see writing the test first as a must, only good to have, but testable code is definitely a must.

This approach is so much easier and useful in real situations, which is anything more complicated than foo/bar. Most of the time, just asking an engineer how they plan to test it will make all the difference. I don't have to enforce my preference on anyone. I'm not restricting the team. I'm not creating a knowledge vacuum where only the seniors know how yo code and the juniors feel like they know nothing.

Just think how you plan to test it, anyone can do that.

[–] anarchist@lemmy.ml 4 points 1 year ago* (last edited 1 year ago)

This reminds me when a senior engineer asked me to write exception handling on a one-off python script, not a production code - just a script devs can use internally. The "handling" was that the program should exit when a file is not found. He wanted me to try the file open, except the file error, print "file not found" message and exit(1).

Guess what, genius. Python already does that for you. No need to write an extra wrapper needlessly.

[–] argv_minus_one 2 points 1 year ago

Also, I hate mocks. I spent so long learning all the test doubles to pass interviews: what’s the difference between a spy, fake, stub, mock, etc.

Oh good grief. I haven't even heard of half of those, and I've been writing code longer than most interviewers.

Also doing it with dependency injection and all that.

After struggling with several DI frameworks, I've come to the conclusion that DI is far too opaque and unpredictable to be useful.

OOP is also intentionally opaque, mind you, but debuggers can see through it if necessary, so that's fine. DI, on the other hand, is opaque even to debuggers!

I much prefer having an in-memory database than mock what a database does.

I much prefer using the same DBMS for tests as the one used in production. That way, I can use its special features (like arrays as query parameters), and avoid bugs resulting from differences between DBMSes (which would only appear in production).

You obviously can’t always get replacements for things and you’ll need to mock and I get that. I just prefer to not use them if I can.

Indeed, and you usually can. Even if your system involves a bunch of services that normally run on different machines, who says you can't spin up instances of them as part of the test? Tests are arbitrary code and can do anything that any other program can do, including starting subprocesses. Just be sure to terminate them once the test is finished, even if it fails.

[–] pkulak 1 points 1 year ago

I like to mock up dependencies with Docker Compose, then run all the tests against that. Keep the compose file in the repo, of course. I don’t tend to build a lot of real unit tests unless I’m doing something very novel and self contained. When you’re just assembling a service out of REST libraries and databases, integration testing is mostly what you want.

[–] VanillaGorilla@kbin.social 26 points 1 year ago (2 children)

With my stakeholders TDD is nearly impossible. I mean it's possible, but doesn't make sense as they shuffle their specifications every other day. I implement, they decide they wanted something different, I refactor, they don't like it, I refactor, they accept, I write tests.

Please send help

[–] Anticorp@lemmy.ml 12 points 1 year ago

Send help? We're all caught in the same fire.

[–] b1ab@lem.monster 1 points 1 year ago

Yep. I’ve been there.

[–] SmoothSurfer@lemm.ee 13 points 1 year ago* (last edited 1 year ago) (2 children)

Years of experience speaking:

  • Make it work
  • Make it right
  • Make it fast

If your end results are following this pattern, no one gives a fuck how you do

Make it maintainable should be up there too.

I’ve worked with projects that does the right thing, but no one can add new features to it because it’s a nightmare to work with. It’s at the level of not being able to move a button on the UI without breaking how the software interacts with the cloud.

[–] Anticorp@lemmy.ml 2 points 1 year ago (1 children)
[–] rikudou@lemmings.world 5 points 1 year ago

Nah, do all three. That should have been a numbered list.

[–] interdimensionalmeme@lemmy.ml 11 points 1 year ago (4 children)

How would you know what you're going to need or what it can do before you code it ?

[–] spike@discuss.tchncs.de 12 points 1 year ago

There's some things called software architecture, requirement engineering and software design. More dev teams should try this.

[–] Hexorg 7 points 1 year ago* (last edited 1 year ago) (1 children)

The Test part of TDD isn’t meant to encompass your whole need before developing the application. It’s function-by function based. It also forces you to not have giant functions. Let’s say you’re making a compiler. First you need to parse text. Idk what language structure we are doing yet but first we need to tokenize our steam. You write a test that inputs hello world into your tokenizer then expects two tokens back. You start implementing your tokenizer. Repeat for parser. Then you realize you need to tokenize numbers too. So you go back and make a token test for numbers.

So you don’t need to make all the tests ahead of time. You just expand at the smallest test possible.

[–] argv_minus_one 5 points 1 year ago (1 children)

It also forces you to not have giant functions.

No, being unable to read giant functions is what forces me not to have giant functions.

[–] Hexorg 4 points 1 year ago (1 children)

My friend, let me tell you a story during my studies when I had to help someone find a bug in their 1383-line long main() in C… on the other hand I think Ill spare you from the gruesome details, but it took me 30 hours.

[–] argv_minus_one 2 points 1 year ago

Case in point, actually. Whenever I'm forced to write a giant function, I always get nervous because it could be crawling with bugs that I have little hope of spotting.

[–] noodle@feddit.uk 6 points 1 year ago

You don't write a whole app in tests and then write the whole app in code, you make tests for the functionality as you go.

[–] biscuitsofdeath@lemmy.ml 1 points 1 year ago

I don't know if you're joking or not. I suppose tdd will help know what you need before you start.

[–] JackbyDev@programming.dev 10 points 1 year ago

I write my code, comment it out, write tests that fail, then uncomment my code, then do the proper TDD loop. Some folks get too strict about the process at the beginning saying that that a test that doesn't compile is still a failing test. My brain doesn't work like that.

[–] amio@kbin.social 10 points 1 year ago (1 children)

It's criminally underutilized. Of course, one reason is that it's hard to TDD a moving target. Since it's also hard to get people to actually fucking specify things in a lot of real world cases, it's just one more thing you ought to do, but aren't allowed to.

[–] Notnotmike 4 points 1 year ago

I think you have a point with the moving target, but also I believe that development should pretty much always be a moving target. You should be refactoring your domain based on new experiences and new knowledge all the time. So, personally, I find integration tests much more useful, because they test the input and output of a system, rather than how it's implemented. I can change my domain without having to modify my tests and that makes changes to the domain much simpler.

That being said, I also definitely recognize the advantages of TDD, I just don't think it's a silver bullet; there's good projects for it and bad ones

[–] babypuncher3000@feddit.ch 9 points 1 year ago (2 children)

TDD is actually helpful once you get the hang of it.

[–] gogosempai@programming.dev 6 points 1 year ago

Yeah but getting the hang of it is the hard part. I don't know any dev in my company or my circle that uses it; we all did learn about it alright.

[–] RagingToad@feddit.nl 2 points 1 year ago* (last edited 1 year ago)

It is a lifesaver in some cases

[–] argv_minus_one 9 points 1 year ago

It doesn't even sound great in theory. It sounds backwards in theory.

I actually love using TDD in my real life development job…

[–] Gnubyte@lemdit.com 7 points 1 year ago

Write code to test your code then repl build and run it anyways and smoketest it to see if it actually works

Sounds like activities for people who don't have real work to do. These tech layoffs cut deep because there was so much fluff in the industry. I sort of blame these companies that marketed devops too hard and oversold overcomplicated solutions, but it's also the fault of the tech leads advising managers.

[–] Bsstahl@lemmy.az.social 7 points 1 year ago
[–] RoadieRich@midwest.social 5 points 1 year ago (2 children)

It's definitely great in theory until you inherit a codebase with no tests, poor documentation, and numerous reported bugs already live in production. Even better if it was written by people hired because they could do other things better than they could code - which looking at some of the unlabeled wiring messes we were left, isn't saying a lot.

[–] Razzazzika@lemm.ee 8 points 1 year ago* (last edited 1 year ago) (1 children)

Good way to figure out how an unknown code base works is to add unit tests tho

[–] huginn@feddit.it 4 points 1 year ago

It's also the only way to migrate architecture safely.

[–] shulhan@lemmy.sdf.org 1 points 1 year ago

How does that even correlated?

[–] 7heo@lemmy.ml 5 points 1 year ago* (last edited 1 year ago)
[–] Empathy 3 points 1 year ago

I do it sometimes, especially when the bug is hard to reproduce and I know exactly what's causing it. Sometimes it's quicker to write the tests than to test manually.

[–] Metaright@kbin.social 2 points 1 year ago
[–] nothacking@discuss.tchncs.de 2 points 1 year ago* (last edited 1 year ago)

OOP is another case of nice in theory.

[–] snake_baitman@lemmy.sdf.org 1 points 1 year ago

You're goddamn right.