不好的代碼

今天在檢視代碼察郁,發(fā)現(xiàn)一些問題,順便貼出來美旧,第一篇簡書文章

濫用guard

首先看一下官方對guard關(guān)鍵字的說明

Early Exit
A guard statement, like an if statement, executes statements depending on the Boolean value of an expression. You use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. Unlike an if statement, a guard statement always has an else clause—the code inside the else clause is executed if the condition is not true.”
摘錄來自: Apple Inc. “The Swift Programming Language (Swift 3 beta)”怀大。 iBooks. https://itun.es/us/k5SW7.l

而下面的例子并不是對異常情況進(jìn)行判斷,所以該場景下應(yīng)該使用ifswitch

guard orderData["workOrderType"].numberValue == OrderType.ServiceOrder.rawValue else {
    orderCell.orderType = .WorkOrder
    orderCell.configCell(orderData, indexPatch: indexPath)
    return orderCell
}

使用硬代碼(魔鬼數(shù)字)

cell.remarkLabel.preferredMaxLayoutWidth = (screenWidth - 320) + 304

代碼擠成一坨

下面的代碼有幾個問題

  • 代碼擠成一坨
  • 用代碼去生成按鈕(不推薦)
  • 用代碼去生成按鈕的目的實(shí)際上是想設(shè)置按鈕的顏色逼友,這個可能通過tintColor很方便地處理
let cancelButton = UIButton(type: .System)
cancelButton.frame = CGRectMake(0, 0, 42, 20)
cancelButton.setTitle(cancelButtonTitle, forState: .Normal)
cancelButton.titleLabel?.font = UIFont.boldSystemFontOfSize(16)
cancelButton.setTitleColor(UIColor.colorWithHexString("#29b6f6"), forState: .Normal)
picker.setCancelButton(UIBarButtonItem(customView: cancelButton))
let doneButton = UIButton(type: .System)
doneButton.frame = CGRectMake(0, 0, 50, 20)
doneButton.setTitle(doneButtonTitle, forState: .Normal)
doneButton.titleLabel?.font = UIFont.boldSystemFontOfSize(16)
doneButton.setTitleColor(UIColor.colorWithHexString("#29b6f6"), forState: .Normal)
picker.setDoneButton(UIBarButtonItem(customView: doneButton))

比較好的代碼應(yīng)該這樣(忽略selector的處理)

let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: nil)
cancelButton.tintColor = UIColor.colorWithHexString("#29b6f6")
picker.setCancelButton(cancelButton)

let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: nil)
doneButton.tintColor = UIColor.colorWithHexString("#29b6f6")
picker.setDoneButton(doneButton)

類型初始化有誤

下面代碼中的變量不可能為浮點(diǎn)型精肃,卻聲明為浮點(diǎn)型

var currentPage = 1.0
var pageSize = 10.0

毫無意義的重載

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
}

Bean使用復(fù)數(shù)

Bean是抽象的,不應(yīng)該為復(fù)數(shù)
public static class WorkOrdersBean

多層if嵌套

圖片來自網(wǎng)絡(luò)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末帜乞,一起剝皮案震驚了整個濱河市司抱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌黎烈,老刑警劉巖习柠,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匀谣,死亡現(xiàn)場離奇詭異,居然都是意外死亡资溃,警方通過查閱死者的電腦和手機(jī)武翎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來溶锭,“玉大人宝恶,你說我怎么就攤上這事∨客保” “怎么了垫毙?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拱绑。 經(jīng)常有香客問我综芥,道長,這世上最難降的妖魔是什么猎拨? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任膀藐,我火速辦了婚禮,結(jié)果婚禮上红省,老公的妹妹穿的比我還像新娘消请。我一直安慰自己,他們只是感情好类腮,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布臊泰。 她就那樣靜靜地躺著,像睡著了一般蚜枢。 火紅的嫁衣襯著肌膚如雪缸逃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天厂抽,我揣著相機(jī)與錄音需频,去河邊找鬼。 笑死筷凤,一個胖子當(dāng)著我的面吹牛昭殉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播藐守,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼挪丢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了卢厂?” 一聲冷哼從身側(cè)響起乾蓬,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎慎恒,沒想到半個月后任内,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體撵渡,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年死嗦,在試婚紗的時候發(fā)現(xiàn)自己被綠了趋距。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡越除,死狀恐怖棚品,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情廊敌,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布门怪,位于F島的核電站骡澈,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏掷空。R本人自食惡果不足惜肋殴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望坦弟。 院中可真熱鬧护锤,春花似錦、人聲如沸酿傍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赤炒。三九已至氯析,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間莺褒,已是汗流浹背掩缓。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留遵岩,地道東北人你辣。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像尘执,于是被迫代替她去往敵國和親舍哄。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容