Jon said:
On Jun 7, 9:14 am, "Frans Bouma [C# MVP]"
10.Cos() might look weird, I agree, but
logically, it is something in line with "foo123".Substring(2);
I was going to reply to your response to me, but this is actually the
best example I've seen. As you said elsewhere, readability is vital.
It's more important (to me) than the logic of the thing. I would far
rather read:
double dist = Sqrt (x*x+y*y);
than either of
double dist = (x*x+y*y).Sqrt();
double dist = Math.Sqrt(x*x+y*y);
and likewise I'd much rather read
double foo = Cos(theta) + Sin(alpha);
than
double foo = theta.Cos() + alpha.Sin();
double foo = Math.Cos(theta) + Math.Sin(alpha);
I'm raised with assembler and C, when I was at the uni we didn't get
OO languages, just a lot of other paradigms. All nice but not OO.
Reading your examples, I have to admit:
double dist = Sqrt(x*x + y*y);
fits like a glove, I really like it because it's so familiar, as I've
written and read code like that for years and years.
What I want to make clear here is that it apparently seems that I am
actively PROMOTING 10.Sqrt();, but I'm not. I'm just arguing that
theoretically, it would be more OO. Not that that would be BETTER
necessarily.
Just because the code reads like old code from the good ol' times,
doesn't make it FIT in the surrounding code it is placed in,
necessarily. So if the surrounding code is pure OO code, it feels like
a friction to have code like
double dist = Sqrt(x*x + y*y);
The main reason is that the general paradigm is that 'Sqrt' here is a
method of the class this line is in. But that's not the case in the TS'
proposal.
This thus clashes, IMHO. By proceeding with what TS's proposes, you
create ambiguity in the language for the reader of the code. I then
argue: you shouldn't proceed with this, as it is a degration of
readability (== reader understands what's written).
My argument was a bit ripped apart, perhaps because I didn't structure
it well enough, but what I argued was that the REASON the static
methods are there is because of the friction between OO and procedural
and you could solve it in two ways: move the method to the class which
instance holds the data the method works on (the dreaded 10.Sqrt()) or
use it as static methods, which is IMHO still some sort of hack.
I personally agree with your examples, but not because I find them
formulate better constructs, but because they feel more closer to what
I have been reading/writing for a long time, more familiar.
However merging that with an OO language gives IMHO more problems than
it solves, so I can't agree with the proposal of TS, on the contrary.
Do you genuinely believe that either the instance method version or
the static method version including the class name conveys the
intention of the code more simply than my preferred version?
Yes, because the preferred version clashes with
SomeType t = ClassMethod(val);
when we see this kind of code in a C# class, we KNOW that
'ClassMethod' is either in this class or in its supertype. It's not a
class defined elsewhere.
Yes, you may occasionally want to use Intellisense to confirm which
class a method actually belongs to - although I'd say that when used
sparingly, it should always be obvious anyway. That productivity
decrease is easily matched by the improvement of readability in cases
like the above.
IMHO any reliance on intellisense for readability of code is bad,
because code should be clear from its construction, not because you
have fancy tools at hand. The next step will be that 'code is perfectly
clear if you have resharper x.y, coderush vZ etc. installed and setup
like user ABC.'.
Could it be used stupidly? Yes. Can it also vastly improve the
readability of certain cases, particularly maths? Absolutely.
As I see 'readability' as the term for: the reader understands what's
been written perfectly, I don't really see how
double d = Math.Sqrt(val);
is less readable than
double d = Sqrt(val);
simply because Math.Sqrt() tells the reader which Sqrt is called: the
one in the Math class. Simply calling Sqrt() is less clear, because it
could be in the class or supertype OR it could (if TS' suggestion is
added) somewhere in the classes of the scope data imported by the using
statements. IMHO not a thing you'd want to have. (Sqrt is pretty
obvious what it does, a simple thing, but a method which is pretty
complex and will call a lot of other methods, it's not that clear).
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------