Register to get access to free programming courses with interactive exercises

Dispatch data by key JS: Polymorphism

We often see the term dynamic dispatch along with polymorphism. We will get to know the concept later, but now let us discuss dispatching in principle.

Dispatching is the process of coordinating any action. For example, a dispatcher at an airport coordinates planes, and a dispatcher in a cab service connects available drivers with customers.

Consider some conditional code in which the selection of execution branches relies on a specific variable value:

let databaseConfiguration;
if (env === 'development') {
  databaseConfiguration = {
    adapter: 'sqlite',
  };
} else if (env === 'production') {
  databaseConfiguration = {
    adapter: 'postgresql',
  };
}

You will find the concept of an environment almost everywhere. It is the environment in which the project runs.

During development, the code runs in a development environment. The environment where the application works is called the production environment. Depending on the environment, applications may start up, be configured, and even run differently. For example, we can use different databases in different environments.

The code above is a typical example of how the configuration changes depending on the environment. The process of selecting something can be considered a dispatching process.

The static conditional design, where dispatching is done line by line, can easily be replaced with a switch. It makes the code clearer and easier to understand:

let databaseConfiguration;

switch (env) {
  case 'development':
    databaseConfiguration = {
      adapter: 'sqlite',
    };
    break;
  case 'production':
    databaseConfiguration = {
      adapter: 'postgresql',
    };
    break;
}

Although there is a bit more code, the switch defines the dispatching process more clearly. But you can go even further and carry out dispatching by keys in associative arrays:

const databaseSettingsByEnv = {
  development: {
    adapter: 'sqlite',
  },
  production: {
    adapter: 'postgresql',
  },
};

const databaseConfiguration = databaseSettingsByEnv[env];

The default value is easy to add. We can do it using the ?? operator:

const databaseConfiguration = databaseSettingsByEnv[env] ?? { adapter: 'memory' };
// Either through _.get
// const databaseConfiguration = _.get(databaseSettingsByEnv, env, { adapter: 'memory' });

This option is better than the previous two for several reasons. First, it's shorter, and second, it's more flexible. The conditional constructions are static codes. It means we cannot change it without rewriting the program. On the other hand, data is another matter. Usually, we can find it in configuration files with JSON or YAML formats. For example:

---

development:
  adapter: sqlite

production:
  adapter: postgresql

This approach makes it easy to add new behavior without changing the application code. Its strengths are especially noticeable when we use a third-party library or framework for processing. After all, we can't just open the source code to fix it.


Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.