4 ms·
> Everyone's code basically sucks, including yours. Which is why I'd say, unit-test your code, or TDD, or something. When you look at that crap months later, y
by pmarreck 10y ago
> Everyone's code basically sucks, including yours.
Which is why I'd say, unit-test your code, or TDD, or something. When you look at that crap months later, your mental model of how it all works has completely eroded, and only the test suite is left to preserve your expectations as they existed back then, preventing you from wasting a lot of time stepping on your own toes while you re-grok the big picture, if you have to make a change.
> The boundary between a beginner and a coding rockstar really isn't very great unless you are getting into the realm of computer sciences, like machine learning.
I don't agree with this. I didn't spend 20 years learning nothing, as it were.
- hinkley 10y agoYeah, I started using TDD, and now I sweat over my tests instead of my code. Same general quantity of perspiration either way.
- michaelbarton 10y agoI agree with your sentiment. I also think of TDD as a way of trying to overcome confirmation bias. I actively try and write tests that might prove my code has bugs. When they don't fail I feel more confident about what I'm shipping. For me that's the craftsmanship in software development - shipping something I'm proud of.
- dhruvkar 10y agoI'm.just now getting to the point where I'm finding this necessary. Any resources you can point to for a beginner in testing (python)?
- sk1pper 10y agoTDD seems to be mentioned in a lot of responses here. I'm warming up to the idea but frankly not many folks I've come across in my (short, 3 year) career seem to understand tests really, definitely not TDD. So I've been having to learn the hard way. My main thing with TDD right now is - how do I avoid writing tests that are too tightly coupled? I've gotten burned in the past, not even doing TDD, with tests that "know too much" and end up being more hassle than help. Any good resources on TDD and general testing strategy that anyone can recommend?
- hex13 10y agoYou could divide (in your head) contract and implementation details. Contract - what "unit" should do, e.g. factorial function should compute factorial for given numbers. But how exactly this is done is implementation detail (will be used recursion? do/while loop? will there be result caching?) The trick is to omit implementation details from testing, because implementation can change, but after all fact(4) should return 24 no matter what. So it's useful to think in categories of contract given unit should fulfill instead of testing everything for the sake of testing.
- zachrose 10y agoThree answers: 1) Just keep going. Experiencing the pain of less-than-ideal tests is pretty much the best way to learn. It also frees you from outdated dogma about testing. 2) Maybe try testing at different levels? I got into testing by learning to write big slow acceptance tests for a particular user experience and small fast unit tests for a particular module. This helped me see what each kind of test was best for. (Maybe your "big tests" are written in Cucumber and automate a web browser, maybe they test an HTTP API, that's up to the kind of work and environment you're in.) 3) Watch this video "Boundaries" by Gary Bernhardt: https://www.destroyallsoftware.com/talks/boundaries https://www.destroyallsoftware.com/talks/boundaries
- flukus 10y ago> I'm warming up to the idea but frankly not many folks I've come across in my (short, 3 year) career seem to understand tests really, definitely not TDD. So I've been having to learn the hard way. You aren't wrong, the state of unit tests in the wild is dreadful. Many were forced into creating tests by management decry, others just think they know what unit testing is. Sometimes the latter are even unit testing advocates. Remember what a unit is, it's not a class or the method, it's the unit of behavior you are testing. In practice this basically means whatever you are asserting. Stick to the single assert principle and keep tests small. This doesn't mean only one assert statement, but only one behavior. Often asserting that a function returns an object will one test (it didn't return null), the details of the fields that object contains (like did we format "LastName, FirstName" properly) will be different tests. If you are asserting "LastName, FirstName" in 10 tests then 10 tests will break when that behavior changes. Only one test should break. Always have a setup method that creates the owner of the unit and any of it's (mocked) dependencies. When a behavior is added it should take less than a minute to add the test for that behavior (assuming the test fixture already exists). Do that and you're writing better tests than 95%+ of the industry.