美文网首页
不好的代码

不好的代码

作者: azhunchen | 来源:发表于2016-08-25 21:19 被阅读68次

今天在检视代码,发现一些问题,顺便贴出来,第一篇简书文章

滥用guard

首先看一下官方对guard关键字的说明

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

而下面的例子并不是对异常情况进行判断,所以该场景下应该使用ifswitch

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

使用硬代码(魔鬼数字)

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

代码挤成一坨

下面的代码有几个问题

  • 代码挤成一坨
  • 用代码去生成按钮(不推荐)
  • 用代码去生成按钮的目的实际上是想设置按钮的颜色,这个可能通过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))

比较好的代码应该这样(忽略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)

类型初始化有误

下面代码中的变量不可能为浮点型,却声明为浮点型

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使用复数

Bean是抽象的,不应该为复数
public static class WorkOrdersBean

多层if嵌套

图片来自网络

相关文章

  • 不好的代码

    今天在检视代码,发现一些问题,顺便贴出来,第一篇简书文章 滥用guard 首先看一下官方对guard关键字的说明 ...

  • 如何写出不好的代码

    [toc] 如何写出不好的代码 这是一个从各个项目整理出来的代码,具有一定代表性。坏代码的产生,本篇暂时不深究原则...

  • python基础-注释

    注释注释的作用就在于提供代码的可读性但不是注释越多越好:没有注释的代码不好,写注释特别多的代码也不好 单行注释,注...

  • 写出不好代码的原因有什么

    有这么一种情况, 为什么上学的时候偏科, 我喜欢数理化, 但是让我背东西, 我完全不行. 原来就以为是偏科, 现在...

  • 2. 两数相加

    代码缺点 虽然效率不低, 但是可读性不好, 过于繁琐, 属于笨蛋代码, 需要简化.

  • 命名规范

    突然发现做了那么多年开发,竟然没真正规定过自己的代码规范,这样不好,不好。 常用的代码命名规则主要有四种: 匈牙利...

  • 《笨办法学Python》笔记14-----读写文件

    上节中的代码没有关闭文件,这样并不好。 一、代码 from sys import argvscript, file...

  • 基于GitLab 创建cocoapods库

    脑子不好,需要记录 概念 1.code repository 代码仓库,主要存放我们提交的代码2.spec rep...

  • 单链表的基本操作

    按C语言代码编写 节点 链表的创建 输出 查找 修改 删除 排序 测试代码 C++代码 第一次写博客,有什么不好的...

  • 面向对象实现放大镜效果

    代码不好放上来。所以自己用图片 完整方法

网友评论

      本文标题:不好的代码

      本文链接:https://www.haomeiwen.com/subject/aqmasttx.html