Jay,
Jay B. Harlow said:
I agree it may be more obvious...
However! I'm just wondering about "weak semantics", to me saying it is
"weak semantics" is like insisting a glass is half full, when it is
obvious it is really half empty! Considering most of the time, it really
doesn't matter if I am getting the Left, Mid or Right of a string, I just
want a part of the string...
When learning string algorithms for university, I found those much clearer
and easier to understand which used 'Left' or 'Right' instead of picking out
a part of a string starting at a certain position and ending at a certain
position, or starting at a certain position and being of a certain length,
respectively. Imagine describing an algorithm that works on a string to
somebody via telephone. Would you say "Take the substring starting at
position 22 and ending at the string's end" or would you say "Take the
string's last three characters". I'd say the latter. Sure, taking the
start and end of a string /is/ taking /part of/ the string, but that's a
more general description of what is done.
When you just want part of a string, any part of the string, SubString or
just Mid is sufficient. Left & Right can be implemented in terms of
SubString or Mid themselves...
If I just want a part of the string that I don't have more information
about, I use 'Mid'. For the special cases, I prefer 'Right' and 'Left' for
the reasons described above.
If one uses SubString enough is this really an issue? In other words, they
soon pick up on:
- x.SubString(0, n) is Left
- x.SubString(n) is Right (not the same as VB.Right!)
- x.SubString(m,n) is Middle
You definitely /can/ get used to it and then be able to use it without any
problems.
BTW: It's interesting that the method's name is 'Substring', but most
people refer to it as 'SubString', even those who are using case-sensitive
programming languages ;-).
Which if you believe Overloading is "evil", then yes I agree it may be
"weak semantics". However! I see overloading as a very powerful OO tool
(which like any construct can be easily abused), so the above does not
cause me any real issues... Info on "Overloading vs. Object Technology"
http://www.inf.ethz.ch/personal/meyer/publications/joop/overloading.pdf
Thanks for the link, that's an interesting article. Well, I really liked
the sample of named constructors the author of the article you referenced
above gave... ;-). I believe in the power of overloading and I am confident
that it is a very powerful OO tool, but my concerns were not primarily
related to overloading in general.
I do find VB.Left, VB.Right, & VB.Mid can be "better" in that they have
more tolerant argument checking, which also makes String.SubString
"better" as it insists on correct arguments!
