函数命名:sendCharacter 还是 receiveCharacter?
我试图命名一个在对象接收到字符时运行的函数。
对于调用者来说,它应该被命名为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这只是您选择的示例,但
sendCharacter
和receiveCharacter
都没有意义。 为什么要向对象发送一个字符?更好的选择是选择一些指示为什么发送字符的内容 - 例如,
appendCharacter
。选择一个特定的“追加”示例:C# 和 Java 都有一个
StringBuilder
,它有一个Append(string)
方法。当然,您将字符串发送到StringBuilder
的实例,而StringBuilder
正在接收该字符串,但这不是重点。要点是字符串被附加到其内部缓冲区/数组/实现细节中。Maybe it's just the example you chose, but
sendCharacter
andreceiveCharacter
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 anAppend(string)
method. Sure, you're sending the string to the instance ofStringBuilder
, and theStringBuilder
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.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 ;)
如果方法对角色事件做出反应,那么怎么样
If the method reacts on character events, then how about