Clean Code with Records, Sealed Classes and Pattern Matching by José Paumard

Поділитися
Вставка
  • Опубліковано 12 жов 2022
  • The releases of the JDK 17 to 19 bring interesting features in the Java language: sealed types, pattern matching for switch (as a preview feature). Along with the introduction of records, the implementation of pattern matching in the Java language begins to take shape. The JDK 19 brings even more with Record pattern matching (as a preview feature), that further simplifies your code. In this presentation we show you how records are working, how you can use them to make your code much more readable, and how you can leverage sealed types and switch expressions to improve the modularity of your application. You will see pattern matching in action on real examples, how it will change the way you write Java code, and what you can expect on this topic in the future. It is mostly a live coding presentation, with some slides when the code shown cannot be executed.
    JOSÉ PAUMARD
    José works as Java Developer Advocate at Oracle. PhD in applied maths and computer science, assistant professor at the University Sorbonne Paris Nord for 25 years, he is a Java Champion Alumni and JavaOne Rockstar. He is a member of the french Paris Java User Group, has been a co-organizer of the conference Devoxx France, and is a disorganizer of JChateau, an unconference held in the Chateau of the Loire Valley. He works on the dev.java documentation and community website, publishes the JEP Café, a monthly video cast on UA-cam, and maintains a french UA-cam channel with more than 80 hours of Java courses. He is also a Pluralsight author in the Java space.
    ------------------------------------------------------------
    INTRO
    * visuals & editing by @Mercator
    * music : Avocado by Ephixa
  • Наука та технологія

КОМЕНТАРІ • 19

  • @MrGabblit
    @MrGabblit Рік тому +2

    Very insightful, well presented talk

  • @khalled95
    @khalled95 Рік тому +8

    The main issues in java 17 that companies don't use or upgrade to it, most companies stick with java 8 or 11

  • @TaranovskiAlex
    @TaranovskiAlex Рік тому

    Thank you for the great talk!

  • @PietervandenHombergh
    @PietervandenHombergh Рік тому

    Coverage in the switch can be achieved with an anon innner of the Shape in a unit test.

  • @PanMaciek
    @PanMaciek Рік тому +5

    I love using pattern matching, but seeing how java is going to implement this I'm not sure it will feel great

    • @GavinFreeborn
      @GavinFreeborn Рік тому +4

      This summarizes every functional feature java has added. Kinda sad but I guess it's at least there. Unfortunately a poor implementation means it's much harder to sell at work

  • @DavidBeaumont
    @DavidBeaumont Рік тому

    I have a question about pattern matching on records (about 35:00). If you use "(Circle radius) -> radius * radius", what happens if you add a new component to Circle (e.g. a colour)? Now the record has a new canonical constructor. And worse, what if the property is the same type as the existing value (e.g. "double celcius"). Now I think that all the callers using pattern matching must update their code with a '_'? I guess (looking ahead) the owner of the type would be responsible for adding an explicit custom pattern to retain the old semantics, but this would be a manual thing you need to remember to do.

  • @francksgenlecroyant
    @francksgenlecroyant 6 місяців тому +2

    I think the serialization process is using the Constructor even with regular java classes, the reason why the validation rule is not triggered is because, the constructor which is involved here is the default non-args constructor. That's it!

  • @jamesschmames6416
    @jamesschmames6416 Рік тому

    I thought it was bad form to throw exceptions in a constructor.

    • @patrickproctor3462
      @patrickproctor3462 Рік тому +2

      It should be done judiciously. We use exception throws in constructors for user-provided data so we fail very loudly, very clearly, and in a way that tells our UI developers (and others) that they broke the rules of our API (without resorting to WSDLs which NO ONE wants to maintain).

    • @OMGitzBadCompany
      @OMGitzBadCompany Рік тому +10

      It's far better than knowingly letting invalid objects pop into existence.

  • @IFraid
    @IFraid Рік тому

    So, basically c#

  • @rsrini7
    @rsrini7 Рік тому

    java becomes another scala

  • @pierre-jeanmartin5621
    @pierre-jeanmartin5621 Рік тому +1

    They should have picked a new language keyword instead of “instanceof”. Something like “ofpattern”.
    if ( object ofpattern Point(int x, int, y) )
    // use x and y
    Or maybe it’s just me very not liking instanceof 😅

    • @DavidBeaumont
      @DavidBeaumont Рік тому +8

      Adding new keywords is very hard since it's adding a new reserved word to the language specification and might make existing code stop compiling (e.g. if I have a variable or type called "ofpattern". I agree that "instanceof" is a bit unintuitive though, but it might be the pragmatic choice.

    • @pierre-jeanmartin5621
      @pierre-jeanmartin5621 Рік тому +1

      @@DavidBeaumont i understand but it’s not like all modern IDE have a Replace in Files feature. A simple name like record was introduced but a non camel case keyword like « ofpattern » would be a problem 🤨🤔

    • @DavidBeaumont
      @DavidBeaumont Рік тому

      @@pierre-jeanmartin5621 what about tools which process Java files or databases containing symbol information of binaries for debugging. There are a lot of potential ways new keywords can mess things up in hard to fix ways. Record is, I think, much easier because it's a new type identifier rather than a language keyword.

    • @MrKar18
      @MrKar18 9 місяців тому +1

      ​@@pierre-jeanmartin5621what about 10000 if libraries using? You can't modify existing libraries. Java has always been backward compatible. It can't be make or break as its enterprise's go to language.

    • @KangoV
      @KangoV 2 місяці тому

      maybe if(object match Point(var x, var y)); ?