Giter VIP home page Giter VIP logo

demo-enum-switch-case-anti-pattern's Introduction

Example project to demonstrate an anti-pattern on enums

If you use switch case on enums, your compiler and tests won't recognize a need for code change. In this demo project I show some alternatives.

See blog post for a full explanation https://christianoette.com/en/blog/blog-post/2022-05-07-switch-on-enums

Anti pattern example

  • You could add new values to the StatusEnumNotRefactoringSafe.class
  • This would lead to a runtime error

Map example

  • A first improvement could be a map as in MapExample.class
  • If you add a value to StatusEnum you are forced to also add a color
  • A unit test reminds you to implement a new strategy

Visitor example

  • You could add a standard interface which redirects calls to specific functions
  • E.g. if you add a value to StatusEnumVisitorPattern you are forced to also add a color and implement a method
  • No unit test is needed

Strategy example

  • If you use a framework (e.g. Spring) then it is even better to link a strategy to your enum
  • E.g. if you add a value to StatusEnumWithStrategy you are forced to also add a color and a strategy
  • No unit test is needed

Special case, InstanceOf example

  • On Java 17 you can use a combination of sealed, abstract and final classes to have a fixed set of possible class combinations
  • If you use switch case on them, then you can remove the default case. The compiler is going to warn you if you add a new subclass

demo-enum-switch-case-anti-pattern's People

Contributors

christian-oette avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.