启用/禁用或激活/停用的方法的命名约定

发布于 2024-10-13 13:03:01 字数 424 浏览 4 评论 0原文

我目前正在开发一个 Objective C 项目,并且有一种启用/禁用的方法,或者换句话说:激活/停用全局 HTTP 代理。我想知道这种方法的最佳命名约定是什么。该方法有一个 BOOL 参数来决定执行哪个操作,即:启用或禁用。这是一个与大多数编程语言相关的风格问题,我决定我会喜欢这个伟大社区在这个问题上的意见,这个问题由于某种原因困扰着我。

这样好吗?有什么更好或更清楚的吗?

-(BOOL) activate:(BOOL) theActivateFlag;

-(BOOL) enable:(BOOL) theEnableFlag;

调用 [proxyObj activate:NO] 或 [proxyObj enable:NO] 是否会被理解为停用代理?
谢谢 :)

I'm currently working on an objective c project, and have a method that enables/disables OR in another term: activates/deactivates a global HTTP proxy. I was wondering what is the best naming convention for such a method. The method has a BOOL argument to decide which operation to carry out, ie: enable OR disable. It is a style question which is relevant to most programming languages and i've decided I would love the input of this great community on this matter which is bothering me for some reason.

Is this good ? anything better or clearer ?

-(BOOL) activate:(BOOL) theActivateFlag;

or

-(BOOL) enable:(BOOL) theEnableFlag;

Will calling [proxyObj activate:NO] OR [proxyObj enable:NO] be understood as deactivating the proxy ?
Thx :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红焚 2024-10-20 13:03:02

Apple 在自己的类中使用 enabled 属性:

@property (nonatomic, getter = isEnabled) BOOL enabled;

这将创建以下 getter 和 setter:

- (BOOL)isEnabled {
    return enabled;
}

- (void)setEnabled:(BOOL)flag {
    enabled = flag;
}

或者您可以为您的代理提供一对 activatedeactivate< /code> 方法或 enabledisable 方法。

Apple makes use of an enabled property in their own classes:

@property (nonatomic, getter = isEnabled) BOOL enabled;

That creates the following getter and setter:

- (BOOL)isEnabled {
    return enabled;
}

- (void)setEnabled:(BOOL)flag {
    enabled = flag;
}

Or you can give your proxy a pair of activate and deactivate methods or enable and disable methods.

岛徒 2024-10-20 13:03:02

我喜欢 form 在我的代码中反映 function

在文件的顶部我会放置一个

#define DISABLED NO
#define ENABLED YES

并让我的函数成为

- (void)toggleProxyAs:(BOOL)state {
    // assuming enabled is property/instance variable
    enabled = state;
}

并将其用作

[myProxyObj toggleProxyAs:ENABLED];

这不是标准方式做事情的过程,但对我来说,它更清晰,并且不需要单独的激活器/去激活器。

I like form to reflect function in my code

At the top of the file I'd put a

#define DISABLED NO
#define ENABLED YES

and have my function be

- (void)toggleProxyAs:(BOOL)state {
    // assuming enabled is property/instance variable
    enabled = state;
}

and use it as

[myProxyObj toggleProxyAs:ENABLED];

This isn't the standard way of doing things, but for me it is clearer and eliminates the need for separate activators/deactivators.

行雁书 2024-10-20 13:03:02

我曾想过(经过思考并改变主意几次)...

-(BOOL) enableProxy:(BOOL)shouldEnable;

...会更有意义,尽管我会是第一个承认我偶尔会沉迷其中的人命名约定的暗坑令人绝望(墙上涂满了表达得很糟糕的函数和方法名称),所以也许我不适合回答这个问题。 :-)

I'd have thought (after thinking about it and changing my mind a few times)...

-(BOOL) enableProxy:(BOOL)shouldEnable;

...would make more sense, although I'd be the first to admit that I occasionally wallow in dark pits of naming convention despair (the walls daubed with badly expressed function and method names), so perhaps I'm not best placed to answer this. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文