Why I don't use the "else" keyword in my code anymore
Video Statistics and Information
Channel: Nick Chapsas
Views: 157,484
Rating: 4.5352082 out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, Why I don't use the else keyword anymore, the else keyword, don't use the else keyword, else, don't use else, code, kotlin, java, python, javascript, best practices, dotnet, .net
Id: _ougvb8mT7k
Channel Id: undefined
Length: 20min 40sec (1240 seconds)
Published: Thu Feb 11 2021
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
Thank god for this video. More people need to use guard clauses in their code. It's one of the easiest and biggest improvement someone can make to the readability of their code.
Reading code where guards are not used always cripples the mind.
tl;dw?
This a double edged word, just like everything else
And this is good example of Open-closed Principle in action :)
It's quite interesting reading this thread, a good few people are claiming that nested if else's are good but I think he really most people would consider it a code smell and that this way something funny lies.
Returning early is a good habit, if I have nothing else to do in this function why hang around and cause potential side effects. There was an argument against returning early back in C that has managed to linger but I don't believe modern languages need to worry.
The above being said, if a single method has lots of return statements then it may be a sign something is off about your design and you may need to add another layer because many returns probably means you are doing lots of Ifs which just causes the testing problems from above.
The approach the author takes in his video is sensible, push decisions down the tree, he has created a mechanism to handle each levels responsibility and made it extensible and there is one more level to go where you would shift recipes out to their own class and interface so a recipe book becomes a mechanism for finding a recipe but does not in itself know what goes into a recipe or how to prepare it. The problem is such a trivial example you do think it overkill but apply it to a real world problem and you will soon see benefits especially when it comes to fixing bugs or adding new functionality.
If this goes far enough, we're going to have a revolution of "elseness" in code ten years from now. People will discover the else keyword and start writing 10 point coding principles around why the else is the height of code readability because it flows more naturally inline with the business logic and natural way the business thinks about processes.
Hm I am sorry but it is 20 minutes video about changing else to switch, I thought he will do some oo trick and remove if nd else
Personally i prefer the nested but shorter code. Finding short essentially meaningless methods and classes just makes the project seem much larger than it actually is and much more intimidating. Having all the code in one class with a very few methods makes it easier to grasp from an outsiders perspective. Essentially in the end, as I fold all methods anyways so I only need to read the method names and their documentation.
Also it kind of creates an sloc inflation. Its just my personal opinion and when im not working on personal projects, i tend to follow the style of the other developers.
This looks great and everything but it is waaayyy too advance for me lol