你如何处理与C#的新特性,使它们不会导致写得不好的代码吗? [英] How do you deal with new features of C# so that they don't lead to poorly written code?

查看:119
本文介绍了你如何处理与C#的新特性,使它们不会导致写得不好的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

若干功能引入这让我感到不安,如对象初始化,扩展方法和隐式类型变量C#3.0。现在,在C#4.0之类的东西我得到更多的关注动态关键字。

A number of features were introduced into C# 3.0 which made me uneasy, such as object initializers, extension methods and implicitly typed variables. Now in C# 4.0 with things like the dynamic keyword I'm getting even more concerned.

我知道了这些特性 CAN 使用以适当的方式 BUT 在我看来,他们使开发人员更容易做出错误决定的编码,因此编写代码恶化。在我看来,微软正试图通过使编码简单,要求不高,以赢得市场份额。我个人比较喜欢的语言是严谨,放在我的编码标准,更高的要求,迫使我去结构在一个面向对象的事物。

I know that each of these features CAN be used in appropriate ways BUT in my view they make it easier for developers to make bad coding decisions and therefore write worse code. It seems to me that Microsoft are trying to win market share by making the coding easy and undemanding. Personally I prefer a language that is rigorous and places more demands on my coding standards and forces me to structure things in an OOP way.

下面是我所关注的几个例子上面提到的特征:

Here are a few examples of my concerns for the features mentioned above:

对象构造可以做到这一点不暴露给消费者重要的逻辑。这是在对象显影剂的控制。 。对象初始化借此控制路程,让消费者做出哪些字段初始化决定

Object constructors can do important logic that is not exposed to the consumer. This is in the control of the object developer. Object initializers take this control away and allow the consumer to make the decisions about which fields to initialize.

编辑:我还没有意识到,你可以混合构造函数和初始化(我糟糕),但开始显得凌乱我的脑海里,所以我现在还不能相信这是向前迈进了一步。

I had not appreciated that you can mix constructor and initializer (my bad) but this starts to look messy to my mind and so I am still not convinced it is a step forward.

允许开发者来扩展内置使用扩展方法类型允许所有和杂项开始添加自己喜爱的宠物的方法来串类,它可以用一个选项令人眼花缭乱的结束,或者需要编码标准来清除这些出更多的监管。

Allowing developers to extend built-in types using extension methods allows all and sundry to start adding their favourite pet methods to the string class, which can end up with a bewildering array of options, or requires more policing of coding standards to weed these out.

允许隐式类型变量允许快速和肮脏的节目,而不是或适当的面向对象的方法,它可以快速成为增值分销商的一个无法控制的混乱在你的应用程序。

Allowing implicitly typed variables allows quick and dirty programming instead or properly OOP approaches, which can quickly become an unmanageable mess of vars all over your application.

是我的担忧有道理?

推荐答案

对象初始只是让客户端设置属性立即施工后,没有控制被释放的调用者必须还。确保所有的构造函数的参数都满足

Object initializers simply allow the client to set properties immediately after construction, no control is relinquished as the caller must still ensure all of the constructor arguments are satisfied.

我个人觉得他们加很少的:

Personally I feel they add very little:

Person p1 = new Person("Fred");
p1.Age = 30;
p1.Height = 123;

Person p2 = new Person("Fred")
{
    Age = 30;
    Height = 123;
};



我知道很多人不喜欢'变种'关键字。我可以理解为什么,因为它是一个公开邀请滥用,但我不介意它提供的类型是言自明:

I know a lot of people dislike the 'var' keyword. I can understand why as it is an openly inviting abuse, but I do not mind it providing the type is blindingly obvious:

var p1 = new Person("Fred");
Person p2 = GetPerson();

在上面的第二行中,类型并不明显,尽管该方法的名称。我将使用类型在这种情况下

In the second line above, the type is not obvious, despite the method name. I would use the type in this case.

扩展方法 - 我会用非常谨慎,但他们是用方便的方法扩展.NET类型非常有用,特别是IEnumerable的,ICollection的和String。 String.IsNullOrEmpty()作为一个扩展方法是非常好的,因为它可以对空引用调用。

Extension methods -- I would use very sparingly but they are very useful for extending the .NET types with convenience methods, especially IEnumerable, ICollection and String. String.IsNullOrEmpty() as an extension method is very nice, as it can be called on null references.

我不认为你需要担心,良好的开发商将永远使用他们的工具的尊重和不良开发商总是会想方设法MISUE他们的工具:限制工具不会解决这个问题。

I do not think you need to worry, good developers will always use their tools with respect and bad developers will always find ways to misue their tools: limiting the toolset will not solve this problem.

这篇关于你如何处理与C#的新特性,使它们不会导致写得不好的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