What are the pros and cons of standardizing on using Option Compare Text vs Option Compare Binary for VB.NET development?
--- EDIT ---
Just some background since it seems like it would help - my development team has found it much easier to standardize on Option Strict On
, Option Infer On
, and Option Explicit
due to their obvious advantages over the alternatives. What we haven't found as easy to standardize on is Option Compare Text/Binary as there seem to be advantages and disadvantages to both and different developers have differing opinions. Some of the arguments for each side have been as follows:
Some of the advantages/arguments for Option Compare Text:
StringComparer
s and .ToLower()
calls and StringComparison.OrdinalIgnoreCase
all over the placeTHIS
and This
and this
when doing a data comparison.Select Case
statements for <asp:repeater>
events as an example.Some of the advantages/arguments for Option Compare Binary:
THIS
is different from This
and tHiS
. Of course your code should too - after all, they aren't really the exact same string.So I'm really just wondering if there are any other considerations.
-- EDIT 2 --
Perhaps it would help if I defined what I'd consider an answer to this. If you can point to any authoritative external resource that talks through these issues more thoroughly, or point to a standards and best practices discussion or book that gives guidance on this topic, that would certainly count.
With Option Compare Text
you don't need to worry about case when comparing strings. That can be a big benefit, and avoid converting everything to lower ( or upper) case to comapre for string equality.
The other place where this plays a part is sorting of strings. Option Compare Text
will sort like the file list in Windows, but Option Compare Binary
will sort like a Unix file list (all the upper case file names appear before the lower-case file names).
Update
After reading the comments and the other answer, and thinking a bit more, I'd say Option Compare Binary
is the way to go from point of view of consistency with the rest of the .Net Framework. If dictionary keys etc. are case-sensitive regardless of the Option Compare
setting then using binary comparisons by default throughout your code is just being consistent. All you then need to worry about is if, for a particular comparison, you need it to be case-insensitive and code for that.
If you go with Option Compare Text
then not only do you need to worry about whether or not you need a particular comparison to be case-(in)sensitive you also need to be aware of the default behaviour in the current context.
It then becomes an argument not of consitency with other languages, but of consistency with the framework you're developing to.
Use binary, as that's what most other languages default to, and that's what .NET classes default to.
Messing up a single word shouldn't break your whole file.
If you really need text (which isn't often), then just use String.Compare
or String.Equals
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With