Stop using ELSE statements - Try this instead
HTML-код
- Опубликовано: 3 окт 2020
- Always use a guard clause instead of ELSE statements. The other day I came across a Medium article that said - STOP USING ELSE statements in your programs. I did some research and found some other articles having similar content.
In this video, I'm explaining why you should always try to avoid using else statements in your code. You can always replace them with Guard clauses. Watch the video to find out more. I will be posting another video with a technique explaining how we can avoid If-conditions as well.
References
------------------
/ why-you-need-to-stop-u...
en.wikipedia.org/wiki/Guard_(...)
/ coding-tip-try-to-code...
/ java-write-code-thats-...
/ how-to-prevent-using-i...
/ dont-use-else
/ stop-using-if-else-sta...
Music credits: www.bensound.com/
If you enjoyed watching this video, please consider to like and subscribe as well, so I can keep working on more videos like this. Have a nice day! Наука
I'll give this a thumbs up even before watching it. I've been using guard clauses for nearly 20+ years, and they really clean up conditional logic. They're particularly useful when used as precondition checks, so by the time you get to the core of some logic, you know the exact state of the data. Guard clauses => easier to read, easier to test, more bulletproof code!
Absolutely! (Y)
thank you for this video I'm learning JS and this is very helpful
Thank you. I'm glad to know this helped :)
Interesting, but doesn’t allow for factored logic (which is acknowledged in the video).
You could do the same thing. In a try-except, raising a benign exception to hop out of a stack of non-nested if statements.
Informative indeed 👏
Thank you :)
Informative!! hope more of this kind👍
Thank you. :) Working on more like this.
@@fncoder Hi sorry, I just wrote and asked for help and your email but I literally just figured out what I was doing wrong. Greatly appreciate you responding
@@IronReef77 no problem man. And well done 👍
In practice I don't find the need to use else statements in most cases. A lot of if statements are meant to exit the code early. I tend to keep nesting to a minimum because it makes code more readable.
I also find out in a recent project I could remove some guard clauses that would throw in the beginning of a method by putting the error checking code inside the input object. It was an immutable object and error checking occured in the constructor. That made it very clean.
Guard clauses are one of those things - once you know, you know - it becomes really hard to use else statements.
That constructor level check you mentioned sounds cool, man! Do you have a sample code for something like that?
@@fncoder Sure, I built a web crawler in Java for an interview project and was using the URI class from the standard library for representing pages to be crawled. But there was a lot of code using it so I created a dedicated domain class for it, and it only accepted the right types of URLs. This meant that I didn't have to repeat validation for all the necessary conditions in multiple places.
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
public class Page {
private final URI uri;
public Page(URI url) {
this.uri = getValidatedURI(url);
}
public Page(String url) {
this.uri = getValidatedURI(url);
}
private URI getValidatedURI(String url) {
try {
return getValidatedURI(new URI(url));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("URL " + url + " fails to satisfy RFC 2396", e);
}
}
private URI getValidatedURI(URI url) {
if (!url.isAbsolute())
throw new IllegalArgumentException("URL " + url + " is not absolute");
if (!url.getScheme().equalsIgnoreCase("http") && !url.getScheme().equalsIgnoreCase("https"))
throw new IllegalArgumentException("URL " + url + " does not have a HTTP/HTTPS scheme");
try {
return new URI(url.getScheme(), url.getHost(), url.getPath(), null);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Failure to remove fragment from URL " + url);
}
}
public URI uri() {
return uri;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Page page = (Page) o;
return uri.equals(page.uri);
}
@Override
public int hashCode() {
return Objects.hash(uri);
}
@Override
public String toString() {
return uri.toString();
}
@@lucasdarianschwendlervieir3714 Thanks man! 👍
so, guard clauses mean writing a bunch of if condition without nesting it, right?
Hi Asifur, that's correct. Rephrasing your logic such that the if conditions themselves return without the need of any else statements.
Pretty Instructive👌
Thank you :)
Superb
Thank you :)
Please what is that beautiful font I see?
Sorry about the delayed response. Do you mean the font on the thumbnail of this video?
@@fncoder Sorry if you misunderstood, I meant the font used in the code😅
👍
Thank you :)
Video could have been 90 secs
No I like using else
Alright! I'll allow it
冗長だし新しくもない
What does this mean? :)
Total stupidity... No way you can avoid using else. To avoid the if statements hell you need to use decision tables approach.
That's a wrong statement. You can avoid else statements in most cases, like in the example mentioned in this video.
I advise you try to do some research before calling something stupid mate, don't be a victim of the Donning Kruger effect
I’ve been programming for over 35 years and hardly ever write an else statement.