Proposal: Allow named boolean expressions. #7978
Unanswered
visorz
asked this question in
Language Ideas
Replies: 1 comment 3 replies
-
I don't understand your complaint about comments? Variable names are also written from the viewpoint of a third-party developer. // are we "rooted"?
if (_inputDirectory is not null && _inputDirectory.StartsWith("/"))
{
// ...
}
if (_inputDirectory is not null && _inputDirectory.StartsWith("/")) // are we "rooted"?
{
// ...
}
if (_inputDirectory is not null && _inputDirectory.StartsWith("/"))
{
// we're "rooted"
} Alternatively, just use a helper function: public static bool IsRooted(string? input) =>
input?.StartsWith("/") == true; Tangent: your method of determining if a path is "rooted" is wrong and will break on any non-Unix-like system, such as Windows. Path.IsPathRooted already exists, and the name gives clear intent on what it does. It even handles |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal
Before:
After:
Before:
After:
Or:
Named boolean expressions could explain the expression in-place. They offer a faster code reading without creating a variable name.
Reasons
Boolean expressions can be complex and therefore do not explain themselves easily.
Giving them a name is like adding a meaningful explanation what the expression is about.
Because such expressions are often only used once, creating a scope-wide variable burdens the reader's mind with one more variable name to remember. Because it could be referenced further down in the method.
To support the reading flow, the explanation for the expression should be right in place.
But a comment takes the mind out of the flow of reading instructions in which one tries to be the compiler or computer that needs to execute the code. A comment places the mind into the role of the developer colleague that speaks from a third angle.
I do not want a disturbance of my instruction reading flow, that is why I do not want to use a comment for an explanation. I like what variable names can offer already.
Beta Was this translation helpful? Give feedback.
All reactions