The C# Language Spec, Mono, and the Law
definingterms.com/2009/08/18/c-the-ternary-op...Posted Aug 19, 2009, 6:12 PM UTC
Posted Aug 19, 2009, 6:12 PM UTC
FYI, I posted this to the blog:
I don't think this is right, at least on the legal front. That "to the extent that it conforms to the specifications" just means that you can't put arbitrary code (potentially violating some other Microsoft patent) and expect it to be covered. If Microsoft had a patent on the bug, then maybe you'd have a point, but as it stands I don't think Mono would be giving up any amount of safety by matching Microsoft's implementation.
Why wouldn't this bug be considered "arbitrary code"? Goforth's point is that following Microsoft would not be an implementation of the spec; if it's not an implementation of the .NET spec., then it is an unauthorized implementation of a proprietary .NET-like spec.
I think it's time to call in the legal department on this. I don't know which side is right, but you seem to agree with Goforth while denying his conclusion (e.g. your last sentence contradicts the preceding one).
[deleted]
What is the contradiction? I agree that the arbitrary code, that infinitisimal part that does not match the spec, is not covered. But I doubt Microsoft has a patent on this particular method of error handling or would wish to exercise it if they did. I doubt that it is even patentable.
The clause in the promise is "to the extent that it conforms to specifications." What that means is that all the code that conforms is protected, but the part that does not is not. So this little part that doesn't conform is vulnerable, but the remainder of the work is safe. In order for your point of view to be correct, the limiting clause would have to be something like, "only if it exactly conforms to the spec."
You can feel free to call the legal department. In my opinion that would be a really annoying move, and unproductive to boot. They would probably tell you to retain a lawyer if you have any further questions about the promise. To me the language is plain as day and his conclusion is incorrect.
There is no contradiction if you consider that only the bug would not be covered; there is a contradiction if the presence of the bug invalidates Mono's protection. I do not know which is the case.
I wasn't serious about calling the legal department...
"to the extent that it conforms to the specifications" means that this can not be open-source in any sens of the term: If I take the mono source code and add features or modify it in a way not specificed by MS, I can be sued ? That's insane.
No.
Then, I do not understand "to the extent that it conforms to the specifications".
Does this statement define a category of source code such that if you write a piece of code from that category then MS will not sue you, but if you write a piece of code not from that category, it may sue you ?
If it does, please give me an example of a piece of code not in that category. If it does not what's the point of that statement.
Yes. For example, suppose Microsoft developed a new antialiasing operation for 3d games not part of the C# spec and that they patented and were willing to sue over. If the mono project decided to include source code that implemented this algorithm (e.g. by including the Mesa library in the default distribution) the promise would not extend to that code. It would not because the new source code is not within the extent of the specification. In fact this type of thing is exactly what the clause is there for.
you need more upvotes
The .NET 2.0 compiler implemented this to spec. This is a new bug in 3.5. Why would they break from their own spec?
[deleted]
Does that mean a windows update potentially broke a bunch of existing C# apps?
I don't know if windows update will update your .NET compiler but I doubt it decompiles your apps and recompiles them.
No it does not. It only affects apps that have been compile against the new framework. .NET 2.0 apps still work like .NET 2.0 apps.
How would it break anything? The code in question doesn't compile under Net 2.0. Net 3.5 compiles stuff that Net 2.0 doesn't. How can that break something that was compiled with 2.0?
In any case, it's in C#, not CIL, so you can probably take code compiled by the 3.5 compiler and run it under 2.0 anyway.
And thirdly, that's what the whole side-by-side thing is about anyway.
[deleted]
If you statically link everything into your program so that it is completely self contained,
Yeah, good luck on that. Let me know how that works out when you have the kernel statically linked to your program, along with all the device drivers, and windowing system. Oh, and make sure you include a full copy of the entire .NET or JVM or Ruby interpreter or whatever in every program you distribute.
You have to solve the problem at some point, unless you want to distribute each of your applications as an entire self-contained VM (which has happened). It's just not what I'd call too efficient.
Having debugged code where someone nested ternary operators, I now reject any code that uses them. House style: write out an if statement, braces and all. You'd be surprised how much grief it saves.
I used to think that, but now I disagree. I find single ternarys just fine, and quite useful.
I find myself using them quite often, as I'm used to everything being an expression.
But then you've changed an expression to a statement. I'll present a non-nested example but you should be able to see how this extends to nesting. In C++
// Foo is default initialized
Foo foo;
// now foo is bar initialized and assigned for bar > 3
if (bar > 3) { foo = Foo(bar) }
// foo is constructed in place for all bar
Foo foo = bar > 3 ? Foo(bar) : Foo ();
I would guess there are places where in C# where it is advantageous to use an expression over some statements.
I think
if( bar > 3 )
{
foo = Foo( bar );
}
else
{
foo = Foo();
}
is clearer.
The downside is that this requires foo to be left mutable at compile time; you can't mark something like const or final unless you take the extra step of moving the if/then block to a function that returns the intended value.
Unless there were a significant, palpable performance penalty I'd still prefer the more legible code. I'd also probably move the code to a function just because that's the right thing to do.
I agree it's good to be efficient, but often being efficient is achieved by being nice to the people you're working with.
Oddly enough, that's recommended in Perl Best Practices in certain situations:
For example, to produce a suitable string for a salutation in a form letter,
you might write something like:
my $salute;
if ($name eq $EMPTY_STR) {
$salute = 'Dear Customer';
}
elsif ($name =~ m/\A ((?:Sir|Dame) \s+ \S+)/xms) {
$salute = "Dear $1";
}
elsif ($name =~ m/([^\n]*), \s+ Ph[.]?D \z/xms) {
$sa1ute = "Dear Dr $1";
}
else {
$salute = "Dear $name";
}
The repeated assignments to $salute suggest that a cleaner solution,
using only a single assignment, may be possible. Indeed, you could build
a simple tabular structure to determine the correct salutation, by
cascading ternaries instead of ifs, like so:
# Name format... # Salutation...
my $salute = $name eq $EMPTY_STR ? 'Dear Customer'
: $name =~ m/ \A((?:Sir|Dame) \s+ \S+) /xms ? "Dear $1"
: $name =~ m/ (.*), \s+ Ph[.]?D \z /xms ? "Dear Dr $1"
: "Dear $name"
;
The efficiency of this series of tests will be exactly the same as the
preceding cascaded-if version, so there's no advantage in that respect.
The advantages of this approach are in terms of readability and comprehensibility.
For a start, it's very obvious that this extended construct is, despite the
many alternatives it considers, really just a single assignment statement.
And it's very easy to confirm that the correct variable is being assigned to
Did you notice the cascaded-if version has a bug in its third alternative?
That branch doesn't assign to $salute; it assigns to $sa1ute.
A second advantage of the ternary version is that (if you squint a little) it
looks like a table: one column of tests on $name and a second column listing
the corresponding salutations. It even has column borders of a kind: the
vertical rows of colons and question marks.
The ternary version is also considerably more compact, and requires
two-thirds fewer lines than the equivalent cascaded if. That makes it
far easier to keep the code on one screen as additional alternatives are
added.
The final advantage of using ternaries instead of an if cascade is that the
syntax of the ternary operator is much stricter. In a regular cascaded if
statement, it's easy to accidentally leave off the final unconditional else.
;-) Clearly, Perl people have different notion of readability from "a {} language" people, where multiple ternaries are more frowned upon.
Thank you for a considerate reply.
I disagree about comprehensiblity; if I were to need to add another case to this cascading ternary I'd be confused as to where to add it. If, instead, I encapsulate the name calculation in a function with multiple returns, I can see much more quickly what the impact of a change is going to be.
Also (and I mean this with respect), combining the ternary operator with regular expressions surely makes the code look like line noise.
makes the code look like line noise
This is Perl we're talking about!
I upvoted because you binded a technically interesting topic with hot air.
So you're saying he should get a job in marketing?
Maybe Microsoft will fix this bug, and others like it, once they've re-implemented the C# compiler in C#.
[deleted]
When they re-implement the compiler, they'd have the chance to not introduce the bugs again, since they're starting from scatch.
It doesn't quite work like this in the real world. You're dealing with something that transforms text into CIL, which is then, at some point during runtime, transformed into machine code.
Since you have this path, it becomes infinitely more difficult to just up and "fix things".
You'll have a boat load of code out there that must continue to compile with each iteration. If someone begins to rely on this implementation, things break down.
That said, there are always times where taking a breaking change will make things more sane for everyone when a particular implementation just doesn't make sense. It's never as easy as just changing such an implementation without potentially stepping on the toes of your customers.
Microsoft didn't fix bugs in their browsers for decades so why would they fix this? Software was written to conform to the bugs in the browser. I would assume the same will happen here. Of course, Mono would get screwed whichever way they go but you wouldn't expect anything else from Microsoft.
But who cares anyway?
But who cares anyway?
Usually people who live thanks to that kind of stuff
You call that living?
[deleted]
Interesting. Implicit conversions, while very useful, always seem to have these odd problematic edge cases.
Hmm. I got it right, but not for the right reason, I think... I just figured Int16 was derived from Int32...
All this would be easier if there was an open dialogue with Microsoft.
Are they opposed to that - that they'd have some place where people can ask legal questions on e.g. Mono vs .NET in the open? A subforum on the MSDN boards sounds like a natural place, since MS staff is already there, and they have numerous .NET groups there. They could call it ".NET Framework Interoperability", or something like that. Or have no one simply asked? It just seems counterproductive to guess what's legal or not on a blog, when MS may already know what they think about things.
Even MS can't afford to pay a team of lawyers to fulltime monitor a forum.
[deleted]
How is this a bug? This is just undefined behavior. The spec doesn't say that the compiler must give an error; it doesn't say anything at all about this. The compiler could do anything at all in this situation and it wouldn't be a compiler bug.
The bug is in the third code sample. The language spec says, in summary:
The third code sample sets up a case where the values are of different types (rules out option 1) and where implicit conversions exist in both directions (rules out options 2 and 3). This leaves only option 4, but the .NET 3.5 compiler doesn't follow that. That's a deviation from the spec, and a bug.
I misread. You are correct.
Its hard to imagine that some M$ lawyer isn't rubbing his hands in waiting.
Actually, that's pretty easy to imagine.
Holy shit, an actual .NET/Mono programming article in the programming subreddit? Upvotes for you!