Routing tool in java or kotlin

This is how “experimented” developers (= developers who know that programs can fail) write code
kameamea

It is ok if you are alone on your project, you can even identify easily your legacy code visually by doing that.
It is not (always) ok if you work in a team. Sometimes one might just ask you : can you write a truth table of your algorithm.

Unless if you think your program is simple enough not to need documentation, you will end up writing something like this in your documentation source :

              arch     │ hasJava │ has.NET │ choose
            ───────────┼─────────┼─────────┼─────────
                       │         │         │ RSA
                       │   yes   │         │ JKS
              win      │         │         │ PKCS12
             win|linux │         │   yes   │ PKCS12 

I do think there is another way :
why don’t you use a table in your code ?
You can either do that by using configuration tools or business rules management systems.
Or by using a very naive util class :

https://gist.github.com/libetl/1e446d60da00d1af74a2303918496f1c#file-router-kt

It will allow you to configure routing tables directly inlined in code and then to use them at runtime.
The leftmost parameters will have more priority than the others.
You can also specify several values to match several possibilites rather than copy pasting the rows, just write a|b (a vertical bar) as separator.

Take a look at the RouterTest.kt file and don’t hesitate to copy paste some tables in your code to try out.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.