Repository navigation
Minor nullability fixes - #2762
Conversation
| { | ||
| _commandText = State == CommandState.Idle | ||
| ? value | ||
| ? value ?? string.Empty |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
SQL Client uses coalescing in the getter. Should we follow this way too?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
The same here. You lies that the default value is an empty string, because you have never assigned it before calling the getter.
There was a problem hiding this comment.
We do have this initialization:
npgsql/src/Npgsql/NpgsqlParameter.cs
Line 59 in a13c9c5
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
By the way SourceColumn sometimes set twice during an object construction.
You're right, I'll fix this.
There was a problem hiding this comment.
The double-initialization also happens on ParameterName.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Feel free to go any way after cleanup since we have the same goal - less bugs (:
a13c9c5 to
899026e
Compare
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
899026e to
3b86427
Compare
When setting NpgsqlCommand.CommandText and NpgsqlParameter.SourceColumn to null, they should be set to empty string.
Fixes #2761