Skip to content

Minor nullability fixes - #2762

Merged
roji merged 1 commit into
devfrom
NullabilityFixes
Dec 29, 2019
Merged

roji merged 1 commit into
devfrom
NullabilityFixes

Conversation

@roji

@roji roji commented Dec 9, 2019

Copy link
Copy Markdown
Member

When setting NpgsqlCommand.CommandText and NpgsqlParameter.SourceColumn to null, they should be set to empty string.

Fixes #2761

{
_commandText = State == CommandState.Idle
? value
? value ?? string.Empty

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... What the hell is going in ADO.NET?

By the way, is it possible that _commandText could be null? If so, makes sense to change the getter.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think _commandText could (or should) be null - we should initialize it and normalize it any attempt to set it to null into an empty string. Right now the nullability annotations don't reflect this because I didn't have a clear picture at the time of how it should work.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL Client uses coalescing in the getter. Should we follow this way too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so... I'm a big believer of normalization/sanitization/coalescing as early as possible... Better to get rid of the null in the setter and not think about it again.

public sealed override string? SourceColumn
{
get => _sourceColumn;
set => _sourceColumn = value ?? string.Empty;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here. You lies that the default value is an empty string, because you have never assigned it before calling the getter.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have this initialization:

SourceColumn = string.Empty;
, is there a path where SourceColumn doesn't get called?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't seen this. In that case it would be better to coalesce to an empty string in the getter, or set a value in the field definition.

By the way SourceColumn sometimes set twice during an object construction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to coalesce in the getter and not in the setter? Are you thinking of some perf gain in not initializing to empty string in the constructor (but it implicitly gets initialized to null instead)? Otherwise it seems cleaner to make sure that the private variable (e.g. _commandText) is non-nullable and coalesce in the setter, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way SourceColumn sometimes set twice during an object construction.

You're right, I'll fix this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double-initialization also happens on ParameterName.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for perf, but for making code less error prone. Setting the default in the definition solves the problem too and probably is more preferable way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny, I see it the other way around - better to not get a bad value (null) into your system, but to stop it as early as possible (i.e. when it's set, not when it's gotten). When we complete the nullability work it would be safer too, because it would allow _commandText to actually be non-nullable, which limits potential bugs etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to go any way after cleanup since we have the same goal - less bugs (:

When setting NpgsqlCommand.CommandText and NpgsqlParameter.SourceColumn
to null, they should be set to empty string.

Also cleaned up some double-initialization of fields in the
constructors.

Fixes #2761
@roji
roji requested a review from YohDeadfall December 27, 2019 17:07
@roji
roji merged commit ea9fc42 into dev Dec 29, 2019
@delete-merged-branch
delete-merged-branch Bot deleted the NullabilityFixes branch December 29, 2019 13:36
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.

Setting to null should set to empty string: NpgsqlCommand.CommandText and NpgsqlParameter.SourceColumn

2 participants