News for hackers. Stuff that matters.
Re:Why Kotlin is my next programming language (2015) by lmm on Tuesday June 28, 2016 @12:01PM (#11994832)
If your object has distinct possible states, maybe it should be two different objects. I don't think of user input as modifying my object (after all, this kind of object should be an immutable value) - rather I think of mapping my object to a set of form fields and mapping a set of form fields into a new object. (This goes well with having a component hierarchy model - just as my object is a value made of simpler values, its corresponding editor form is made of simpler editor forms). So I'd only create the (domain-level) object when the user's provided the right input to create the object. If you need a model of the user input in code, that's fine, but make that its own distinct type - then you have a single point at which to do validation, and the types make it very clear whether a given value is a business object (in which case it is necessarily valid) or unvalidated user input.
Re:Why Kotlin is my next programming language (2015) by winstonewert on Wednesday June 29, 2016 @11:10AM (#12002086)
I see now.

That's very interesting, and shows how types can go much further then what we see in languages like Java. I'll have to try out one of these languages some day and see how well it works in practice.

Re:Why Kotlin is my next programming language (2015) by lmm on Thursday June 30, 2016 @06:24AM (#12007614)
Even in Java it's possible (but it's not a great environment to learn these techniques in - they usually end up being very verbose when expressed in Java) .
Re:Why Kotlin is my next programming language (2015) by winstonewert on Thursday June 30, 2016 @10:44AM (#12008875)
I don't know. The pure fact I can't be sure I won't have NullPtrExceptions in java code would make it pretty hard to accept not writing those tests.
Re:Why Kotlin is my next programming language (2015) by lmm on Thursday June 30, 2016 @11:19AM (#12009121)
If you always declare object fields as final then you're forced to initialize them in the constructor, so there's no risk of null there. Reading a field in the constructor should be a red flag, as should literal null in your code. Local variables should of course be final too. At that point you only need to check for null around library calls. I'm not sure how much a test actually helps with that - if you don't think of the possibility that a library call might return null then you wouldn't write a test for it either, if you do think of it then you wrap the call in a null test in the same line (don't try to get clever and store possibly-null values in variables).
Re:Why Kotlin is my next programming language (2015) by winstonewert on Thursday June 30, 2016 @02:57PM (#12010860)
> if you don't think of the possibility that a library call might return null then you wouldn't write a test for it either

No, I don't think so. I'll write a test that uses the library. If the library returns null when I don't expect it, it'll go into my code an cause an exception to tbe thrown. I'm not deliberately testing for nulls, I'm testing generally that my code does what I think it does.

Re:Why Kotlin is my next programming language (2015) by lmm on Friday July 01, 2016 @07:08AM (#12015110)
> No, I don't think so. I'll write a test that uses the library. If the library returns null when I don't expect it, it'll go into my code an cause an exception to tbe thrown. I'm not deliberately testing for nulls, I'm testing generally that my code does what I think it does.

The kind of library function that returns null doesn't usually return it on the "happy path", it usually returns null for some weird condition you haven't thought of. I mean, if it's a function that returns null often or that you'd expect to sometimes return null, you'll wrap up the return value in an option straight away even without having written a test. The kind of library function that causes you to get exceptions in production is the kind that returns null in some weird corner case - but unless you think of the weird corner case, you wouldn't hit it in a test either.

If I was using a library with some complex stateful interface I might write tests for the interaction with that (I'd probably put a facade on it, keeping it separate from the business logic, and the facade would have unit tests) - but I'd try to avoid using such a library. And I'll always have a few high-level end-to-end tests (not really "unit" tests) as a basic "does it work at all", because that kind of mistake is easy to make. But the bread-and-butter unit tests that I wrote when I worked in Python just don't seem valuable or necessary.

Re:Why Kotlin is my next programming language (2015) by winstonewert on Friday July 01, 2016 @11:36AM (#12016958)
I think you are assuming saner code then I'm used to.

Honestly, I can't say how it would work if I was using a java code base that consistently applied these kinds of ideas. You may be right, but at this point I have hard time believing.

Re:Why Kotlin is my next programming language (2015) by lmm on Sunday July 10, 2016 @04:44PM (#12067189)
I've worked on some bad codebases. You can convert a little bit at a time, treating the remainder of the codebase as a large library with a poorly factored interface. You absolutely will need unit test coverage at the boundary, which will probably exist for several years.