void Character::handleInput(const std::string & line)
{
lua_State * state = gTheWorld->getLuaState();
// push the actor input function onto the state
if (!Character::getInputFunction(state)) {
return;
}
// push the actor binding and the input line
if ( !pushBinding(state) )
{
gTheWorld->LogBugString("Error pushing character binding");
return;
}
lua_pushstring(state, line.c_str());
int retval = lua_pcall(state, 2, 0, 0);
if (retval != 0)
{
gTheWorld->LogBugString("Error in Lua actor input handling:");
gTheWorld->LogBugString(lua_tostring(state, -1));
lua_pop(state, 1); // pop the error message
return;
}
// All done.
}
movementShift = {
"\x1b movementShift = {
"\x1b[A": ( 0,-1),
"\x1b[B": ( 0, 1),
"\x1b[C": ( 1, 0),
"\x1b[D": (-1, 0),
}.get(keycode, None)
if movementShift:
# Do something.[/code]And very rarely see anyone rely on catching KeyError.
movementShift = {
"\x1b movementShift = {
"\x1b[A": ( 0,-1),
"\x1b[B": ( 0, 1),
"\x1b[C": ( 1, 0),
"\x1b[D": (-1, 0),
}.get(keycode, None)
if movementShift:
# Do something.[/code]And very rarely see anyone rely on catching KeyError.[/quote]
Well, that works when you don't need to distinguish between things being in the map or not. Perhaps it's just a style thing; most of the code I've seen (not written by myself) makes extensive use of KeyError. This [url=http://www.python.org/dev/peps/pep-0008/... guide for Python[/url] doesn't make explicit a suggestion on this point, however it does show the try/catch KeyError pattern I was talking about.
But your approach is also more efficient than first testing if something is in the map and then grabbing it (which entails two lookups).
(I say 'map' because this reasoning applies to any map type, not just dicts. Two lookups can be quite expensive on complex map types.)
Well, yes. But my rebuttal of being hand waved away was based on my working experience of, strangely enough, enterprise applications. And who says within a "gaming" genera that you may not have 5000 concurrent users?
Be careful using the word "should". Several paradigms you take for granted in day-to-day coding started out being used for things they "shouldn't" have been used for. Besides which, "should" is a very good indicator of "personal preference", and you completely ignored the question, which was "why is it less readable?", not "what should exceptions be used for?"
You are confusing a real world performance issue with a personal preference. I would argue that doing this is actually a wasteful operation and tbh I do not actually need to. The preference comes from knowledge, not from a whim.
Why is it less readable? Well, perhaps readable was the wrong word. I was on my mobile device and didn't feel like expanding on that anyway. I would actually say that throwing an exception can cause a jump in the code to some other, perhaps unrelated, area of code, thus disrupting program flow. If this happens several times in a row, throw catch, throw, catch, debugging or just grokking the code can become more complicated than it needs to.
An "exception use case" is any case one uses an exception to handle. That's it. You're back to what people "should" and "shouldn't" use them for, and that's personal preference.
Preference based on knowledge, not whimsy. "I feel like wearing fancy dress" is a whim. "I feel like wearing a coat, because its cold" is a preference based on the weather conditions. (Unless, of course, you wear warm fancy dress… in which case both conditions are covered :smirk:)
http://www.simonecarletti.com/blog/2010/...
That's only a ~1.4 second variance across 100,000 iterations. That's a dump-truck load of unopenable doors, my friend.
Now run those in parallel, with 5000 threads, and then we can talk.