In the game engine I work with (Unity), a base class for every game object has a method ‘SendMessage(“methodName”)’, which invokes specified method in every class of every object that is associated with it — if it has one.
Should I create a new interface with method “methodName” for every message that I can send that way to formalize this interaction, or will it be just additional work without any gain? It seems like a good thing to do, because that way I will be able to see what messages this class can receive from it’s declaration, but I can’t see what errors, for example, I can help avoid with this. And I didn’t see it used that way either — so may be it’ll just be a wasted effort after all.
Strict answer: YES, use interfaces! Real life answer: it may depend.
In general to use strings in such way is dangerous (What if method declaration will change? What if you type the method name in a wrong way? Do you have any security issue with it?). The answer to these questions is always: you’ll catch that problems at run-time instead of compile -time (and it means you may release something that doesn’t work even if it’s a kind of error that SHOULD BE found at compile-time). Moreover do not forget performances, using strings you have to use Reflection (I guess/hope you won’t have a big switch/case statement).
To write interfaces is tedious but 99% of times is the best way to keep your code clean and easy to maintain.
That said you MAY need to use that strings to invoke methods (I’m thinking, for example, about Java Agents or some kind of “dynamic” instantiation based on configuration or on messages content).
So I guess the right answer is IT DEPENDS. I think you should always use interfaces because of speed, error checking, maintenance and security issues (where applicable). SOMETIMES you may need to use textual messages but you should use them carefully and never because you’ll write less code (this is NEVER a good reason to choose one approach instead of another).
Addendum: with interfaces you may use a “services daemon” mechanism (based for example on IServiceProvider) to discover all bojects that implement a specific interface. Achieve the same thing using SendMessage() may be tricky (will you use a CanHandlMessage() method?) and error-prone because of duplication.
Example:
This is just an example (to explain what I mean with “service daemon”, see the function FindServices() which retrieve all objects that implements a given interface), I guess you wouldn’t use LINQ and enumerables in your high speed game. I think it’s much easy to understand than a SendMessage() with parameters and moreover it’s easy to update.