在CodeProject上面看到一片文章: Hall of shame
觉得挺有意思的,以下是糟糕的代码合集,请大家慢慢品尝。
1:那是一片丛林
我正在重构一个写的很可怕的程序,以吨来计算的重复代码,大量无效的代码(30%-50%),然而我最个人最”钟爱”的是下面的这个方法:
public void Some_Method()
{
try
{
}
catch (Exception x)
{
LogException(x);
}
}
不管是幸运还是不幸,这个方法从来就没被调用过,但是我非常高兴看到catch语句。。万一空的try抛了异常那该怎么办呢?
2:if not null, return value else return null
public DataTable GetFooByID(int memberID)//MethodID #210
{
try
{
string query = "SELECT DISTINCT * FROM Foo WHERE bar=" + barId;
DataTable dt = Utils.ExecuteReader(query);
//这是个神奇
if (dt != null)
{
return dt;
}
else
{
return null;
}
}
catch (Exception ex)
{
//为什么是#210?,难道是210行?
throw new Exception("MethodID:= #210, Error:=" + ex.Message);
}
}
3:完美的循环
上周我正在为一个非常简单的变更做code review,这个变更是我以前让团队成员做的。
我们在下载文件的时候使用循环来判断如果是A的话,或者是B的话该如何处理。
foreach(MyfileInfo fil from files)
{
switch (fil.MyFileType)
{
case A:
//Do some work for file A
break;
case B:
//Do some work for file B
break;
}
}
所有我所要求他修改的是针对File B 的处理。下面就是我得到的:
foreach(MyfileInfo fil from files)
{
switch (fil.MyFileType)
{
case A:
//Do some work for file A
break;
case B:
foreach(MyfileInfo fil2 from files)
{
switch (fil2.MyFileType)
{
case A:
break;
case B:
//Do new work for File B.
break;
}
}
break
}
}
4:无助的Case
无助的Case
5:Or “Else”…
Public Sub SetWrite(ByVal CmdVal As String)
Select Case UCase(CmdVal)
Case "T"
m_WriteOutErrorLog = True
Case "F"
m_WriteOutErrorLog = False
Case "Else"
MsgBox("Error: Error Logging Parameter not recognised, should be 'T' or 'F'")
End Select
End Sub
6: 聪明的码工 还是 恼人的蠢驴
试图调试上一个雇员3-4年前写的代码,我无法判断他究竟是个英雄还是一个蠢驴,你是怎么看的?(在任何人评论之前,我要说它是合法的,并且它大部分时候都工作正常,但是试图调试的时候…)
for (; (Var1 < Var2) && (Var3 == FALSE) && ( ( Var4 < 0 ) || (Var5 <= Var6) ); Var7++)
{
.
.
//700 lines of code
.
.
}
7:怎样生成菜单
下面是生成我们的主菜单的一个循环,没错只有一行代码:
<% int i = 1; foreach (Fatthallah.Web.HttpHandler.PetraMainMenu mainMenuItem in mainMenuCollection) { Response.Write…
在做了一些小小的调整之后:
<%
int i = 1;
foreach (Fatthallah.Web.HttpHandler.PetraMainMenu mainMenuItem in mainMenuCollection)
{
Response.Write("<li style=\"float:"
+ (lang == "en" ? "left" : "right")
+ "\"><a id =\"a" + i
+ "\" name=\"a" + i
+ "\" href=\"" + (mainMenuItem.MainMenuLink.Trim().Length > 0 ? ResolveUrl("~/" + mainMenuItem.MainMenuLink.Split('?')[0]
+ "lang=" + lang
+ "&" + mainMenuItem.MainMenuLink.Split('?')[1]) : "javascript:void(0)") + "\" onclick=\"fetchData('menuHandler.axd','hml','menu=true&id=" + mainMenuItem.MainMenuId
+ "&lang=" + lang + "',this);return false;\" onmouseout=\"hidebox(this);\" >"
+ (mainMenuItem.MainMenuTitleEnglish.Trim().Length > 0 && lang == "en" ? mainMenuItem.MainMenuTitleEnglish : mainMenuItem.MainMenuTitleArabic)
+ "</a></li>");
i++;
}
Response.Write("<script language="'javascript'" type='text/javascript'>mainMenuCounter =" + mainMenuCollection.ToArray().Length + "; var rtl=" + (lang == "en" ? "false" : "true") + ";</script>");
%>
8:有开始就有结束
'''<Summary>
'''根据index 返回View
'''</Summary>
Protected Overrides Function GetViewAt(ByVal index As Integer) As _
System.Collections.Generic.KeyValuePair(Of String, String)
If (index = 0) Then
Return GetView0
End If
If (index = 1) Then
Return GetView1
End If
If (index = 2) Then
Return GetView2
End If
If (index = 3) Then
Return GetView3
End If
If (index = 4) Then
Return GetView4
End If
If (index = 5) Then
Return GetView5
End If
' ...lots and lots of lines...
If (index = 1461) Then
Return GetView1461
End If
If (index = 1462) Then
Return GetView1462
End If
If (index = 1463) Then
Return GetView1463
End If
Throw New System.IndexOutOfRangeException()
End Function
9:Check for True to return True...
Dim bool as Boolean = False
If someExpression Then
bool = True
Else
bool = False
End If
If bool Then
Return True
Else
Return False
End If
10:What do you think of my small game made in Windows API RAW?
11:Another coding horror
Code from one of my junior ----
this is Page-A.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("Page-A.aspx");
}
这段代码使浏览器一直的Loading…
Above piece of code when load, it keeps browser busy to keep loading the same page....
12:After All My Years
I have only just found out what this would do:
<!-- <my:control runat="server" /> -->
这行代码导致其中一个页面加载超慢,并且我也不确定为什么会这样,看来似乎是在asp.net 页面中使用HTML 注释仍然会导致代码被执行,所以通过上面的代码输出的HTML代码可能是:
<!-- <p>Hello</p> -->
使用服务端的注释就不会导致这个问题:
<%-- <my:control runat="server" /> --%>
像期望的一样,它不会输出任何HTML。
13:PHP form processing: the easy way
一个coworker邀请我帮他弄下他的第一个网站(也许是最后一个?)
这是一个真实的房产代理的项目,
他尝试着处理来自搜索表单中的选项,例如(price range, state size, zone location, bla, bla.. )
所有的代码都f***ing 糟透了,但是下面的这一段比其他的代码更给力。
14:完美的老师(The perfect teacher )
class Person
{
private string name;
private string sname;
public Person(string argName, string argSName)
{
name = argName;
sname = argSName;
}
public string getName()
{
return name;
}
public string getSName()
{
return sname;
}
public override string ToString()
{
return name + " " + sname;
}
}
现在考虑下下面几个事实:
-代码是用C#语言编写的,而不是Java.
-万一你没有注意到,这段代码违反了一些基本的C#编码规范。
-写这段代码的人现在正在大学教C#
-这段代码是他给学生的一个“像这样敲代码 “的例子
-他从来没有使用过属性(Java风格的getXXX() 和setXXX()方法可能他更要擅长一点)
-他有时候会使用捷克语来命名类和方法(当然,它可以编译通过,因为VS是基于Unicode的,但是…WTF?)
-还有更多!