There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

eth0p ,

Circular dependencies can be removed in almost every case by splitting out a large module into smaller ones and adding an interface or two.

In your bot example, you have a circular dependency where (for example) the bot needs to read messages, then run a command from a module, which then needs to send messages back.

<pre style="background-color:#ffffff;">
<span style="color:#323232;">    v-----------
</span><span style="color:#323232;">  bot    command_foo
</span><span style="color:#323232;">    -----------^
</span>

This can be solved by making a command conform to an interface, and shifting the responsibility of registering commands to the code that creates the bot instance.

<pre style="background-color:#ffffff;">
<span style="color:#323232;">    main <---
</span><span style="color:#323232;">    ^        
</span><span style="color:#323232;">    |          
</span><span style="color:#323232;">    bot ---> command_foo
</span>

The bot module would expose the Bot class and a Command instance. The command_foo module would import Bot and export a class implementing Command.

The main function would import Bot and CommandFoo, and create an instance of the bot with CommandFoo registered:

<pre style="background-color:#ffffff;">
<span style="color:#323232;">// bot module
</span><span style="color:#323232;">export interface Command {
</span><span style="color:#323232;">    onRegister(bot: Bot, command: string);
</span><span style="color:#323232;">    onCommand(user: User, message: string);
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">// command_foo module
</span><span style="color:#323232;">import {Bot, Command} from "bot";
</span><span style="color:#323232;">export class CommandFoo implements Command {
</span><span style="color:#323232;">    private bot: Bot;
</span><span style="color:#323232;">
</span><span style="color:#323232;">    onRegister(bot: Bot, command: string) {
</span><span style="color:#323232;">        this.bot = bot;
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">
</span><span style="color:#323232;">    onCommand(user: User, message: string) {
</span><span style="color:#323232;">        this.bot.replyTo(user, "Bar.");
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">// main
</span><span style="color:#323232;">import {Bot} from "bot";
</span><span style="color:#323232;">import {CommandFoo} from "command_foo";
</span><span style="color:#323232;">
</span><span style="color:#323232;">let bot = new Bot();
</span><span style="color:#323232;">bot.registerCommand("/foo", new CommandFoo());
</span><span style="color:#323232;">bot.start();
</span>

It’s a few more lines of code, but it has no circular dependencies, reduced coupling, and more flexibility. It’s easier to write unit tests for, and users are free to extend it with whatever commands they want, without needing to modify the bot module to add them.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines