So I'm not sure if this is the right topic but it seems to kinda fit so I figured I'd try. Especially interested in @Thenomain and @Lithium's thoughts since we were talking about making the next-gen servers easier to learn.
Ares plugins have Command Handlers - each responsible for a single command. You can think of them as the equivalent of &CMD-FOO attributes from Penn/Tiny/Rhost land.
Now the easiest and most straightforward thing would be to have command handlers implement a single method to do everything.
Side note: Ares uses Ruby, but what I have below is more psuedocode showing the general idea for simplicity. I want to focus on the overall flow, not the actual code.
class PageCmd
def on_command(client, cmd)
# Figure out the parameters, like *=*
recipients = cmd.args.arg1
message = cmd.args.arg2
# Do error checks
if (a recipient is not valid)
client.emit "Don't know who you're trying to page."
return # do not continue
end
# Emit the page to everyone
recipients.each { emit page to recipient }
client.emit page message
The pseudocode there is pretty short, but in reality it would be a bit longer.
Now, in the current version of Ares I tried to make the coder's life easier by breaking this process up into three distinct steps with three distinct Ruby methods: crack parses the parameters, checks do the error checking and abort if there's a problem, handle executes the command if there are no problems.
class PageCmd
# Define member variables for your command parameters
# so they're available throughout the class
attr_accessor :recipients, :message
def crack
# Figure out the parameters, like *=*
recipients = cmd.args.arg1
message = cmd.args.arg2
end
# This is an error-check method, because it starts with check_
# If it returns nil, all is well. If it returns a message, that message gets
# emitted to the client and the command is aborted.
# You can have other check methods too checking different things
def check_recipients
if (a recipient is not valid)
return "Don't know who you're trying to page."
end
return nil # All is well
end
# All the error checks have passed, do the thing.
def handle
# Emit the page to everyone
recipients.each { emit page to recipient }
client.emit page message
end
I personally like the second example better. I think each step is broken out clearly. There's less repetition with the error check because the if (error) emit return
stuff is bundled up in the check method handling.
BUT is it too much for new devs to absorb? Functions that get auto-executed based on their names? Member variables? The emit/return stuff happening behind the scenes? I worry that in making it cleaner, it's actually made it harder to learn.
Thoughts?