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!
  • НаукаНаука

Комментарии • 36

  • @bits-of-bass
    @bits-of-bass Год назад +12

    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!

    • @fncoder
      @fncoder  Год назад +1

      Absolutely! (Y)

  • @delcarmat
    @delcarmat 2 года назад +1

    thank you for this video I'm learning JS and this is very helpful

    • @fncoder
      @fncoder  2 года назад

      Thank you. I'm glad to know this helped :)

  • @johnnyragadoo2414
    @johnnyragadoo2414 Месяц назад

    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.

  • @sreerajpr
    @sreerajpr 3 года назад +2

    Informative indeed 👏

    • @fncoder
      @fncoder  3 года назад

      Thank you :)

  • @jithvishnu1
    @jithvishnu1 3 года назад +2

    Informative!! hope more of this kind👍

    • @fncoder
      @fncoder  3 года назад

      Thank you. :) Working on more like this.

    • @IronReef77
      @IronReef77 Год назад +1

      @@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

    • @fncoder
      @fncoder  Год назад +2

      @@IronReef77 no problem man. And well done 👍

  • @lucasdarianschwendlervieir3714
    @lucasdarianschwendlervieir3714 Год назад +1

    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.

    • @fncoder
      @fncoder  Год назад +1

      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?

    • @lucasdarianschwendlervieir3714
      @lucasdarianschwendlervieir3714 Год назад

      @@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();
      }

    • @fncoder
      @fncoder  Год назад +1

      @@lucasdarianschwendlervieir3714 Thanks man! 👍

  • @asifurrahman5436
    @asifurrahman5436 2 года назад +2

    so, guard clauses mean writing a bunch of if condition without nesting it, right?

    • @fncoder
      @fncoder  2 года назад +2

      Hi Asifur, that's correct. Rephrasing your logic such that the if conditions themselves return without the need of any else statements.

  • @reshmaraveendran7674
    @reshmaraveendran7674 3 года назад +2

    Pretty Instructive👌

    • @fncoder
      @fncoder  3 года назад +1

      Thank you :)

  • @musicwithsoorajnairhs
    @musicwithsoorajnairhs 3 года назад +1

    Superb

    • @fncoder
      @fncoder  3 года назад +1

      Thank you :)

  • @zayne-sarutobi
    @zayne-sarutobi Год назад +2

    Please what is that beautiful font I see?

    • @fncoder
      @fncoder  Год назад

      Sorry about the delayed response. Do you mean the font on the thumbnail of this video?

    • @zayne-sarutobi
      @zayne-sarutobi Год назад

      @@fncoder Sorry if you misunderstood, I meant the font used in the code😅

  • @karthikgopakumar4363
    @karthikgopakumar4363 3 года назад

    👍

    • @fncoder
      @fncoder  3 года назад

      Thank you :)

  • @securethebag1613
    @securethebag1613 4 месяца назад +1

    Video could have been 90 secs

  • @djtomoy
    @djtomoy 8 месяцев назад +1

    No I like using else

    • @fncoder
      @fncoder  7 месяцев назад

      Alright! I'll allow it

  • @alij5668
    @alij5668 Год назад

    冗長だし新しくもない

    • @fncoder
      @fncoder  Год назад

      What does this mean? :)

  • @TarekFaham
    @TarekFaham 2 года назад +2

    Total stupidity... No way you can avoid using else. To avoid the if statements hell you need to use decision tables approach.

    • @fncoder
      @fncoder  2 года назад +4

      That's a wrong statement. You can avoid else statements in most cases, like in the example mentioned in this video.

    • @zayne-sarutobi
      @zayne-sarutobi Год назад +3

      I advise you try to do some research before calling something stupid mate, don't be a victim of the Donning Kruger effect

    • @larsp5109
      @larsp5109 2 месяца назад

      I’ve been programming for over 35 years and hardly ever write an else statement.