Stop using if-else! #developer #javascript #programming #softwareengineer #shorts #short

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Yes, very useful... Combine this technique with the design pattern for simplifying conditional by using polymorphism. I'm eager to use this approach.

TarekFaham
Автор

What about if/return statements?

function run(action) {
if (action === 'created') return afterCreate( );
if (action === 'updated') return afterUpdate( );
If (action === 'destroyed') return afterDestroy( );
}

Dev-Siri
Автор

First, this is not a map it's an object. Recreating a map on every call is bad performance wise and memory wise. You could use a
switch(action) {
case 'created': createdAction(); break;
...
}
Thats no memory overhead no computation overhead, single hash calculation. Optimized compilation. Not a single else.

haraldgame