after being a faithful user of Stackoverflow for a number of years, I've finally decided to write my own question, so I apologize if I've missed something.
I tried searching for this topic, but wasn't entirely sure how to word it.
I am programming in Java, and am writing a plugin for a Minecraft server. I want to make this plugin responsive to user input, so I'm sending a great variety of messages to players, including success messages, help messages, errors, etc. Sending a message to a player can be achieved through a simple method in the player object: player.sendMessage(String message);
However, I don't want to simply use that method to directly send the message in every instance, but rather I'd prefer to store all the messages in one location, in order to make the code easier to manage in the event of a typo or a decision to change the text.
My current implementation of this is a class called Messenger. For each message that can be sent to players, it contains a public void method that accepts a player as an argument, as well as specific information relative to the message, in order for it to be highlighted and included in the message.
A hypothetical example would look like this:
public void playerPosition(Player player, int x, int y, int z) {
player.sendMessage("Your coordinates are: "+x+", "+y+", "+z+".");
}
However, I'm still new to Java and am doing my best to implement best practices and efficient designs. I know there's a better way to handle this situation and maintain a high level of readability and maintainability.
These methods would be static, as I read that static methods have a smaller memory footprint, but some of them reference non-static variables in the Messenger class, in order to convey some dynamic information from other sources to the player.
I'm going to rework that so that all the information needed will be passed directly to the methods, so they can be static.
However, I got to wondering: Is it more efficient to generate the results of the message every time the method is called, or should I store the content of the message in a string and simply return that string? Is the compiled code of a method faster and more efficient than returning a stored string?
If returning a stored string were faster, I could simply store the string with argument placeholders for later use by String.format();
I hope this wasn't too lengthy for my ultimate question, but I hope that I might also be led in the paths of programming righteousness in regards to my approach as well. That said, if you see something I should do differently, or indications of a fundamental misunderstanding I might be experiencing, please, let me know! You're all great, thanks!
I think you're asking if it's faster to build the string each time playerPosition is called (as in your example) or to store the string each time x
, y
, or z
is updated, and return that each time playerPosition
is called.
This depends on which operation happens more frequently. You want the string building to happen least often.
If you update the coordinates more frequently than getting the position, then build the string in playerPosition
. If you query the player position more frequently than you update it, then build the string in updateCoordinates
(or whatever equivalent method you have).
For example, if this is for a 2D platformer game with graphics, you will be querying the player position constantly, whereas the position will only be updated on a keypress; therefore you would want the String to be built in the updateCoordinates
method.