- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Not quite what it is doing.
new Guid() will create a guid with all zeros.
To create a guid you use the method Guid.NewGuid().
Still a WTF, just more in the framework than this code.
Admin
new Guid() in C# actually return 00000000-0000-0000-0000-000000000000 (this is also accessible via Guid.Empty). To create a random Guid you have to call Guid.NewGuid(). I'm guessing this is to match behavior with other value types, since those also default to the, well, default value when instantiated. So this code actually checks if the given Guid is non 0.
Admin
new Guid()
in C# returns an empty Guid{00000000-0000-0000-0000-000000000000}
.Guid.NewGuid()
returns a random Guid.This is a weird way of checking the provided Guid is set to something
Admin
Comment held for moderation.
Admin
Actually, the code does something completely different: new Guid() created a specific Guid composed only of zeros (00000000-0000-0000-0000-000000000000), so the code is checking it that specific 0 guid has been passed in.
If you want a new random guid you need to use Guid.NewGuid()
Admin
Sounds to me like the coworker is a new guy...d.
And almost certainly unique. Perhaps a bit null as well.
Admin
(probably the 7 comments held for moderation are saying that same thing -- TRWTF #1)
actually
new Guid()
in c# doesn't randomly generates one because a value type default constructor is always generated by the compiler as to fill the memory with zero, therefore yielding the00000000-0000-0000-0000-000000000000
Guid -- TRWTF #2Admin
new Guid() actually returns the same as the static Guid.Empty field, an empty GUID (all zeroes). If you'd want a new random Guid, you'd use Guid.NewGuid().
This check could be rewritten as if (objectId != Guid.Empty).
Admin
Isn't the "non-nullable" support in C# an optional feature?
Admin
True, but it's mostly by coincidence. Guid is a "value" type, and the parameterless constructor of such a type initialises it (and by implication its members) to default values, which (because the members are all value types) means zero.
It's worth noting that the Microsoft documentation doesn't mention the parameterless constructor at all.
Admin
More like the other way around - nullability is optional for "value" types.
Admin
If
objectId
is declared asGuid? objectId
, then yes, it can be null.Admin
There are many duplicates, but isn't this code simply a valid but awkward way to ensure that the GUID was generated by Guid.NewGuid() rather than a misused 'new Guid()'?
Admin
TRWTF here is that Remy's interpretation of this code is wrong in at least two ways 😆
objectId
is declared. It could be aGuid?
(a.k.a.Nullable<Guid>
), in which case it could be nullnew Guid()
isn't the same asGuid.NewGuid()
. The default constructor of a value type just creates an all-zero instance of the type, so00000000-0000-0000-0000-000000000000
in this case. (although in recent versions of C#, it's possible to create explicit default constructors on structs, making the existingGuid
default constructor even more confusing...)Admin
Actually, I think you'll find that
new Guid()
returns a GUID with all bits set to zero....... oh wait..... frist! No, dammit.
Admin
is objectId is declared as Object?
SOO many things wrong with this article(not the submitter), that this article) rather than any submission may win the WTF of the week award....
Admin
I don't know if anyone's mentioned it yet, but
new Guid()
returns a Guid with all zeros, and you have to useGuid.NewGuid()
to get a randomly generated value.Admin
There is an optional feature for explicitly nullable reference types. I love it, it's great for avoiding those pesky null-reference errors, but it's only syntactic sugar. There's nothing at build or run time to prevent you from assigning null to a non-nullable reference. As mentioned already, value types are never nullable, unless you wrap them with a Nullable<T> (or the '?' abbreviation).
Admin
That was exactly my thought. Since objectId is of type object, the developer couldn't figure out how to compare it to Guid.Empty (obviously, using something like
object.Equals(objectId, Guid.Empty)
), and went with theToString
approach. The article is a much bigger WTF than the code itself.Admin
I'm pretty sure you get at least a compiler warning when trying to compare value types to null. So objectId is probably of type object (or if it's really WTF, dynamic).
So the real WTF is the developer is not using types and/or casting properly, also doesn't know about Guid.Empty.
Addendum 2023-08-16 11:03: On the other hand, if objectId is a Guid? (that is, a nullable Guid), then the comparison to null is fine. But then the developer could have compared it to a new Guid easily so this is probably not the case.
Admin
Suppose ObjectID is a Guid? At that point the code makes sense other than it should be compared to Guid.Empty rather than new Guid(). This is testing for a null or default case, akin to String.IsNullOrEmpty();
Admin
Comment held for moderation.
Admin
Is this some sort of protest against / joke about the long list of "held for moderation" comments yesterday? After all, those should have a gray background.
Admin
Value types (struct) have always been non-nullable (because they are stack values), since 2.0 there is a generic type struct Nullable<T> (also a value type), which is basically T plus a boolean if T should be considered null. There's a lot magic actually happening on a CTS level so that nullable value types behave very similar to reference types.
Reference types (class) on the other hand are always nullable (because they live on the heap and are allocated, so basically they are type safe pointers and a zero pointer is considered invalid), which makes them compared to value types super expensive especially when it comes to allocations (but also when they get released). Since .net 5 there is an option to opt-into the non-nullable reference type feature, which dictates syntactically that reference should be treated by the compiler as non-nullable. However there is less direct support on a CTS level, so there's a lot of attributes required even for a common application developer to make the feature work correctly (for example when you implement the Try-Parse pattern for reference types).
Admin
All value types have a default constructor which initializes structs to binary zeros.