Classic AndroidWeekly browsing at the beginning of the week is almost always great. What could be better than reading some harmful advice. Especially about code style. I love this.
Today we have three interesting takes about how we don't know how to write readable code in terms of using negations. 🎲
The first - the most extravagant - is about how you can override the not() operator for any class. And for example, they say, you can write user = !user instead of user.copy(isActive = false). Very readable, yeah, very clear (not). However, the author left a disclaimer there that the example is more for demonstration of implementation, not about where to use it. But if you can't even come up with a normal example, maybe it's a shitty idea, no?
The second is about how the exclamation mark is too small and inconspicuous, no one sees it. Therefore, you need to write limitRequests.not() instead of !limitRequests. I don't understand this at all. Yes, I agree that not is more eye-catching than an exclamation mark, but that's because usually people don't write code like that, so the eye stumbles. By the way, Osip recently wrote about this in his channel. And I also don't understand at all where this fashion came from, at some point there became really a lot of this.
I don't even really understand the argument about nullable variables, which is often brought up in such discussions. Well, just check for null earlier, why would you assemble a monster like limitRequests?.not() ?: false in a condition.
The third take is that you can change the essence of the checked variable itself. That is, instead of !limitRequests use, for example, allowRequests. Which in general is not a bad advice, but applicable in a much smaller number of cases than it seems in such a synthetic example. You won't rename some obvious variable like isLoading if it's called that throughout your project. Especially since in one place you draw a loader on true, and in another content on false, for example.
In short, if you can write code without negations, then why not. But what's wrong with the exclamation mark, if you still need it, is not entirely clear. And before overriding operators, you'd better think ten times.