方法的目标C命名约定执行操作并返回值 [英] Objective C naming convention for method performs an action and returns a value

查看:96
本文介绍了方法的目标C命名约定执行操作并返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行动作并返回值的方法.例如,从输入编号开始,它将更新类的输入历史记录,然后生成并返回输入记录.那我怎么命名这个方法呢?

I have a method that performs an action and returns a value. For example from the input number it will update class's input history, then generate and return an input record. So how do I name this method?

我认为甚至C都有这个问题,例如 fopen 函数确实会打开文件并返回处理程序.但是使用Objective C约定,似乎很难命名该方法.

I think even C has this problem, e.g. fopen function does open a file and return a handler. But with Objective C convention it seems difficult to name the method.

从苹果文档中获取可可名称约定以及本文的可可与爱他们都说:"如果某个方法执行一个动作,则第一个单词应表明该动作"和"如果一个方法返回一个值,则第一个单词通常表明将返回什么"

From apple document for cocoa name convention and also from this article cocoa with love They both said "If a method performs an action the first word should indicate that action" and "If a method returns a value the first word normally indicates what will be returned"

因此,如果我命名为callRecordFromNumber:(NSString*) number之类的名称,则仅指示返回的值,而不指示其执行的操作.如果我用updateCallRecordFromNumber:(NSString *) number之类的名称命名,则仅指示操作,而不指示返回的值.

So if I name something like callRecordFromNumber:(NSString*) number, it only indicates the returned value, not the action it performs. If I name something like updateCallRecordFromNumber:(NSString *) number, it only indicate the action, not the returned value.

顺便说一句,我确实考虑将其分为两种方法,但问题是新记录是在执行操作的同时生成的.像这样的代码:如果不存在,则创建一个新的,然后更新历史记录.实际的代码具有几种类型的历史记录(例如,未接来电历史记录,已拒绝来电历史记录,总历史记录),并且会针对自己的历史记录生成并更新记录.因此,将其分为2种方法将引入过多的重复代码.似乎不是一个很好的选择.

BTW, I do consider separate it into 2 methods, but the thing is the new record is generated alongside with the action is performed. The code like this: creating a new one if not exists, then update history record. And the actual code has several types of histories (say, missed call history, rejected call history, total history), and record is generated and updated for its own history. So separating it into 2 methods will introduce too many duplicated codes. Seems not a good option here.

Record *record = totalHistory[number];
if (record !=nil ) return record;
record = [[Record alloc] initWithNumber:number];
record.type = [self callType:number];
totalHistory[number] = record;
switch (record.type) {
    case -1: /*missed call*/ {
    missedHistory[number] = record;
    break; }
    case 0: /*normall call*/ {
    acceptedHistory[number] = record;
    break; }
    case 1: /*rejected call*/ {
    rejectedHistory[number] = record;
    break; }
    ...
}
return record.

----更新----

---- Update ----

下面的2个讨论也很有帮助.

Following 2 discussions are helpful too.

命名取值的函数

现在我正在使用这样的方法名称,例如 valueByPerformed .... recordByUpdatedCallHistoryFrom:

Now I am using the method name like this, valueByPerformed..., e.g. recordByUpdatedCallHistoryFrom:

罗迪(Loy):(我希望有个更好的名字!

Wordy :( I wish there was a better name!

----更新----

---- update ----

swift 3.0命名指南,例如动词vs名词命名约定,就像排序vs排序给了我一些关于这个问题的新思路.现在我确实认为 valueByPerformed 可能是个好名字:D

swift 3.0 naming guideline, e.g. Verbs vs Nouns Naming Convention, like sort vs sorted gave me some new thought about this question. Now I do think valueByPerformed is probably a good name :D

推荐答案

听起来好像您是在让其他一些代码的限制阻止您编写干净的代码.不要这样到底是什么在阻止您执行这样的操作...?

It sounds like you're letting the limitations of some other code prevent you from writing clean code. Don't let it. What exactly is stopping you from doing something like this...?

NSNumber *myID = @(123);
[self updateCallRecordWithID:myID];
Record *myRecord = [self callRecordWithID:myID];

- (void)updateCallRecordWithID:(NSNumber *)recordID {
    history[number] = [[Record alloc] initWithNumber:recordID];
}

- (Record *)callRecordWithID:(NSNumber *)recordID {
    return history[recordID];
}

这篇关于方法的目标C命名约定执行操作并返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