Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 3, 2021 06:20 pm GMT

Why we shouldn't use "else"

I have recently watched several videos and read many posts about the misuse of 'else' and why we should avoid it. At first I thought 'well, it will be the
novelty and they will forget about this in a few months'. To my surprise it is not like that, and they have been dealing with this subject for a long time.

One can say 'and why would I stop using' else 'if they are indispensable in any program', and you are absolutely right, I thought the same thing until I think I have come to understand it. I am going to put an example; Let's say we have to do three checks when registering a user and each of these checks has an exception attached if it is not true:

  1. Check that the username follows a correct format.
  2. Check if the user is already registered.
  3. Check that the user has a correct image

It is not a real case but it can help us. Ok, to do these checks we would have to do the following

carbon.png

Seems like everything is ok right? Or not? Leaving aside the three levels of indentation, it could be a valid solution. The problem is that if we have a lot of code within each check, when we get to the 'else' we won't even remember what we're checking. What not using 'else' proposes is to put it in a different way, something like this:

carbon (1).png

Doing it this way, we would check from the first moment if we can go ahead with the registration, and then do the logic if everything goes well. Without a doubt, in the second way everything seems to be more organized and clear when reading code.


Original Link: https://dev.to/dd8888/why-we-shouldn-t-use-else-279c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To