Register to get access to free programming courses with interactive exercises

Dispatch data by key JS: Polymorphism

Along with polymorphism, we often see the term “dynamic dispatch” appear frequently. We'll get to know the concept later, but now let's talk about dispatching in principle.

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

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

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

You'll find the concept of an “environment” in all projects. This is the environment in which the project runs. During development, the code runs in a development environment. The environment in which the application really works is called the production environment. Depending on the environment, applications may start up, be configured, and even run differently, for example, different databases may be used in different environments.

The code above is just 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, in which dispatching is done line by line, can easily be replaced with a switch. This 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 itself 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, it can be done 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. Conditional constructions are static code that cannot be changed without rewriting the program itself, unlike the previous variant. Data, on the other hand, is another matter. For convenience, they're often put in configuration files, which use the 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 itself. Its strengths are especially noticeable in situations where it's not our code that does the processing, but the code of a library or framework. After all, we can't just open the source code and fix it.


Hexlet Experts

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

The Hexlet support team or other students will answer you.

About Hexlet learning process

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:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

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.