Skip to content

Avoid comparison against nullptr, instead use it as boolean identity. #2168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Avoid comparison against nullptr, instead use it as boolean identity. #2168

wants to merge 1 commit into from

Conversation

hzeller
Copy link
Collaborator

@hzeller hzeller commented Apr 20, 2024

Readability improvement.

We never use pointers as an artifact of a low-level memory implementation, we always use it in the context of things that can exist or not.

For these, comparing against nullptr uses a low-level technical aspect about the implementation, but not what we mean.

Using the boolean evaluation is more expressive as it reads more naturally like an existence check if (thing) thing->dosomething()

C/C++ gives us the improved expressiveness, let's use it.

@hzeller hzeller requested a review from fangism April 20, 2024 23:50
@hzeller
Copy link
Collaborator Author

hzeller commented Apr 21, 2024

Some low-level technicality purists might argue that that the underlying representation should always bleakly shine through. I'd argue that readability and expression of intent is much more important.

Readability improvement.

We never use pointers as an artifact of a low-level memory implementation,
we always use it in the context of things that can exist or not.

For these, comparing against nullptr uses a low-level technical aspect
about the implementation, but not what we _mean_.

Using the boolean evaluation is more expressive as it reads more
naturally like an existence check `if (thing) thing->dosomething()`

C/C++ gives us the improved expressiveness, let's use it.
@hzeller hzeller closed this by deleting the head repository Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants