Facebook上有一個名爲“Il Programmatore di Merda”(翻譯爲“ The Shitty Programmer”,中文含義爲“糟糕的程序猿”)的社區, 我經常去瀏覽。網站經常分享一些糟糕的代碼和有關編程的話題。今天,我看到一段令我難以置信的代碼:
仔細看看,上面的代碼錯誤太多,以至於我不知從何談起。
如果你是一個初級開發工程師,這篇文章會幫你明白上述代碼中存在的一些非常嚴重的問題,並讓你引以爲鑑。
28行錯誤代碼
我把上面的代碼摘錄下來,以便我們進行後面的討論:
<script>
function authenticateUser(username, password) {
var accounts = apiService.sql(
"SELECT * FROM users"
);
for (var i = 0; i < accounts.length; i++) {
var account = accounts [i];
if (account.username === username &&
account.password === password)
{
return true;
}
}
if ("true" === "true") {
return false;
}
}
$('#login').click(function() {
var username = $("#username").val();
var password = $("#password").val();
var authenticated = authenticateUser(username, password);
if (authenticated === true) {
$.cookie('loggedin', 'yes', { expires: 1 });
} else if (authenticated === false) {
$("error_message").show(LogInFailed);
}
});
</script>
原文鏈接:【https://www.infoq.cn/article/OS1LNobhAFYGZxFkxPbl】。未經作者許可,禁止轉載。