this post was submitted on 28 Jun 2023
515 points (97.2% liked)

Programmer Humor

19623 readers
126 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
top 47 comments
sorted by: hot top controversial new old
[–] [email protected] 34 points 1 year ago (1 children)

Not writing unit tests just isn't an option for a reliable app in the long term. But, it'll take way more than 10 minutes, always.

[–] [email protected] 5 points 1 year ago

Good tests remove development time, not add to it.

I can't tell you the countless hours saved from automated testing. Red Green refactor for life!

[–] [email protected] 32 points 1 year ago* (last edited 1 year ago) (1 children)

One rule of thumb I've heard and follow is that every time you encounter a bug, you write a unit test that would catch it. I find that does a pretty good job of getting high code coverage, though maybe that's cause my code is naturally buggy 😅.

[–] [email protected] 2 points 1 year ago
  • Writes test to trigger the bug
  • Test is green without fixing the bug.
  • Dafuq?
[–] [email protected] 21 points 1 year ago* (last edited 1 year ago)

Not testing is crazy. Once you realize you can actually refactor without ever having the fear you've broken something, there's actually opportunity to make rapid improvments in structure and performance. Taking 2 minutes to write the test can save your hours of debugging. Unless you're building a throwaway prototype, not unit testing is always the wrong choice.

[–] [email protected] 19 points 1 year ago* (last edited 1 year ago) (2 children)

Here's my take. In order to be able to write meaningful unit tests the code should be structured in a certain way, with very modular, decoupled units, dependency injection, favoring composition and polymorphism over inheritance and so on.

If you manage to write your code this way it will be an objective advantage that will benefit the project even if you don't write a single unit test. But it does make unit tests much easier to write, so presumably you'll end up with more tests than otherwise.

IMO teams should prioritize this way of writing code over high test coverage of non-modular code. Unit tests for deeply-coupled code are a nightmare to write and maintain and are usually mostly meaningless too.

[–] [email protected] 8 points 1 year ago (1 children)

I really hate the projects I work on where they've overtested to the point of meaninglessness. Like every single class has a full suite of tests mocking every single dependency and it's impossible to look at it without breaking 50 test cases.

Similarly I hate the projects with no tests because I can't change anything and be sure I've not broken some use-case I didn't think about.

Much prefer the middle ground with modular, loosely coupled code and just enough tests to cover all the use cases without needing to mock every single little thing. It should be possible to refactor without breaking tests.

[–] d0m 1 points 1 year ago

For real. I have been using DI to only mock infra and external services while keeping the actual code unmocked, and using an in memory db (we use Dynamodb so that's easy) for testing. I write tests only at the interface level, send data in, mock infra, external services, and then assert side effects. Found this to work very well for now, especially for Lambdas which we have quite a lot of.

Whenever we encounter a bug, we ensure that that case is captured in a unit test as well, but priority on integration tests.

[–] [email protected] 2 points 1 year ago

This. Just thinking about how you would test something leads to better code, at least in my experience.

[–] [email protected] 18 points 1 year ago* (last edited 1 year ago) (2 children)

All you folks are crazy not to unit test personal projects. Unit tests don't need to be fancy and exhaustive. A sanity check and having a simple way to execute isolated code is well worth the 15 minutes of setting it up. Heck, just use them as scratch files to try out libraries and APIs. I can't imagine having the kind of time to raw-dog that f12 button and sifting through print() nonsense all night.

[–] [email protected] 13 points 1 year ago

I think it also very much depends on your tooling & how easy it feels to start writing unit tests.

When I work in a Java project for example I always write unit tests even for personal stuff, because the IDE integration is great and it's really quick to create a test class, run it and see granular results. I don't feel the same way about testing JavaScript because the tooling at least for me hasn't worked quite as well (though that could very well be my own fault, it's been a while since I looked into it). The more cumbersome setting up and running the tests is, the more tempting it becomes to just use the console or manually test parts instead.

[–] [email protected] 3 points 1 year ago

If I ever want to take a break from my personal project and come back to it. Unit tests are necessary. If I try to continue working on a project that doesn't have unit tests. I feel like every bit of code I touch is introducing countless hours of debugging. It really demoralizes me.

[–] [email protected] 17 points 1 year ago (1 children)

That's why you write the tests first

[–] [email protected] 12 points 1 year ago (2 children)

TDD is pretty difficult when you don't have a very clear idea of what's the best way to achieve what you want to do – which is most projects most of the time.

It's usually also a hard sell for management, especially if the project is on its first steps and it has to show something functional in order to keep its budget.

Last but not least, the problem domain for unit tests is pretty focused. The parts of the code that are suited for unit testing may not be all that much. I know that TDD is not just unit tests but I mentioned it in the context of this discussion.

[–] Trashcan 2 points 1 year ago

If you don't know what you to achieve with the code you are writing right now, then that's your problem right there.

I'm not saying TDD is easy, and it takes effort. But you do know what you want to do before you start coding, right? Or am I missing something here?

[–] [email protected] 0 points 1 year ago (1 children)

1st problem - you should be coding such that you can make at least two iterations. It should be perfectly okay to code it once so that you have an idea and then throw it out and do it correctly. If you work in sufficiently small steps, there's barely any time waste plus you save time that you would spend maintaining

