tags:

views:

2374

answers:

9

After reading many of the replies to this thread, I see that many of those who dislike it cite the potential for abuse of the new keyword. My question is, what sort of abuse? How could this be abused so badly as to make people vehemently dislike it? Is it just about purism? Or is there a real pitfall that I'm just not seeing?

+13  A: 

Some see it as a tool that will be abused. Like "Option Strict Off" and "On Error Resume Next" in VB which "pure" languages like C# and Java have never had.

Many said the same about the "var" keyword, yet I don't see it being abused, once it became understood that it wasn't the same as VB's "Variant"

It could be abused in places that lazy developers don't want type checking on classes and just try catch dynamic calls instead of writing "if blah is Blah ...".

I personally feel it could be used properly in situations like this recent question that I answered.

I think the ones really understanding it's power are those heavily into the dynamic .NET languages.

TheSoftwareJedi
var is abused in a different manner - it reduces readability, not safety.
TraumaPony
I agree. I didn't mean to imply otherwise.
TheSoftwareJedi
I have seen it be abused. It definitely does reduce readability, although Jon Skeet said in a .NET Rocks podcast that it increases readability .. which I'm not sure how that is possible ...
BobbyShaftoe
It only reduces visibility if the programmer is lazy with the variable names.... or simply too lazy to look right to the = new bar()I feel that it increases visibility by removing the duplicate noise of declaring the type of the object twice, one the left and right of the variable.Also, as Martin Fowler put it:"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Dave the Ninja
Dave The Ninja
If you refractor your code enough and keep function small this way, var shouldn't be a problem.Like to add dynamic isn't to be used if you don't like type checking and stuff. It is made to make flexible patterns and easier interop possible. One implementation I saw was an xml dynamic object which you could approach like `xmlroot.node.node.Content`, that are nice flexible patterns.
Dykam
Of course "var" increases readability. There are hundreds of files I write which use StringBuilder in perhaps one function, but I don't necessarily want to import "System.Text" at the top of my file. So now with var, I can do:var builder = new System.Text.StringBuilder();insead ofSystem.Text.StringBuilder builder = new System.Text.StringBuilder();
Dave Markle
+4  A: 

The real pitfall? Severe lack of documentation. The entire application's architecture exists in the mind of the person (or persons) who wrote it. At least with strong-typing, you can go see what the object does via its class definition. With dynamic-typing, you must infer the meaning from it's use, at best. At worst, you have NO IDEA what the object is. It's like programming everything in JavaScript. ACK!

Robert C. Barth
To be fair, this was already somewhat possible: interface-heavy C# programs can make determining the actual implementation for a given call an exercise in frustration, fraught with guesses and full-text searches.
Shog9
Yes, but the interface provides SOME context of the operation. The dynamic object provides NO context whatsoever. What's on the other end of dynamic q? Who knows?
Robert C. Barth
A: 

Mandated type declaration is a crutch for developers who want the type-resolution mechanisms to help them avoid grokking the components of their application. Maybe helpful, but the downside is the problems introduced by believing that resolving a datatype mismatch is enough to assume that a poorly understood statement is correct.

Java is a good argument that you tend to end up with less simple code. Javascript is a good argument that you can't avoid knowing what you're doing.

le dorfier
Spoken like someone who has yet to have to grock a ten year old, mission-critical business system consisting of hundreds of thousands of lines of code. It already takes a new-hire a bunch of time to learn a new system. Taking away the type information makes it just that much longer (and expensive).
Robert C. Barth
You're right, no question for a ten-year-old mission-critical business system. That's IMO the proper place for java (and .NET). I see more of the other end of the spectrum where too often my comments have applied. And yes I'm familiar with the kinds of projects you describe. In java. Too often with problems, but for other reasons beyond the choice of language. But those problems would have been much worse with a less structured language. (Kinda peripheral to the original question anyway, isn't it?)
le dorfier
+6  A: 

dynamic is bad because code like this will pop all over the place:

public dynamic Foo(dynamic other) {
  dynamic clone = other.Clone();
  clone.AssignData(this.Data);
  return clone ;
}

instead of:

