函数命名:sendCharacter 还是 receiveCharacter?

发布于 2024-08-31 12:29:44 字数 746 浏览 6 评论 0原文

我试图命名一个在对象接收到字符时运行的函数。

对于调用者来说,它应该被命名为sendCharacter,这样它就可以调用:

object->sendCharacter( character ) ;

对于调用者来说这看起来不错..但是对于接收者来说,它实现了一个方法

/// Called when this object is do something
/// with a character
/// from the caller
void sendCharacter( char c ) ;

所以对于接收者类来说,它看起来像这样方法实际上会发送一个字符,而不是接收一个字符。

那么,我可以调用函数 receiveCharacter

/// Called when this object is do something
/// with a character
/// from the caller
void receiveCharacter( char c ) ;

但现在调用者这样做:

object->receiveCharacter( character ) ;

这看起来很奇怪。

我怎样才能更好地命名这个函数?

I'm trying to name a function that runs when a character is received by the object.

For the caller, it should be named sendCharacter, so that it can call:

object->sendCharacter( character ) ;

That looks nice for the caller.. but for the receiver, it implements a method

/// Called when this object is do something
/// with a character
/// from the caller
void sendCharacter( char c ) ;

So for the recipient class, it looks like this method will actually send a character out, not receive one.

So then, I could call the function receiveCharacter

/// Called when this object is do something
/// with a character
/// from the caller
void receiveCharacter( char c ) ;

But now the caller does this:

object->receiveCharacter( character ) ;

Which just looks odd.

How can I better name this function?

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

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

发布评论

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

评论(3

暗地喜欢 2024-09-07 12:29:44

也许这只是您选择的示例,但 sendCharacterreceiveCharacter 都没有意义。 为什么要向对象发送一个字符?

更好的选择是选择一些指示为什么发送字符的内容 - 例如,appendCharacter

选择一个特定的“追加”示例:C# 和 Java 都有一个 StringBuilder,它有一个 Append(string) 方法。当然,您将字符串发送StringBuilder的实例,而StringBuilder正在接收该字符串,但这不是重点。要点是字符串被附加到其内部缓冲区/数组/实现细节中。

Maybe it's just the example you chose, but sendCharacter and receiveCharacter are both meaningless. Why are you sending a character to the object?

A better choice would be to pick something that indicates why the character is being sent -- for instance, appendCharacter.

Picking a specific "append" example: C# and Java both have a StringBuilder that has an Append(string) method. Sure, you're sending the string to the instance of StringBuilder, and the StringBuilder is receiving the string, but that's not the point. The point is that the string is appended to its internal buffer/array/implementation detail.

别忘他 2024-09-07 12:29:44

void processCharacter(char c)
{...}

我很高兴看到有人为方法名称而烦恼。我花了太多时间这样做;)

void processCharacter(char c)
{...}

I am glad to see someone else who agonizes over method names. I spend too much time doing that ;)

爱本泡沫多脆弱 2024-09-07 12:29:44

如果方法对角色事件做出反应,那么怎么样

void onCharacter(char c);

If the method reacts on character events, then how about

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