2nd problem - Kent Beck's (the guy who "invented" TDD) idea behind TDD was that it should help you be less stressed. If your job is to make a prototype or get something really out the door really fast and you won't lose sleep over it, then there is no need to write tests. TDD is there to enable you.

I am not exactly sure, what you mean with the unit tests, so apologies if I am misunderstanding. I use integration tests in my red-green-refactor loop as well

[–] [email protected] 1 points 1 year ago

Budget+Client expectations don't always give you time for two iterations

[–] [email protected] 17 points 1 year ago

I hate manually verifying stuff. There's no way I'm going to walk through all the cases I care about after making a change. So I just run my stupid tests.

[–] [email protected] 16 points 1 year ago (1 children)

When you can press "Go" and dozens of little green lights light up? That's the stuff.

[–] [email protected] 12 points 1 year ago (1 children)

Testing is so important though! It can be mind-numbingly boring, but it is an important part of software development, imo.

[–] [email protected] 5 points 1 year ago
[–] [email protected] 11 points 1 year ago

For personal projects? Sure. At work? MR doesn't get approved if the coverage drops even by 0.02% :(

[–] [email protected] 10 points 1 year ago

you'll change your mind when it's more effort to set up a test situation for a manual test. like when you change something that communicates with an expensive propietary API and you can't just push it to production.

[–] [email protected] 8 points 1 year ago

this especially makes you feel idiotic when you spend a long ass time finding & fixing a simple bug resulting from a typo then realize that it would've been caught immediately if you just wrote tests like you said you would 3 days ago

[–] [email protected] 8 points 1 year ago (1 children)

Copilot Chat can write some pretty good unit tests now

[–] [email protected] 13 points 1 year ago (2 children)

A good unit test is an expression of the intent of the programmer that wrote the unit of code. How would Copilot know what your intent was?

What Copilot does is to test exactly what the code is currently doing... which kinda defeats the purpose.

But it does raise a good point, because that's usually exactly what teams are asked to do (especially when there's a high coverage requirement). It's completely pointless busywork, but that's what makes it perfect for Copilot.

[–] [email protected] 2 points 1 year ago

Copilot Chat uses GPT-4 and can see more context than just the line you're on. It has more reasoning than the autocomplete model.

[–] [email protected] 1 points 1 year ago

Copilot happily writes failing testa too, TDD style. It just generates code tests that are very common and probably won't catch those hard to miss cases anyway.

[–] [email protected] 7 points 1 year ago

whoah okay no need for personal attacks

[–] [email protected] 6 points 1 year ago

I know I should... I know I should! But I still don't...

[–] [email protected] 6 points 1 year ago (1 children)

Writing good tests is foxtrotting hard, don't kid yourself.

Writing good tests that are actually readable and thoroughly exercise the code under test is damn near impossible.

Send help.

[–] [email protected] 3 points 1 year ago (1 children)

So so many unit tests I see don't meaningfully test anything. It would be faster to just read the unit under test because the test itself presents nothing that you wouldn't instantly recognize. Or the test is so tightly coupled to some arbitrary property that of course the test fails whenever you change something. UI tests at my current place are terrible for this, as they're just comparing DOM structures so any change breaks it.

[–] [email protected] 1 points 1 year ago (1 children)

I've never done automated UI testing. Honestly, I'm afraid to.

[–] [email protected] 1 points 1 year ago

It's not the worst thing. Like any other test there are more and less valuable methods. Imo, the hardest part is not coupling yourself to the incidental. All tests have that issue but UIs are almost entirely incidental. Styles, layout, and even data and function can be incidental and thus likely to change.

[–] [email protected] 6 points 1 year ago* (last edited 1 year ago)

Simple, design your code in a way that makes it untestable. Edit: For bonus points, design your code in a way that makes it unreadable.

[–] [email protected] 6 points 1 year ago (1 children)

This is so painfully true it hurts. Especially for personal projects.

[–] [email protected] 3 points 1 year ago (1 children)

i have never written a unit test for a personal project in my life it's not worth the overhead imo

[–] [email protected] 2 points 1 year ago

I prefer acceptance tests or end-to-end tests mostly for personal projects. Libraries I write will have unit tests though.

However, for the most part. Personal projects are a lot of prototyping and throwing away. Unit testing and testing in general can get in the way of that.

[–] Sunflip 5 points 1 year ago

The next project will be test driven, I promise!

[–] [email protected] 5 points 1 year ago

I’m a frontend developer. All the meaningful tests always break for the stupidest, obscure reasons. That’s why I don’t write tests.

[–] [email protected] 3 points 1 year ago (2 children)

As a manual QA, is writing unit tests a good way to build on coding skills?

[–] [email protected] 8 points 1 year ago

Ask your boss if there are paths for you to learn automated testing. It's a somewhat common career path for manual QA to become an SDET.

[–] [email protected] 6 points 1 year ago

No. If your goal is to move towards automated testing, see what language is used by the most popular automated frameworks and pick up a beginner's tutorial for that language.

[–] [email protected] 1 points 1 year ago

I like to tell myself "the scope of the feature is simple enough that it's unnecessary"

[–] ItsMeForRealNow 1 points 1 year ago

At least for frontend development, with new faster e2e testing frameworks that can even record the test while you see your work in a browser, lately I've been feeling I want to write more e2e tests and less unit tests.

load more comments
view more: next ›