重构的第一步就是读代码,从中发现其中的坏味道。在最近的一个重构项目中就发现如下的一些不合理点并在阅读过程中考虑了初步的解决方案。
1 控件创建和配置代码
– (void)loadView
{
[super loadView];
CGRect rect = CGRectMake(1, 1, 318, 34);
_urlField = [[UITextField alloc] initWithFrame:rect];
_urlField.borderStyle = UITextBorderStyleBezel;
_urlField.textColor = [UIColor blackColor];
_urlField.delegate = self;
_urlField.text = FIELD_TEXT;
_urlField.backgroundColor = [UIColor whiteColor];
//_urlField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_urlField.returnKeyType = UIReturnKeyGo;
_urlField.keyboardType = UIKeyboardTypeURL;
_urlField.autocapitalizationType = UITextAutocapitalizationTypeNone;
_urlField.autocorrectionType = UITextAutocorrectionTypeNo;
_urlField.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:_urlField];
}
建议:IB、配置文件
2 死代码
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
建议:要么删掉,要么不用模板直接手动编写
3 不可能复用的UITableCell
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
CellIdentifier = [NSString stringWithFormat:@"Cell_%d_%d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
??
}
建议:这种复用等于没有复用
4 不利用模型的“口算”数
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
建议:有个dataSource的model,可以直接通过[model count]获取,而自己写意味着实际是一份的数据硬是做成两份,网站空间,需要解决同步问题。口算解决不了问题。
5 拼写或者语法错误
– (void)showStatue; // showStatus
– (BOOL)isAllowWifi; // isWifiAllowed
建议:不确定的时候多查查字典,并且经常阅读自己的代码
6 多余空行
– (void)loadView
{
[super loadView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:imageBt];
self.navigationItem.leftBarButtonItem = item;
[item release];
[imageBt release];
}
建议:删除最后一行的空行
7 简单罗列判断
– (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
int rows = 0;
switch (section) {
case 0:
rows = 1;
break;
case 1:
rows = 2;
break;
case 2:
rows = 3;
break;
case 3:
rows = 1;
break;
default:
break;
}
return rows;
}
建议:建立一个模型,代码比配置文件贵的多
8 奇怪的命名
@interface PasswordSettingView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
建议:UITableViewController继承的应该是Controller,不是View
9 类中定义的静态函数
static void MsgBox(NSString *info)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:info
message:nil
delegate:nil
cancelButtonTitle:_(@"OK")
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
建议:要不放在公用文件中,网站空间,要不作为类的一个私有成员函数,总之不要夹杂在类实现中。
10 无所事事函数
– (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
}
建议:什么都不干的函数。白领饷了
11 多此一举函数
– (void)loadView
{
[super loadView];
}
建议:写了等于没写。所以不要写。
12 正话反说的判断
– (BOOL)checkItem:(Item *)item
{
if (!item || !item.teleplayName || !item.introduction || !item.collectName
|| !item.actor || !item.url || !item.extName)
{
return NO;
}
return YES;
}
人生没有彩排,每一天都是现场直播