News for hackers. Stuff that matters.
Re:My bag of tricks – loose notes, design patterns, rules-of... by fatoons21 on Friday August 31, 2018 @09:25AM (#17885344)
I'm new to development, would you recommend any resource out there that covers points 1) 2) and 3)? Thanks
Re:My bag of tricks – loose notes, design patterns, rules-of... by TeMPOraL on Friday August 31, 2018 @10:35AM (#17885911)
I'm old to development, and would like to ask for the same thing. But something beyond "Pragmatic Unit Testing".

You see, I've read that. I've read one other book on unit tests too, and been on 2-day long training in TDD, and spent many hours trying to write unit tests, and yet the skill still eludes me. It's like I have a blind spot there, because I can't for the best of me figure out how to test most of the code I write.

In the projects I'm working on, I find roughly 10% of the code to be unit-testable even in principle. That's the core logic, the tricky parts - like the clever pathfinding algorithm I wrote for routing arrows in diagrams, or the clever code that diffs configurations to output an executable changeset (add this, delete that, move this there...). This I usually write functional-style (regardless of language), I expect specific inputs and outputs, so I can test such code effectively. Beyond that, I can also test some trivial utilities (usually also written in functional style). But the remaining 80-90% of any program I work on turns out to be a combination of:

- code already tested by someone else - external dependencies

- code bureaucracy, which forms vast majority of the program - that is, including/injecting/managing dependencies, moving data around, jumping through and around abstraction layer; this I believe is untestable in principle, unless I'm willing to test code structure itself (technically doable for my Lisp projects...)

- the user interface, the other big part, which is also hilariously untestable, and rarely worth testing automatically, as any regression there will be immediately noticed and reported by real people

I'm having trouble even imagining how to unit-test the three things above, and it's not something covered in unit testing books, tutorials or courses I've seen - they all focus on the basics, like assertions and red-green-refactor, which are the dumb part of writing test. I'm looking for something for the difficult part - how to test the three categories of code I mentioned above.

Re:My bag of tricks – loose notes, design patterns, rules-of... by vishalh on Friday August 31, 2018 @12:28PM (#17886861)
> - code already tested by someone else - external dependencies

You test for whether the integration works correctly, for example if you use a library for computation you will test whether your function which uses the library is producing the expected result

> - code bureaucracy, which forms vast majority of the program - that is, including/injecting/managing dependencies, moving data around, jumping through and around abstraction layer; this I believe is untestable in principle, unless I'm willing to test code structure itself (technically doable for my Lisp projects...)

Here again you do some integration testing, for example when handling database operations, you can setup a test to load fake data in the database, run operations on this fake data and compare the results, and then purge the fake data.

> - the user interface, the other big part, which is also hilariously untestable, and rarely worth testing automatically, as any regression there will be immediately noticed and reported by real people

This sort of testing has become much more common for web apps and mobile apps, there are two things being tested one is whether the interface loads correctly under different conditions(device types, screen resolutions etc) this is tested by comparing the image for the 'correct' configuration with the test results, the other thing is whether the interface behaves correctly for certain test interactions - this is tested by automating a set of interactions using an automation suit and then checking whether the interface displays/ouputs the correct result.

Re:My bag of tricks – loose notes, design patterns, rules-of... by cellularmitosis on Friday August 31, 2018 @12:42PM (#17886981)
This "Architecture: the lost years" keynote from uncle bob was helpful for me: https://www.youtube.com/watch?v=WpkDN78P884

I've also struggled with this notion of "untestable" code. Untestable code seems to usually be a big pile of function calls. Testable code seems to be little islands of functionality which are connected by interfaces where only data is exchanged.

Practically, this seems to be about shying away from interfaces which look like 'doThis()', 'doThat()', and more like 'apply(data)'. Less imperative micro-management and more functional-core/imperative-shell.

Edit: a network analogy might be: think about the differences between a remote procedure call API vs. simply exchanging declarative data (REST).

Re:My bag of tricks – loose notes, design patterns, rules-of... by bcbrown on Friday August 31, 2018 @05:41PM (#17888767)
Have you heard of the ideas in https://www.destroyallsoftware.com/screencasts/catalog/funct...? Essentially, his position is that it's only worth unit-testing those tricky bits, and the rest is inherently not worth unit-testing, because you'll almost-certainly embed the same assumptions you're attempting to test into the tests themselves.

In other words, I agree with you, but would question why you're even _trying_ to unit-test those other areas that aren't conducive to unit testing.

Re:My bag of tricks – loose notes, design patterns, rules-of... by TeMPOraL on Friday August 31, 2018 @06:24PM (#17888966)
Yes, I've heard of the idea of functional core, imperative shell. In fact, that's how I write most code these days. But I don't think I've watched this talk before, so it just went straight into my todo list.

> In other words, I agree with you, but would question why you're even _trying_ to unit-test those other areas that aren't conducive to unit testing.

Well, other people make funny looks at me when they see how little tests I write...

No, but honestly, I see so much advocacy towards writing lots of tests, or even starting with tests, and so I'm trying (and failing) to see the merit of this approach in the stuff I work on.

> you'll almost-certainly embed the same assumptions you're attempting to test into the tests themselves.

That was my main objection when I was on a TDD course - I quickly noticed that my tests tend to structurally encode the very implementation I'm about to write.

Re:My bag of tricks – loose notes, design patterns, rules-of... by icc97 on Friday August 31, 2018 @01:01PM (#17887136)
The only thing that smelled like a path to enlightenment seemed to be Haskell's QuickCheck [0]. It's actually going out searching for bugs for me, rather than me having to pretend like I can think of all the failure cases. I haven't implemented it in a project yet though so I don't know how much easier it is than unit testing.

[0]: https://hackage.haskell.org/package/QuickCheck

Re:My bag of tricks – loose notes, design patterns, rules-of... by AllegedAlec on Friday August 31, 2018 @09:39AM (#17885465)
To be fair: much of it it practice and prior knowledge. I personally found the book Pragmatic Unit Testing helpful, as well as the Clean Code chapter on unit tests.

Others may be more able to find you more readily available sources.