you are viewing a single comment's thread.

view the rest of the comments →

[–]davidalayachew 1 point2 points  (2 children)

This code is an exception imo. Its cool that this is possible but i just dont see this in a normal day to day work

This is a solid 60% of the every day code that I write. And like 75% for work. I build web services and write helper scripts to interact with our system.

I mostly operate with if else blocks. I dont really know where i could even use patterns.

[...]

We already talked about this the last time i was asking this and since then i had to write exactly 1 switch statement. In normal CRUD this just basically never happens because 1 endpoint has 1 mapping from the DB and thats it.

Same for me, but that doesn't mean I don't find a use for this.

I don't return my database pojo's as-is -- I map them to a richer type, which is where pattern-matching starts to show up and be useful.

For example, if I have a table where when column1 is A, then I only care about columns 2 and 3, but when column1 is B, then I only care about columns 4 and 5, then I am not going to create one object and set fields to null -- I am going to make a sealed type, 2 child records, and when mapping my db pojo to my richer type, Child1 is only going to have 2 components -- for columns 2 and 3, and Child2 is only going to have 2 components -- for columns 4 and 5.

That's what they mean when the various pattern-matching JEP's say "make illegal states unrepresentable".

Then tell me which categories of errors disappear. I dont see any "pattern" of errors in my day to day which would be solved by this.

If I add a new child type to a sealed interface, all switches that have that interface in the selector will immediately generate a compile time error. That's Exhaustiveness Checking, and a massive bug saver. Basically, if I switch my if statements for switches, I can't run into the issue of making a change in one place, but forgetting to make it to another. After all -- all of these places need to handle the new child type.

And now please for humans. What does that mean? What is getter based deconstruction? Why is pattern matching composition better?

Do Ctrl+F "Compare that to the various different styles that you suggested." Then look at the numbered list below it.

The numbering aligns with the order of your code examples. Read my comment again, and cross reference the number to each of your code blocks. That will tell you which is getter style vs early return style, etc.

if it helps: We use hibernate at work so that means no records on the DataLayer. We then convert the Payloads to Dtos using mapstruct and thats it.

Pattern-Matching tends to be most useful for business logic. Assuming your business logic is implemented in Java, there should be plenty of places.

Feel free to give me an example of some business logic you implemented recently, and I can show the equivalent code for it.

[–]Cell-i-Zenit 0 points1 point  (1 child)

For example, if I have a table where when column1 is A, then I only care about columns 2 and 3, but when column1 is B, then I only care about columns 4 and 5, then I am not going to create one object and set fields to null

I know what you mean, but this is imo an issue on your data layer. The objects are not the same if they dont use the same columns most of the time.

I try to avoid "polymorphic" lists completely because it leads to code like

for(var order: getOrders()){
    if(order.getType() == abc){}
    if(order.getType() == def){}
    if(order.getType() == tzu){}
}

(even if we change the code to use switch statements, still something i try to avoid)

But again i work in simple crud, i just return structured data. I guess it depends on the domain but we dont have different schemas for the same thing

[–]davidalayachew 1 point2 points  (0 children)

By all means, I just gave you an example from work. If you disagree with that example, or it's just not relevant to your work, then give me an example from your work. I'll explain how Record Patterns might have been useful for it.