An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
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:
- Check that the username follows a correct format.
- Check if the user is already registered.
- 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
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:
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
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To