public T Foo<T>(T other) where T: ICloneable, IAssignData{
    T clone = (T)other.Clone();
    clone.AssignData(this.Data);
    return clone;
}

The first one, has no static type info, no compile time checking, it's not self documenting, no type inference so people will be forced to use a dynamic reference at the call site to store the result, leading to more type loss, and all this spirals down.

I'm already starting to fear dynamic.

Pop Catalin
What do you think might encourage people to needlessly write dynamic code? What might discourage them?
Curt Hagenlocher
+1  A: 

When people realize that they don't get good IntelliSense with dynamic, they'll switch back from being dynamic-happy to dynamic-when-necessary-and-var-at-all-other-times.

The purposes of dynamic include: interoperability with dynamic languages and platforms such as COM/C++ and DLR/IronPython/IronRuby; as well as turning C# itself into IronSmalltalkWithBraces with everything implementing IDynamicObject.

Good times will be had by all. (Unless you need to maintain code someone else wrote.)

Justice
I did a project in actionscript, which is both dynamic and static, and I will say that dynamic/static hybrids do not work well.
FlySwat
All people generalize from just one example. Or, at least I do. :)
Curt Hagenlocher
I did a project in QBASIC, and that should not have been a language either. But the points about ActionScript and QBASIC don't really have any relevance to C#. C# will remain, at core, statically typed object-oriented, with very useful alternative feature-sets blended in.
Justice
+11  A: 

I think that a lot of the revulsion that people are expressing to this feature boils down to "this is a bad language feature because it will allow bad developers to write bad code." If you think about it, by that logic all language features are bad.

When I run into a block of VB code that some genius has prefixed with On Error Resume Next, it's not VB that I curse. Maybe I should, I suppose. But in my experience a person who is determined to put a penny in the fuse box will find a way. Even if you empty his pockets, he'll fashion his own pennies.

Me, I'm looking forward to a more useful way of interoperating between C# and Python. I'm writing more and more code that does this. The dynamic keyword can't come soon enough for that particular use case, because the current way of doing it makes me feel like I'm a Soviet academic in the 1950s who's traveling to the West for a conference: there's an immense amount of rules and paperwork before I get to leave, I am pretty sure someone's going to be watching me the whole time I'm there, and most of what I pick up while I'm there will be taken away from me at the border when I return.

Robert Rossney
Interesting analogy
Varun Mahajan
A: 

I don't see a reason why the current way of invoking methods dynamicly is flawed:

It takes three lines to do it, or you can add a extension method on System.Object to do it for you:

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();            
        Console.WriteLine(foo.Invoke("Hello","Jonathan"));
    }        
}

static class DynamicDispatchHelper
{
    static public object Invoke(this object ot, string methodName, params object[] args)
    {
        var t = ot.GetType();
        var m = t.GetMethod(methodName);
        return m.Invoke(ot, args);
    }
}

class Foo
{
    public string Hello(string name)
    {
        return ("Hello World, " + name);
    }
}
FlySwat
What if "Hello" is overloaded? What if someone passes an int for a long? What about performance? When you travel down this road far enough, you'll have rewritten a good part of the DLR and still not handled Python or COM interop.
Curt Hagenlocher
...not to mention the natural C# syntax for indexing or infix operators
Curt Hagenlocher
That only supports Reflection-based late binding. It doesn't work for IDynamicObject based late-binding. Nor does it work for JSON or XML-based objects.
Jonathan Allen
+1  A: 

This is sort of like discussing public cameras, sure they can and will be misused but there are benefits to having them as well.

There is no reason why you couldn't outlaw the "dynamic" keyword in your own coding guideline if you don't need them. So whats the problem? I mean, if you want to do crazy things with the "dynamic" keyword and pretend C# is the some mutant cousin of JavaScript, be my guest. Just keep these experiments out of my codebase. ;)

JohannesH
A: 

FUD. It might be the best thing since sliced bread, but all the experience I've had with VB, JavaScript, etc., brings me the chills to know that C# will have dynamic binding. I know I will be able to look at it rationally in the near future and see how good that new feature will be to enable interoperability with dynamic languages. But it will take some time. I'm human, ok? :-)

Rui Craveiro