May I propose a refactor for the handle method? You could save some duplications by flipping the logic. If you first ask for if the user is not logged in, redirect to login you catch this case once and can skip it for everything that follows. Also, instead of else…else, you can simply use if…if. If a case proves true, you return something and the function stops, so you do not need an else. This reduces cognitive load.
A Software Developer who is geared towards building high-performing and innovative products following best practices. I help you become a great Backend Engineer @ https://masteringbackend.com
A Software Developer who is geared towards building high-performing and innovative products following best practices. I help you become a great Backend Engineer @ https://masteringbackend.com
Yes, switch would cut some duplicate lines. I would still propose the lookup array pattern as recommended in this extra comment as it is a lot easier to add and cut arguments and is super easy to read.
Yes, but there still would be lots of repetitions even in short-form. Maybe I can write an own article soon about possible refactorings so that people can learn and compare. The original poster went with the simple if solution but there are multiple ways to make it shorter and easier to maintain.
May I propose a refactor for the
handle
method? You could save some duplications by flipping the logic. If you first ask forif the user is not logged in, redirect to login
you catch this case once and can skip it for everything that follows. Also, instead ofelse
…else
, you can simply useif
…if
. If a case proves true, you return something and the function stops, so you do not need anelse
. This reduces cognitive load.This is great, thanks for your contribution, I will refactor immediately.
Updated now..
What about switch statement?
Yes,
switch
would cut some duplicate lines. I would still propose the lookup array pattern as recommended in this extra comment as it is a lot easier to add and cut arguments and is super easy to read.These if statements could also be one-liners to save some more characters and space.
Yes, but there still would be lots of repetitions even in short-form. Maybe I can write an own article soon about possible refactorings so that people can learn and compare. The original poster went with the simple
if
solution but there are multiple ways to make it shorter and easier to maintain.Indeed.
Tell me where can I start a new discussion?
Just put a comment on the root of this article: dev.to/kaperskyguru/multiple-role-...