C# 反模式
长话短说:我发现 Java 反模式 是不可或缺的资源。对于初学者和专业人士来说都是如此。我还没有为 C# 找到类似的东西。因此,我将把这个问题作为社区维基开放,并邀请每个人分享他们的知识。由于我是 C# 新手,我对此非常感兴趣,但无法从一些反模式开始:/
以下是我发现特别适用于 C# 而不是其他语言的答案。
我只是复制/粘贴这些!也考虑看看这些评论。
抛出 NullReferenceException
抛出错误的异常:
if (FooLicenceKeyHolder == null)
throw new NullReferenceException();
属性与公共变量
类中的公共变量 (使用属性代替)。
除非该类是一个简单的数据传输对象。
不理解 bool 是一种真实类型,而不仅仅是一种约定,
if (myBooleanVariable == true)
{
...
}
或者甚至更好的
if (myBooleanVariable != false)
{
...
}
像这样的构造C 和 C++ 开发人员经常使用布尔值的概念(0 == false,其他均为 true);这在 C# 或其他具有实数布尔值的语言中是不必要的(或不需要的)。
使用 using()
不使用 using< /code> 在适当的情况下:
object variable;
variable.close(); //Old code, use IDisposable if available.
variable.Dispose(); //Same as close. Avoid if possible use the using() { } pattern.
variable = null; //1. in release optimised away. 2. C# is GC so this doesn't do what was intended anyway.
To cut a long story short: I find the Java antipatterns an indispensable resource. For beginners as much as for professionals. I have yet to find something like this for C#. So I'll open up this question as community wiki and invite everyone to share their knowledge on this. As I am new to C#, I am strongly interested in this, but cannot start with some antipatterns :/
Here are the answers which I find specifically true for C# and not other languages.
I just copy/pasted these! Consider throwing a look on the comments on these as well.
Throwing NullReferenceException
Throwing the wrong exception:
if (FooLicenceKeyHolder == null)
throw new NullReferenceException();
Properties vs. public Variables
Public variables in classes (use a property instead).
Unless the class is a simple Data Transfer Object.
Not understanding that bool is a real type, not just a convention
if (myBooleanVariable == true)
{
...
}
or, even better
if (myBooleanVariable != false)
{
...
}
Constructs like these are often used by C
and C++
developers where the idea of a boolean value was just a convention (0 == false, anything else is true); this is not necessary (or desirable) in C# or other languages that have real booleans.
Using using()
Not making use of using
where appropriate:
object variable;
variable.close(); //Old code, use IDisposable if available.
variable.Dispose(); //Same as close. Avoid if possible use the using() { } pattern.
variable = null; //1. in release optimised away. 2. C# is GC so this doesn't do what was intended anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
错误地重新抛出异常。重新抛出异常:
Rethrowing the exception incorrectly. To rethrow an exception :
GC.Collect()
来收集而不是信任垃圾收集器。GC.Collect()
to collect instead of trusting the garbage collector.我在 Java 和 C# 中看到太多这种方式...
如果它也有的话,会加分
I see this one way too much, both in Java and C#...
with bonus points if it also has
'努夫说
'nuff said
我经常看到以下代码:
应该是:
I see following code a lot:
should be:
侮辱德墨忒尔法则:
Insulting the law of Demeter:
抛出
NullReferenceException
:Throwing
NullReferenceException
:这是真的,我亲眼所见。
它实际上在应用程序中使用,甚至还有一个与之配套的存储过程,一个会返回 null 的 sp_GetNull...
这让我很高兴。
我认为 sp 用于经典的 asp 站点.. 与结果集有关。 .net 是有人将代码“转换”为 .net 的想法...
This is true I seen it with my own eyes.
It was actually used in the app, and even had a stored procedure to go with it too, an sp_GetNull that would return null....
that made my day.
I think the sp was used for a classic asp site .. something to do with a result set. the .net one was someone idea of "converting" the code into .net...
或者更一般的情况:
Or the more general case:
我在我们的项目中发现了这个,差点把椅子弄坏了......
I have found this in our project and almost broke the chair...
我经常遇到这种 var 滥用:
甚至更好:
以这种方式使用 var 没有任何意义,也没有任何收获。它只会使代码更难遵循。
Quite often I stumble over this kind of var-abuse:
or even better:
Using var that way makes no sense and gains nothing. It just makes the code harder to follow.
不理解 bool 是一种真实类型,而不仅仅是一种约定
,甚至更好。
C
和C++
开发人员经常使用这样的构造,其中布尔值的想法是只是一个约定(0 == false,其他都是 true);这在 C# 或其他具有实数布尔值的语言中是不必要的(或不需要的)。更新:重新表述最后一段以提高其清晰度。
Not understanding that bool is a real type, not just a convention
or, even better
Constructs like these are often used by
C
andC++
developers where the idea of a boolean value was just a convention (0 == false, anything else is true); this is not necessary (or desirable) in C# or other languages that have real booleans.Updated: Rephrased the last paragraph to improve its clarity.
类中的公共变量(改为使用属性)。
除非该类是一个简单的数据传输对象。
请参阅下面的评论以进行讨论和说明。
Public variables in classes (use a property instead).
Unless the class is a simple Data Transfer Object.
See comments below for discussion and clarification.
我确实见过这个。
轻松击败
isAvailable == true
反模式!使其成为超级反模式!
I have actually seen this.
beats the
isAvailable == true
anti-pattern hands down!Making this a super-anti-pattern!
私有自动实现的属性:
Private auto-implemented properties:
在每个方法的顶部声明和初始化所有局部变量是如此丑陋!
Declaring and initializing all local variables at the top of each method is so ugly!
两个字符串反模式
反模式#1
检查字符串是否为 null 或空
反模式 #2(仅适用于 .NET 4.0)
检查字符串是否为空、空白或空格
Two string anti patterns
Anti-Pattern # 1
Checking strings for null or empty
Anti-Pattern # 2 (only for .NET 4.0)
Checking strings for null or empty or white space
无需强制转换(请相信编译器):
Needless casting (please trust the compiler):
可以更好地编写为
,甚至更好地编写为
最后一个代码清单适用于 .NET 2.0 及更高版本
can be better written as
and even better written as
Last code listing works in .NET 2.0 and above
说话带着口音总是让我着迷。
C++ 程序员:
在 C# 中,如果您输入
if (1=variable)
,这会给您带来编译器错误,这样您就可以按照您的意思编写代码,而不必担心自己会被射中。脚。Speaking with an accent always caught me.
C++ programmers:
In C# this will give you a compiler error if you were to type
if (1 = variable)
, allowing you to write the code the way you mean it instead of worrying about shooting yourself in the foot.不使用三元是我看到转换为 c# 的东西,偶尔
你会看到:
而不是:
Not using ternary's is something I see converts to c# do occasionally
you see:
instead of:
访问修改后的闭包
(请参阅链接以获取说明和修复)
Accessing modified closures
(see link for explanation and fix)
用于使用字符串连接而不是字符串生成器来连接任意数量的
字符串
For concating arbitrary number of strings using string concatenation instead of string builder
Exampls
这被认为是普遍的吗?
我不知道如何解释它,但这就像有人捕获异常并一遍又一遍地重试代码,希望它以后能正常工作。就像如果发生 IOException 一样,他们只是一遍又一遍地尝试,直到它起作用为止。
is this considered general ?
I dont know how to explain it, but its like someone catching an exception and retrying the code over and over hoping it works later. Like if a IOException occurs, they just try over and over until it works..
我所在的项目有五十个类,全部继承自同一个类,全部都定义了:
要么将其放在父类中,要么将实用程序类放在一边。啊。
您是否考虑过浏览The Daily WTF?
The project I'm on had fifty classes, all inheriting from the same class, that all defined:
Either put it in the parent class, or a utility class off to the side. Argh.
Have you considered browsing through The Daily WTF?
过于复杂的“Page_Load”方法,想要完成所有事情。
Massively over-complicated 'Page_Load' methods, which want to do everything.
使用属性除了简单地检索值或可能进行廉价计算之外,还可以用于其他任何用途。如果您从属性访问数据库,则应将其更改为方法调用。开发人员预计方法调用的成本可能很高,但他们不希望属性出现这种情况。
Using properties for anything other than to simply retrieve a value or possibly an inexpensive calculation. If you are accessing a database from your property, you should change it to a method call. Developers expect that method calls might be costly, they don't expect this from properties.
在我继承的系统中发现了几次......
反之亦然
Found this a few times in a system I inherited...
and vice versa