Understanding the Readability of Negative Forms

Asked 2 years ago, Updated 2 years ago, 46 views

I understand that it is better not to use negatives as much as possible after reading the readable code, but
I think there are situations where you have no choice but to use it.
In that case, for example, which of the following is better for Python?

 if not something()
if doesNotHaveSomething()

Or, if there is another way to write it, please let me know

python python3

2022-09-30 21:24

3 Answers

Readable code claims like this

  • Don't use negatives in your name
  • Do not use negation in conditional expressions

For the latter, it is also stated that negation may be easier to read.

if not something()
if doesNotHaveSomething()

In the readable code, you should think about turning over then and else to erase the former not, but if you can't do that, you should use the former.

In the latter case, it's hard to think about what happens when doesNotHaveSomething() returns false.This is why "do not use negatives in your name."


2022-09-30 21:24

"""I can't help but use negation"" = ""It's readable to use negation."

"


2022-09-30 21:24

In general, "Do not use negatives" does not mean that you should not use negative operators, but it is about method names, etc.
"If there is a negative element, the cause may be ""denial of negation"" or the loss of consistency."
You don't have to prepare a method like doesNotHaveSomething(), but not hasSomething(), right?

(Whether to follow this or not is a matter of subjectivity.)


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.