Function naming: sendCharacter or receiveCharacter?
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 )开发者_如何学JAVA ;
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?
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.
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
void onCharacter(char c);
精彩评论