There was a bit of a discussion in my team recently. Someone was fixing an issue and had come across some pretty poor code. A pseudo pipeline had an optional validation step. If a null validator was provided it skip validation.
final var data = deserialiser.convert(input);
if (validator != null) {
validator.validate(data);
}
processor.process(data);
archiver.archive(input, data);
What had happened was the null check had accidentally been removed, even though there was a pipeline implementer not providing a validator. Mistake aside, this is actually a pretty poor design. The developer quite rightly bemoaned the use of null to decide the behaviour.
Another developer chimed in to say that this situation wouldn’t have happened had the code been written in Kotlin instead of Java.
He was told he was missing the point.
He doubled-down, stating that the null-safe features of Kotlin would have alerted the developer to the issue.
He was told that he was still missing the point. Language features and syntactic sugar are no substitute for a good design. So the compiler alerts me to the fact that this property might be null. Great! I won’t make that mistake, but that doesn’t help the fact that this kind of check shouldn’t even needed to have been made in the first place. Does Kotlin tell me that? No. That issue has been somewhat obfuscated.
There is no substitute for good design. It’s language-agnostic. It won’t try to convince you that Shiny New Toy(TM) will solve all your problems. It will stop you from shooting yourself in the foot.
So what would have been better than a null validator? Well, in this instance the process with the null validator was updated to provide a no-op validator, the null check was removed with code asserting a validator be provided. Better, but any other improvement would require a more substantial rewrite.
If you find yourself thinking that using a particular language would solve your problems, you probably don’t understand your problems. Take the time to look at the bigger picture and wonder if maybe there is a better design there.
Banner image by Andrea De Santis on Unsplash