Java’s new paradigm | Ties van de Ven

BangaloreJUG
25 Apr 202453:43

Summary

TLDRThis talk delves into the concept of data-oriented programming, emphasizing its distinction from object-oriented and functional programming paradigms. The speaker explores the 'why' behind data-oriented programming, its impact on software architecture, and how it can enhance scalability. The discussion also highlights Java's new language features like records, sealed classes, and pattern matching, which support this programming style. The speaker advocates for a pragmatic approach, using the best tools for each situation rather than adhering strictly to one paradigm.

Takeaways

  • 😀 The speaker is passionate about software engineering fundamentals and was intrigued by the concept of data-oriented programming in Java after reading an article by Brian Goetz.
  • 🔍 The speaker embarked on a journey to understand data-oriented programming, finding it to be a different, rather than a superior, approach to software development compared to object-oriented and functional programming.
  • 📚 Brian Goetz's talk on functional and object-oriented programming emphasizes the idea of not being dogmatic and being a better programmer by using the right tools for the situation.
  • 🤔 The main question the speaker sought to answer in their talk is the definition and essence of data-oriented programming, as they couldn't find a clear answer online.
  • 🛠️ Data-oriented programming focuses on the data itself rather than the types or classes that hold the data, which can lead to more scalable and performance-optimized applications.
  • 🔄 The speaker discusses the concept of data streams and how most applications can be viewed as a series of data transformations involving map, filter, and branch operations.
  • 🧱 The use of types in Java and their downsides, such as not naturally mapping to other types, is contrasted with the efficiency of using data structures like hashmaps that are designed for operations like map and filter.
  • 📈 Data-oriented programming can offer performance benefits, especially in gaming and other real-time applications where frame rate is critical.
  • 🏗️ The talk also covers how data-oriented programming affects software architecture, advocating for separating use cases from data to create a cleaner object graph and easier scalability.
  • 🆕 The introduction of new Java language features like records, sealed classes, and pattern matching are discussed as tools that enable data-oriented programming, but not as the central focus.
  • ⚠️ The speaker advises against being dogmatic about any programming paradigm, suggesting that the best approach often involves a combination of object-oriented, functional, and data-oriented programming.

Q & A

  • What is the main focus of the talk on data-oriented programming in Java?

    -The talk focuses on explaining what data-oriented programming is, its mindset, and how it differs from object-oriented and functional programming. It also discusses the implications of data-oriented programming on software architecture and the benefits of new language features like records, sealed classes, and pattern matching in Java.

  • Why was the author initially excited about data-oriented programming coming to Java?

    -The author was excited because they had no prior understanding of what data-oriented programming entailed and were interested in exploring this new concept after reading an article by Brian Goetz.

  • What is the fundamental difference between object-oriented programming and data-oriented programming?

    -Object-oriented programming focuses on using encapsulation to create boundaries within an application and relies on the 'tell-don't-ask' principle, whereas data-oriented programming is centered around the data itself and how it is transformed, often using data structures that are optimized for operations like map, filter, and branch.

  • What are the three basic operations that can be performed on data streams according to the data-oriented mindset?

    -The three basic operations are filter (removing data), map (transforming data), and branch (splitting data into different paths).

  • How does data-oriented programming affect software architecture?

    -Data-oriented programming can lead to a cleaner object graph by separating use cases from data, allowing for the creation of components for each use case and passing data as arguments to these components. This can result in better scalability and maintainability.

  • What is the 'tell-don't-ask' principle in object-oriented programming?

    -The 'tell-don't-ask' principle means that instead of asking an object for its data and then acting on it, you should tell the object what to do, providing it with the necessary information to perform the action itself.

  • How does the use of records and sealed classes in Java support data-oriented programming?

    -Records and sealed classes provide a way to encapsulate data and define restricted sets of classes that can extend an interface, respectively. This allows for more compile-time safety and cleaner pattern matching, which are beneficial for data-oriented programming.

  • What is the advantage of using pattern matching with sealed interfaces in Java?

    -Pattern matching with sealed interfaces allows for compile-time safe branching operations, enabling developers to handle different cases based on the type of data without the need for a default case, thus reducing the risk of runtime errors.

  • What is the relationship between data-oriented programming and the gaming industry?

    -Data-oriented programming is popular in the gaming industry due to its focus on performance optimization. By using data structures that are optimized for certain operations, games can achieve higher frame rates and better performance.

  • What does the author suggest about being a 'better programmer'?

    -The author suggests that instead of strictly adhering to one programming paradigm, programmers should be pragmatic and use a combination of tools and approaches that best fit the situation, including both object-oriented and data-oriented programming where appropriate.

  • How does the new pattern matching feature in Java 21 impact the use of polymorphism?

    -The new pattern matching feature in Java 21 allows for a more expressive and safe use of polymorphism by enabling developers to branch logic based on the type of an object without needing a default case, thus enhancing compile-time safety and code readability.

  • What is the speaker's opinion on using mutable state in programming?

    -The speaker prefers immutability because it reduces cognitive load and makes the code more predictable. However, they acknowledge that mutable state can be used for performance reasons, but it should be contained within function scope to minimize its impact on the rest of the program.

  • What is the speaker's view on the adoption of data-oriented programming features in Java?

    -The speaker believes that it will take time for the community to adapt to these new ways of thinking, as Java has a legacy of a different mindset. They also mention that while Java is slowly adding features that are present in languages like Scala, it may not be as powerful or as easy to use for functional programming as Scala is.

  • What does the speaker suggest about the use of builders in Java?

    -The speaker is not a fan of builders because they can lead to a loss of compile-time safety. They prefer immutable objects by default and suggest that Java's newer versions may provide better ways to handle immutability.

  • How does the speaker compare the use of sealed interfaces in Java to similar features in other languages like Scala?

    -The speaker notes that Java's approach to sealed interfaces is a step towards features that have been available in Scala for a long time. They also mention that Java allows for more flexibility in terms of file organization for sealed interfaces and records.

  • What is the speaker's advice for developers who are considering whether to stick with Scala or explore Java further?

    -The speaker suggests that developers can do functional programming in any language, including Java, but acknowledges that Scala provides more powerful features for this paradigm. They encourage sharing knowledge and learning from those who have experience with these features, regardless of the language.

Outlines

00:00

🤔 Exploring Data-Oriented Programming in Java

The speaker expresses excitement about data-oriented programming coming to Java, a concept they initially didn't understand. They recount their journey of discovery, starting with Brian Goetz's article and moving through various talks that focused on new language features like records, sealed classes, and pattern matching. The speaker clarifies that the talk isn't about how to use these features, but rather about the 'why' behind data-oriented programming. They emphasize that it's not a superior method but a different approach with its own set of trade-offs, compared to functional and object-oriented programming. The talk aims to answer the question of what data-oriented programming is and how it can make applications more scalable, ending with a nod to Brian Goetz's advice to be a better programmer by not being dogmatic and having a versatile toolset.

05:01

📚 Understanding Object-Oriented vs. Functional vs. Data-Oriented Programming

This paragraph delves into the differences between object-oriented programming, functional programming, and data-oriented programming. Object-oriented programming is characterized by encapsulation and the 'tell, don't ask' principle, focusing on methods rather than data. Functional programming is depicted as a series of functions chained together like dominoes, emphasizing composition. Data-oriented programming, in contrast, is all about the data, viewing it as streams and transformations. The speaker uses the car example to illustrate the object-oriented approach and contrasts it with the data-oriented mindset, which would de-emphasize the class structure in favor of the data itself. The paragraph also touches on the inefficiencies of type mapping and the benefits of using data structures like hashmaps and lists for operations like map and filter.

10:03

💡 The Data-Oriented Mindset and Its Impact on Software Architecture

The speaker introduces the data-oriented mindset, which views applications as data streams and transformations. They explain that most applications can be abstractly modeled using filter, map, and branch operations on data. The paragraph discusses the state changes involved in applications, such as transforming JSON to a hashmap and back, and how this can be streamlined in a data-oriented approach. The speaker also touches on the benefits of denormalizing data for performance, using the 'persons' class example to illustrate different data representations. They highlight the trade-offs between maintainable structured code and fast, performance-optimized code, particularly in the gaming industry, and how data-oriented programming affects software architecture by keeping use cases separate from data.

15:05

🔍 Case Study: Object-Oriented vs. Data-Oriented Design in Java

This paragraph presents a case study comparing object-oriented and data-oriented design approaches in Java. The speaker uses the example of implementing a 'save' method to illustrate the complexities that can arise in object-oriented programming, such as dependencies and testing difficulties. They contrast this with the data-oriented approach, where use cases are separate components that receive data as arguments, leading to a cleaner object graph and easier scalability. The speaker also discusses the implications of removing use cases and the benefits of this approach in terms of maintainability and reducing the risk of application bloat.

20:06

🛠️ Utilizing Java's New Language Features for Data-Oriented Programming

The speaker discusses how to apply data-oriented programming in Java, focusing on the use of new language features like records, sealed classes, and pattern matching. They explain polymorphism in the context of data-oriented programming and provide an example of implementing the strategy pattern using these features. The paragraph explores the benefits of using pattern matching with sealed interfaces for compile-time safe branching operations, which is a game-changer for control flow in Java applications. The speaker concludes by emphasizing the importance of being a better programmer by using the right tools for the job, rather than adhering strictly to one programming paradigm.

25:06

🔄 The Evolution of Java's Approach to Polymorphism and Data Streams

The speaker reflects on the evolution of Java's approach to polymorphism, discussing the introduction of new features that enable a new way of using pattern matching and sealed classes. They highlight the impact of these features on data streams and state changes within applications. The paragraph also touches on the community's adaptation to these new features and the importance of understanding type theory concepts like product types and sum types. The speaker anticipates a period of adjustment as developers become accustomed to this new paradigm in Java.

30:07

📘 Navigating the Transition from Mutable to Immutable State in Java

In this paragraph, the speaker discusses the transition from mutable to immutable state in Java, emphasizing the cognitive benefits of immutability. They argue that immutable objects simplify software engineering by reducing the need to understand the entire program to ensure consistency. The speaker also addresses the performance implications of mutable state and suggests that functions should ideally have immutable inputs and outputs, with mutable state confined to the function scope. They express a preference for immutability and caution against the use of mutable state, advocating for careful handling if it is necessary.

35:08

🔍 Discussing the Use of Builders and Records for Immutability in Java

The speaker engages in a discussion about the use of builders and records for achieving immutability in Java. They express a preference for records due to their simplicity and the default immutability they provide. The paragraph explores the trade-offs between using builders, which can offer a more fluent way of constructing objects but at the cost of compile-time safety, and records, which enforce immutability. The speaker also mentions upcoming Java features that will further support immutability and improve the quality of life for developers working with immutable objects.

40:09

🤝 Sharing Knowledge and Adapting to New Java Features

The speaker concludes by emphasizing the importance of knowledge sharing and adapting to new Java features. They encourage experienced developers to share their insights with the community, particularly regarding advanced features like sealed interfaces and records. The paragraph also touches on the ongoing addition of features to Java that have been present in other languages like Scala and Kotlin, and the need for Java developers to continue learning and evolving their skills. The speaker expresses optimism about the future of Java and the opportunities it presents for developers to grow and contribute to the community.

Mindmap

Keywords

💡Data-Oriented Programming

Data-Oriented Programming (DOP) is a programming paradigm that emphasizes the organization and manipulation of data rather than the actions performed on it. In the script, DOP is introduced as a different approach to software development that focuses on how data flows through the system and how it can be optimized for performance. The speaker discusses how DOP can lead to more scalable and maintainable code by considering data transformations and state changes.

💡Object-Oriented Programming

Object-Oriented Programming (OOP) is a widely-used programming paradigm that uses objects, which can contain data and code, to design applications. The script contrasts OOP with DOP, highlighting the encapsulation principle and the 'tell-don't-ask' approach in OOP where objects are given instructions rather than queried for their state. The speaker uses the example of a 'car' class to illustrate the differences in mindset between OOP and DOP.

💡Functional Programming

Functional Programming is a programming style that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. In the script, functional programming is described as a paradigm where everything is a function, and these functions can be composed together like dominoes, creating a chain of data transformations. The speaker emphasizes the importance of immutability in functional programming.

💡Polymorphism

Polymorphism is a concept in programming that allows objects to be treated as instances of their parent class rather than their actual class. The script discusses how polymorphism has traditionally been used in Java for code sharing but can now be used for type sharing with the introduction of new language features like records and sealed classes.

💡Records

Records are a new feature in Java that serve as immutable containers for data. The script mentions records in the context of DOP, explaining that they can be used to represent data without the need for traditional class constructors and getters/setters. Records simplify the creation of immutable data types and are part of the shift towards more functional and data-oriented programming styles in Java.

💡Sealed Classes

Sealed classes are a feature introduced in Java that restricts class inheritance to a specific set of classes, enhancing compile-time safety. The script explains how sealed interfaces, in conjunction with records, allow for pattern matching with compile-time safety, a significant advancement for data-oriented programming in Java.

💡Pattern Matching

Pattern matching is a programming technique that allows a program to determine the structure of an object and execute code based on its shape. The script discusses the new pattern matching capabilities in Java 21, which enable more expressive and safer code by eliminating the need for a default case in switch statements when dealing with sealed interfaces.

💡Immutability

Immutability refers to the property of an object that cannot be modified after it's created. In the script, immutability is highlighted as a key aspect of data-oriented design, where immutable objects reduce cognitive load and improve the predictability of software. The speaker contrasts mutable and immutable approaches to data modeling, advocating for the latter.

💡Cognitive Load

Cognitive Load refers to the mental effort involved in processing information or learning. The script uses the term to discuss how certain programming practices, such as immutability, can reduce the amount of mental effort required to understand and maintain code, making it a simpler and more straightforward approach.

💡Strategy Pattern

The Strategy Pattern is a design pattern used to enable an object to change its behavior or algorithm dynamically at runtime. The script uses the strategy pattern as an example to illustrate the difference between object-oriented and data-oriented approaches to programming, showing how the same pattern can be implemented in both paradigms with varying levels of flexibility and safety.

Highlights

Introduction to the concept of data-oriented programming and its distinction from traditional object-oriented and functional programming paradigms.

Excitement about Brian Goetz's article on data-oriented programming coming to Java, which sparked a deeper exploration of the topic.

The importance of understanding the 'why' behind software engineering concepts rather than just the 'how'.

Data-oriented programming's focus on data streams and transformations as the core of application development.

The comparison between object-oriented and data-oriented programming in terms of handling data and operations.

The debate between functional programming and object-oriented programming, emphasizing the need for a flexible approach rather than being dogmatic.

The role of language features like records, sealed classes, and pattern matching in facilitating data-oriented programming.

The potential of data-oriented programming to improve application scalability and maintainability.

The practical application of data-oriented programming in enterprise software and the gaming industry.

The impact of data-oriented programming on software architecture and the benefits of separating use cases from data.

The discussion on the trade-offs between performance optimization and long-term maintainability in software development.

The use of pattern matching with sealed interfaces and records in Java to implement compile-time safe branching operations.

The comparison between Java's new language features and existing capabilities in languages like Scala and Kotlin.

The importance of being a 'better programmer' by utilizing various programming paradigms and tools as appropriate for the situation.

The future of data-oriented programming in Java and the community's adaptation to new language features.

The call to action for programmers to share knowledge, mentor others, and contribute to the growth of the software development community.

The conclusion emphasizing the need for pragmatism in software development and the value of combining different programming approaches.

Transcripts

play00:02

and I'm quite big on software

play00:04

engineering fundamentals and Concepts so

play00:07

more about the why instead of the how

play00:10

those are things that get me

play00:11

excited um that's why when I saw Brian

play00:14

gats write a article about two years ago

play00:17

now about data oriented programming

play00:19

coming to Java so I was quite excited

play00:21

because I had no clue what data oriented

play00:23

programming actually meant um so I

play00:26

wanted to figure that out and I went

play00:28

searching online and I quite found quite

play00:31

a few talks and most mention these new

play00:34

language features records Shield classes

play00:36

and pattern matching which in off

play00:38

because these are the things that should

play00:40

make this data oriented programming

play00:43

possible however basically all talks I

play00:47

found were about how to use these new

play00:49

language features and that's why I don't

play00:53

want that to be the main focus of this

play00:54

talk because I needed answer to the

play00:57

question so what actually is data ored

play00:59

programming

play01:01

and because I couldn't find the answer

play01:02

of the talk online then I figured oh for

play01:05

the question online I figured then I'll

play01:07

just write my own talk about it so

play01:09

although we'll go into a few language

play01:11

features at the end and that's not the

play01:13

main purpose of this talk and something

play01:16

that's important to me uh that this is

play01:18

not a better way of programming it's a

play01:20

different way of

play01:22

programming um there's a lot of debate

play01:24

between functional programming and

play01:26

objectoriented programming and such and

play01:29

for me especially as someone who's more

play01:31

into fundamentals uh in the end it's all

play01:35

engineering and both ways have the

play01:39

tradeoffs some work better in certain

play01:41

areas other things will work better in

play01:43

other areas so this is not a better way

play01:46

of programming it's a slightly different

play01:48

way of programming and one which you

play01:50

might already do Brian gats has a really

play01:52

good talk function programming object

play01:54

oriented programming pck two and he

play01:56

ended with this quote don't be a

play01:58

functional programmer don't be an object

play02:00

a programmer be a better programmer and

play02:03

I think this this for me this was quite

play02:05

inspiring as in no we shouldn't be

play02:07

dogmatic uh we should get as many Tools

play02:09

in our toolbox as possible so we are

play02:12

prepared for every situation and we can

play02:14

write the best possible solution for

play02:15

every occasion so what are we going to

play02:17

talk about today we're going to do a

play02:19

quick recap about objectoriented

play02:22

programming functional programming data

play02:23

oriented programming what it actually

play02:24

means then we'll head into the data

play02:27

oriented programming mindset programmer

play02:29

mindset

play02:30

um we'll take things to a bit of an

play02:32

extreme there but it's just to get a

play02:34

picture of what it's like to see data as

play02:36

data then we'll go into what data

play02:38

oriented programming does to our

play02:39

architecture and how we can use it there

play02:41

to make our application more

play02:43

scalable and then we finish off with

play02:46

talking a bit about what the language

play02:48

feature actually means the new language

play02:49

feature actually means and what this

play02:51

allows us to do so let's talk with

play02:53

objectoriented programming versus

play02:55

functional programming versus data

play02:56

oriented programming so uh what's object

play02:59

oriented program programming well here

play03:01

everything is an object and it uses

play03:03

encapsulation to create boundaries

play03:05

within your application and it relies

play03:08

heavily on the tell don't ask principle

play03:11

so what do I mean by that it means if we

play03:13

would have some code like this where we

play03:15

have a car with current Fuel and a Max

play03:18

Fuel and some Getters and Setters if you

play03:20

would have a function add fuel that

play03:23

takes a car's input here we ask the car

play03:26

for the current Fuel and we ask the car

play03:28

for the max fuel and then we set the

play03:31

curent the current view this is not

play03:34

really the object oriented programing

play03:36

mindset because here we ask the object

play03:38

for its data instead in object oriented

play03:41

programming we want to tell the the

play03:43

object what to do we want to tell the

play03:45

car to add the fuel and just give the

play03:47

amount and how it solves it we don't

play03:50

care we just tell it what to do we don't

play03:53

care about the data we care about the

play03:54

methods put that more in perspective if

play03:57

we would have a class data we actually

play03:59

don't care about what What fields it has

play04:02

we care about what are things we can do

play04:04

with this car object if you then look at

play04:07

functional programming uh here's

play04:08

everything is a function uh a good way

play04:11

to symbolize that uh and they're tied

play04:14

together using something called

play04:16

composition good way to visualize that

play04:18

are dominoes where every stone could be

play04:21

seen as a function which has a number as

play04:23

input and a number as output and as long

play04:25

as the number output matches the input

play04:27

of another Stone you can chain them

play04:29

together you can create really beautiful

play04:31

software that way that's the basic

play04:33

Parise of functional programming so what

play04:35

is data or programming uh well here

play04:39

everything is data or more specific

play04:42

everything is about the data so if you

play04:45

would have any class within data or

play04:48

programming we don't actually care it's

play04:50

in a class we just care about these two

play04:53

Fields because that's the actual data it

play04:55

just happens to have something called a

play04:57

car wrapped around it but we don't care

play04:59

about that stuff we care about what's

play05:01

inside the car what are this the data we

play05:03

can work with and this brings us to the

play05:06

data oriented Pro

play05:07

mindset um so here data mindset it's all

play05:13

about the data to visualize that bit uh

play05:17

if you take a big enough step back like

play05:20

and look at applications in a really

play05:22

abstract way most applications are just

play05:26

data streams and data streams are

play05:28

nothing more than data transformation so

play05:32

data streams is nothing more that you

play05:34

have a source which something that emits

play05:36

data and a sync something that receive

play05:38

data and the nice thing is there are

play05:41

only basic three operations you can

play05:43

perform here there's a filter that takes

play05:46

data away from a scen there's a map that

play05:49

changes data to another type of data and

play05:52

we have a branch that either says uh

play05:55

splits up the data uh in one way another

play05:58

way or just splits it up evenly and

play06:01

using this way of thinking you can

play06:03

actually model basically every

play06:05

application out there to give an example

play06:07

if we would just look at any web service

play06:10

we would have some Json that gets mapped

play06:13

to the user a user might need to go we

play06:16

might need to check some permissions

play06:18

which is a filter operation because

play06:19

everything might stop there then we

play06:22

might find a filter the user out because

play06:25

it it doesn't come through our

play06:26

validation phase and once everything is

play06:28

done we map the user to a query we M the

play06:31

query to an entity and we map The Entity

play06:34

to response and we map the response back

play06:36

to Jason so if you look at any

play06:39

application abstractly enough it's just

play06:41

a data stream that goes from Json

play06:43

through this back to Json uh using only

play06:46

map and filter operations so there's

play06:49

also a lot of State changes going on

play06:51

here it go from user to query forr to

play06:53

entity for entity to respon and response

play06:55

to

play06:56

Jason and usually in Java we use types

play06:59

for is so we go from Json to hashmap

play07:02

from hashmap to user from user to entity

play07:04

enti response from response to hashmap

play07:06

and from hashmap back to Json and that's

play07:08

quite a lot of types for quite a simple

play07:11

operation and this is where a real data

play07:14

oriented programmer thinks huh that's

play07:17

not very very good because types also

play07:20

have a downside as types don't naturally

play07:23

map well to other types so uh if you

play07:26

want one type to map to another type you

play07:28

usually have to write a custom mapper so

play07:31

if you make a class person and want to

play07:32

turn it into a second person you would

play07:35

need to write a custom mapper or you

play07:38

have to use reflection for example uh

play07:41

spring boot uses Jackson as Jackson for

play07:44

Json calization which US under

play07:46

reflection under the hood working with

play07:48

types either means you have to write

play07:50

custom code to map one type to the other

play07:53

or you have to rely on reflection magic

play07:55

to do it so types don't map well to

play07:57

other types and this is a problem

play08:00

according to data oriented programmers

play08:03

because that's most of the things we do

play08:05

we map types to other types fav types

play08:08

don't M natur you map to other types uh

play08:11

so data programmers are like why

play08:13

shouldn't we use uh data structures that

play08:17

are made to do these things like hashmap

play08:20

or List these are data structures that

play08:22

are meant to do operations as as map and

play08:24

filter and such uh on the data structure

play08:27

itself so if would instead of that use

play08:31

hashmaps it would look something like

play08:33

Json to hashmap to hashmap to hashmap to

play08:35

hashmap to Json which is a lot easier in

play08:38

a way we don't have to write uh classes

play08:41

for intermediate data uh representations

play08:45

you can just go from Json to hashmap

play08:48

then it's hashmap all the way back to

play08:49

Json so that's basically thinking in uh

play08:52

it's just data we don't care about the

play08:54

types we only care about data and data

play08:57

transformation so does this mean that we

play08:59

should just stop using types uh well not

play09:01

really like real data oriented program

play09:04

languages like closure they can get away

play09:05

with this uh because in closure you have

play09:09

things like um schemas where you can

play09:14

basically tell that the hash should have

play09:16

at least these keys and they circumvent

play09:19

certain problems by just writing a lot

play09:21

of tests and this is where types shine

play09:25

because types gives a lot of comp time

play09:27

checks and therefore we have to write

play09:29

lot less test because we are certain

play09:32

these fields are there because we use

play09:34

the type system so in Java I wouldn't

play09:36

recommend going uh all hashmaps but we

play09:39

can learn something from this way of

play09:41

thinking we don't have to model our data

play09:44

in that structured way it can also uh

play09:47

denormalize data and that can have some

play09:49

benefits as well to give an example of

play09:52

that um if we have a public class person

play09:56

which has a age and a name if you would

play09:58

have a bunch of person we could put them

play10:00

in a list and this is nice and ordered

play10:03

we have a person and we have a list of

play10:04

persons a way to denormalize this data

play10:07

would be our class persons which

play10:09

contains two lists one containing the

play10:12

ages and the other containing the names

play10:14

so these are two ways to represent the

play10:17

exactly the same data where this is nice

play10:19

and structured and this well I don't

play10:22

really like you would normally really

play10:25

like this uh representation but it can

play10:27

have some benefits and that really shows

play10:30

when you look at for example

play10:32

databases where if you make a database

play10:34

for your own software or database

play10:36

designed for your own

play10:37

software you want everything to more

play10:40

look like this everything should be

play10:42

dormal normalized uh there shouldn't be

play10:44

duplication everything should be nicely

play10:46

checked from keys and everything so the

play10:49

database can protect you against

play10:51

mistakes and protect you against data

play10:52

corruption uh but it has a downside is

play10:55

that relatively the queries are

play10:57

relatively slower than they could have

play10:59

been and so if you would give your

play11:01

database to a data warehouse they would

play11:04

do really really bad things to that data

play11:07

as in they would duplicate everything

play11:09

create massive tables uh but they're all

play11:12

optimized to do certain searches which

play11:15

are lightning fast so by giving up all

play11:17

the structure and all the duplication as

play11:20

such we get a lot of performance and

play11:23

this is also what we can do in code

play11:25

instead of having nicely structured code

play11:27

which is optimized for

play11:29

uh longer term maintainability we can

play11:32

also go for different representations of

play11:34

the same data and have it to be lighting

play11:37

fast uh where you see this a lot is the

play11:39

gaming industry for example because in

play11:41

the gaming industry you want want that

play11:43

60 FPS so uh doing every trick in the

play11:47

book to make sure you get to that uh

play11:50

that's important there there is also a

play11:52

bit of a different distinction where the

play11:54

tradeoff of uh long maintainable code

play11:57

versus quick code uh tends to lead more

play12:00

towards quick codee with gaming because

play12:03

even though the statement isn't that

play12:04

true these days uh but once the game is

play12:06

finished you don't really do more

play12:09

patches on it while enterprise software

play12:12

will get a lot of Maintenance over the

play12:14

years so the trade of of or we rather

play12:16

have fast code than than code we have to

play12:19

maintain um in gaming that leads more

play12:22

towards the fast code while Enterprise

play12:24

software would Le more toward structured

play12:26

code but if you have struggle somewhere

play12:29

with uh performance well these are

play12:31

tricks that you can do uh so that's what

play12:34

the reason why data programming is very

play12:36

popular within um gaming within the

play12:39

gaming industry so aside from

play12:41

performance um data oriented programming

play12:43

also affects architecture so let's see

play12:45

how we would design something in

play12:47

objectoriented programming versus the

play12:49

data oriented programming way in

play12:51

objectoriented programming uh all the

play12:53

use cases we together with the data as

play12:55

you saw we have this tell don't ask uh

play12:58

principle

play12:59

where our car would have method set VI

play13:02

and this makes it very easy to see what

play13:03

operations are available on that data in

play13:06

data programming we don't care about the

play13:08

class we only care about the data and

play13:11

therefore use cases will live separate

play13:13

uh in separate components so they won't

play13:16

be directly connected to the data which

play13:18

does give a clean object draph and can

play13:20

scale better uh to give an example of

play13:23

that uh let's implement the S the save

play13:26

method in an objectoriented programming

play13:28

tell don't ask way so we tell a person

play13:31

it needs to persist itself in order to

play13:34

do some do persisting we would need

play13:36

something like a data access Helper and

play13:38

there are three ways to do this one is

play13:41

that a person would get a data access

play13:43

helper for its Constructor so when call

play13:45

the safe method it can use that data

play13:46

access help the problem here however is

play13:49

now construction of the person becomes

play13:51

way more difficult aside from all the

play13:53

data you also need to give all the

play13:55

dependencies uh for all the side effects

play13:58

that the person needs

play14:00

we could of course cheat this and use a

play14:02

static lookup instead which would work

play14:05

uh but now it's very very difficult to

play14:07

mock this which is also not very good so

play14:10

this makes it a lot more difficult to

play14:12

test or finally we could try a more

play14:14

visitor pattern approach where we would

play14:16

pass the data access outut to the save

play14:18

function but now both the corer both the

play14:21

corer of this function and the person

play14:23

needs to know about uh how this object

play14:26

should be persistent which is also not

play14:27

good but the bigger problem here is that

play14:31

all three methods uh rely on the person

play14:34

class to have knowledge of the data

play14:36

access help so if you would put this

play14:38

into a diagram a person would now be

play14:42

dependent would now be dependent on the

play14:44

Exel but a new component would be

play14:45

dependent on the person but because

play14:47

person has this dependency transitively

play14:49

the component is also dependent on the

play14:52

data access out and if it's just this

play14:55

then it's probably fine but what if we

play14:57

need to add a second component component

play14:59

which needs needs to do something with a

play15:02

person that involves a rest controller

play15:05

then our object graph would look

play15:06

something like this the first component

play15:08

needs to do the person to do something

play15:10

with the data exess helper the second

play15:11

component needs the person to do

play15:13

something with the rest Helper but now

play15:15

trfy both components are dependent on

play15:18

both the data access help and the rest

play15:20

helper so we started creating a bit more

play15:22

of a net here also what if we don't need

play15:26

this component anymore this use case um

play15:29

this use case doesn't exist anymore we

play15:31

will remove this will we actually also

play15:34

remember to remove all the codes within

play15:36

person that was needed for just that use

play15:38

case uh because you probably wrote test

play15:41

for this I assume and

play15:44

therefore the IDE won't show you those

play15:46

methods are no longer in use so since

play15:49

the person class slowly would get bigger

play15:51

and bigger at some points it's quite

play15:54

like you forget to remove these methods

play15:56

which means that it will have become

play15:58

even bigger than it should be so there's

play16:01

a bit of risk in there as well like I

play16:03

said the chances of application

play16:05

shrinking are not that big most

play16:07

applications only tend to grow so the

play16:09

amount of use cases will increase and

play16:12

therefore our object cses will increase

play16:15

as well we just keep adding more use

play16:17

cases to for example the person class

play16:20

and I've seen this happening I worked at

play16:22

a company where we had a bit of software

play16:24

that was already uh in development for

play16:26

15 years and we had this exact produ

play16:29

that a few of our core components were

play16:31

getting massive with all the use cases

play16:33

it had to support um so how can we solve

play16:37

this uh using data programming uh in

play16:39

data programming again we don't want to

play16:42

mix the use cases together with the data

play16:44

so the only thing we can do is we create

play16:47

a component for every use case and then

play16:50

we pass the data as argument to that

play16:52

component so that would look something

play16:54

like this where we'd have a person

play16:57

persistor which would take the person as

play16:59

data and then just save that person so

play17:03

the person doesn't know uh that it's

play17:06

being saved that's just a bit of data

play17:08

this use case decides to save the data

play17:10

using a data access Helper and if you

play17:12

would look at how that would look within

play17:15

um our independency graph it would now

play17:17

look something like this where the

play17:19

component that needs data access helper

play17:21

is depend on data access help and it's

play17:24

dependent on person but person doesn't

play17:26

know it's being used by the data access

play17:29

helper so what happens in this case when

play17:32

we add a second component which needs

play17:35

that rest helper now would look

play17:37

something like this where this component

play17:39

only knows the data exess Helper and a

play17:41

person and this component will only know

play17:43

the rest Helper and this person and the

play17:46

person doesn't know anything so our

play17:48

object graph is way way cleaner this way

play17:51

so what if we remove a use case oh now

play17:53

we can just remove that component and

play17:56

all the logic containing uh to the juice

play17:58

case has now been deleted of course we

play18:01

could still forget to delete the whole

play18:03

rest helper thing uh but at least this

play18:06

this doesn't harm things in any way this

play18:08

is just a separate bit of code that

play18:10

doesn't get C anymore uh but it's not

play18:13

part of your big uh big monster of O CL

play18:17

anymore so worst case scenario uh this

play18:20

isn't that bad and can even be reuse in

play18:23

the later at a later point and if you

play18:24

now think this kind of looks familiar uh

play18:27

well if you write this boot application

play18:29

for example this is kind of what you

play18:32

already do you take some data you pass

play18:35

it for example to a service and the

play18:38

service will persist that data so you

play18:40

don't build a safe method on that data

play18:43

that on the data that you pass to the

play18:45

service you do that within the service

play18:48

and for me that was quite a revelation

play18:49

that I've actually been doing data

play18:51

oriented programming for years and years

play18:53

now I just didn't know that's this was

play18:56

being called Data ored programming and

play18:58

wasn't actually object oral

play19:00

programming so if you use any Frameworks

play19:03

like spring Micronaut or quarkus or

play19:05

something you probably already do things

play19:07

in a data oriented programming style

play19:10

which which I found to be quite

play19:11

interesting and it also shows that oh it

play19:14

it actually works so should you not just

play19:17

not use object on your program anymore

play19:20

uh no not really that again it's just a

play19:23

tool in your toolbox and just be a bit

play19:25

careful with how your dependency tree

play19:27

will turn out to be and therefore why

play19:29

you apprach your business logic but most

play19:31

importantly uh be pragmatic and not

play19:33

dogmatic do whatever works best for your

play19:35

application and it might even be a

play19:37

combination of the two and that brings

play19:39

us to the final bit of my talk um how

play19:42

does the how can we do these use these

play19:43

things in Java and like I said I don't

play19:45

really go that much into the language

play19:47

features itself uh but I'll try to show

play19:50

the idea behind using those language

play19:52

features because the biggest part we

play19:54

have uh that all these things added is a

play19:57

new way to use morphism within Java

play20:00

polymorphism is basically if one object

play20:03

extends another thing uh now it's both

play20:06

types therefore it's

play20:08

polymorphis so polymorphing is just a

play20:10

fer for if some one type extends another

play20:13

type and for a long time in Java this

play20:15

was mostly used to share code but right

play20:18

now with the new Java one language

play20:19

features we can also have a lot of

play20:22

benefits for just sharing

play20:24

types uh an example of this is uh using

play20:27

the strategy pattern first in an object

play20:30

oriented programing way and then we'll

play20:32

see how we can build the same strategy

play20:34

pattern now in a data oriented

play20:36

programming way so the strategy and

play20:38

object programming means you have an

play20:40

interface for example credit card which

play20:42

has a method pay we can have two

play20:44

implementations of of this credit card a

play20:46

Visa card and a Master Card and both

play20:49

have a dependency on the actual thing

play20:51

that the side effect that does the

play20:52

payment so Visa card has a dependency on

play20:55

Visa card payment and the Master Card

play20:56

has a dependency on MasterCard payment

play20:59

and a component only knows the the use

play21:02

case component only knows the interface

play21:04

so to put that into code uh we have one

play21:07

interface credit card which a method pay

play21:10

a Master Card implements a methods pay

play21:12

in its own way and a Visa card

play21:14

implements a method pay in its in its

play21:16

own way and now the logic component gets

play21:20

any credit card it doesn't know which

play21:22

one but it does know it can just call

play21:24

the pay method and the really nice thing

play21:26

about the strategy pattern that it's

play21:29

fully compiled time save if I would ever

play21:32

add a thir credit card uh the interface

play21:35

forces it to implement the payment

play21:37

method so this du logic will work on any

play21:41

credit card I would ever add into my

play21:43

system fully compiled time safe so my

play21:45

compiler can really help me here so this

play21:49

way it skills really well because I

play21:51

don't have to change basically any code

play21:52

if I add another credit card however um

play21:56

it does force us to have our payment

play21:59

methods together with our data so let's

play22:01

see how we could do the same in data or

play22:04

program so what we kind of want to

play22:06

create here is we still have an

play22:08

interface credit card which is either a

play22:10

Visa card or a Master Card but we want

play22:13

to remove all the logic and towards the

play22:15

component so now the component should

play22:17

know the Visa card payment and the

play22:19

MasterCard payment so to do this we can

play22:21

use Java's new records uh where we

play22:24

create the interface credit card and

play22:26

since we don't actually need any methods

play22:28

whatsoever we can just use a record

play22:29

because it only holds data so it only

play22:32

holds a the credit card number so it

play22:34

creates a record for Visa card and a CR

play22:36

record for MasterCard and this is where

play22:39

Java 21's switch statement comes in so

play22:43

we can now pattern match over the credit

play22:45

card and do an instance of check so in

play22:48

case it's an instance of MasterCard we

play22:50

put it in the variable MC and then we

play22:53

call the MasterCard payment component to

play22:55

do the pay the payment if it's an in of

play22:58

Visa card we put it into this variable

play23:01

and it can say tell the Visa card

play23:04

payment to do this and for a very very

play23:06

long time um this was the reason why we

play23:09

don't really use this pattern a lot in

play23:11

Java because we have to have a default

play23:14

statement here what if another credit

play23:16

cards get added uh then we will

play23:19

automatically go into the default mode

play23:21

and this is why um reduce all the

play23:24

compile time safety that we had with the

play23:26

previous strategy pattern because if I

play23:28

add another credit card I will get an

play23:30

eal State exception here well with the

play23:33

previous pattern uh if I would add in

play23:35

object oral programming if you would add

play23:37

a credit card there it would always work

play23:39

to overcome this ja introduced the

play23:42

sealed keyword so you create a sealed

play23:46

interface credit card which permits Visa

play23:49

card and MasterCard so basically we tell

play23:51

the compiler here the only two possible

play23:54

implementations of this credit card is

play23:56

either a master card or or a Visa card

play23:59

and there are no other implementations

play24:02

and because we told to compil this

play24:04

information if we P match over a sealed

play24:07

interface we now get this we don't need

play24:10

the default anymore since the compiler

play24:12

knows these are the only two possible

play24:14

implementations it will even Auto uh

play24:17

like if you use intell or something it

play24:19

would even with old enter complete the

play24:23

whole pattern match for you because it

play24:24

knows which are all the possible

play24:26

implementations of the credit card

play24:28

and if it would ever add a credit card

play24:30

in the future or remove one this switch

play24:33

statement no longer compiles so finally

play24:36

we have our compile time safety back

play24:38

again so we can actually use this

play24:40

pattern so until Java 21 we didn't

play24:44

really have the option to do this but

play24:46

during Java 21 what we actually finally

play24:49

created was our branching operation

play24:51

because that's what we're doing here

play24:53

given a credit card we either go one

play24:56

route or we go another route and and

play24:58

this is really really uh good since we

play25:02

can not not just use this for credit

play25:03

cards also think for example oh we get a

play25:06

contract if it's a signed contract go

play25:08

this way if it's unsigned contract go

play25:10

that way you get a result from a

play25:12

function it either succeeded or it

play25:15

failed we can use this pattern matching

play25:17

uh for all kinds of purposes and that's

play25:21

why I think this is really a game

play25:22

changer this is a new way of using

play25:24

polymorphism we didn't have access to

play25:27

until before Java 21 uh which also means

play25:32

um it will I think take a while before

play25:34

we get used to this because this is a

play25:36

feature that has been in for example

play25:38

Scala a lot for a very very long time

play25:41

and it it's also there in cotland uh but

play25:43

in Scala basically people don't write

play25:46

write if statements anymore basically

play25:49

everything is done through sealed

play25:50

glasses and patter matching to predict

play25:52

all to do all the flows and this also

play25:55

really helps you in thinking in state

play25:56

changes so I said um in the beginning uh

play26:02

when you think about an application it's

play26:04

mostly data transformation which is just

play26:06

State changes for example like I said uh

play26:09

a contract is it's signed do this if

play26:11

it's unsigned do that so with Java 21 we

play26:15

actually have a whole new way of using

play26:17

polymorphism and if you find it

play26:19

interesting I can tell a little bit more

play26:21

about that um after I finish the talk

play26:24

but in conclusion of uh data program in

play26:27

Java

play26:29

is most applications are just data

play26:31

streams if you look at them in a very

play26:33

abstract way um and data streams only

play26:36

have a map and a filter and a brand

play26:38

operation and it's basically just State

play26:40

Changers through data so why have types

play26:43

you could use hashmaps as well and this

play26:46

can be beneficial for performance uh for

play26:49

performance increases uh by separating

play26:51

States from functionality we can offer

play26:53

some better scaling because we can

play26:55

create components for every use case

play26:57

instead of all the use cases having to

play26:59

live into side a single component and

play27:01

thanks to um the new patter matching

play27:05

with instance of and SE classes we now

play27:07

have a game changer U with compile time

play27:10

safe branch operations and with that I

play27:12

would slly like to end uh with a

play27:14

slightly adjustable quote from Brian

play27:16

gets uh don't be a functional programmer

play27:18

don't be an object programmer don't be a

play27:20

data oriented programmer be a better

play27:22

programmer don't just write hashmaps

play27:25

everywhere right now uh all these things

play27:27

all these ways of thinking and using

play27:29

there are just Tools in your toolbox uh

play27:32

and most of often It's a combination of

play27:34

all three uh that's why we get the best

play27:36

out of your software um and with that uh

play27:39

that's the official talk uh if you have

play27:42

any questions we can go into those now I

play27:44

can also tell a little bit more about uh

play27:47

the what kind of big deal the new

play27:49

pattern matching thing is uh if you guys

play27:52

like uh if you have any questions uh

play27:55

later you can add me on Twitter or X as

play27:57

it's been called now or LinkedIn I'm

play27:59

actually a bit more active on LinkedIn

play28:02

also have a website if you want to see

play28:03

any slides of me uh of all my talks or

play28:06

uh any recorded talks they will all come

play28:08

on this website so this talk has been

play28:10

recorded on Heaton before he before so

play28:13

that's also a way to look it back I do

play28:15

want to give some attention to this uh

play28:17

this is a website I'm also a member of

play28:20

um it's a place where people can either

play28:23

offer to Mentor others for voluntarily

play28:25

or you can look for a mentor uh so

play28:27

everything is unily uh no cost to any

play28:30

party and I already helped like five or

play28:33

six people there and it's just a really

play28:35

rewarding feeling if you can help

play28:36

someone so if you have any skills and

play28:38

think you can help if you can help

play28:39

someone else please register as a mentor

play28:42

otherwise if you're looking for a mentor

play28:44

um you can go to that website as well I

play28:47

like I said I'm quite bit big on

play28:48

knowledge sharing and I would like to

play28:51

have this kind of knowledge accessible

play28:52

to as many possible people uh and most

play28:56

of the skills I possess now almost all

play28:58

come through teaching others so it's a

play29:01

really good investment not only in

play29:02

others but also in yourself and if you

play29:05

just want to see the sheets you can find

play29:07

these on this URL so with that are there

play29:10

any

play29:12

questions s Kumar here I think there are

play29:15

a couple of questions Raja has a

play29:17

question so before that I will go with

play29:19

mine uh so um considering that this data

play29:23

oriented programming can work with the

play29:26

latest version of java that is only when

play29:28

we use sealed classes and record which

play29:30

is a recent Edition and how do you see

play29:32

the how do you see the adaption uh of

play29:35

these features because uh the community

play29:37

itself is lagging right most people will

play29:39

are there in 11 only so for an existing

play29:42

programmer in your team did you find any

play29:44

any problems or how is it actually that

play29:46

option um the thing is that uh well

play29:51

first of all for any Paradigm you

play29:53

actually don't need that many language

play29:56

features um it's like just with

play29:59

functional programming like Java can do

play30:01

functional programming very very well

play30:02

you don't need a Scala to do that it's

play30:04

more of a way of thinking and applying

play30:06

those patterns and of course some

play30:09

languages do things more easily than

play30:11

others like without SE classes this

play30:13

stuff becomes a bit harder to do um and

play30:17

the thing is that I think it will using

play30:20

this sealed class SP Manching I think it

play30:22

will take a long time before we actually

play30:24

start using it because people don't know

play30:26

this way of thinking um

play30:28

because uh well we we can go a little

play30:30

bit into um uh Theory because up until

play30:35

then this is a completely new thing in

play30:37

Java um we have something called type

play30:40

Theory where you think about types and

play30:42

we have this very fancy word uh called

play30:45

uh an ADT which stands for altic data Ty

play30:49

type which is just a fancy word for an

play30:52

immutable object and you have two

play30:54

different immutable objects two

play30:56

different kinds of altic data types we

play30:59

have product types and we have some

play31:02

types um product types are just like if

play31:05

you would make any uh immutable class

play31:08

you were probably making a prodot type

play31:11

so what do we mean with that a person is

play31:13

for example has a name and an age so if

play31:17

you want to create all possible persons

play31:19

you have to do all possible names times

play31:22

all possible ages so it's a product of

play31:25

all names and all ages so this this is a

play31:28

product type uh a different way um to

play31:31

see that is this is a and type a person

play31:34

is a name and an H A some type is an or

play31:38

type or type is for the only thing we

play31:41

had in Java uh was an enim for example

play31:45

it's uh

play31:47

Monday uh or Tuesday uh and it can't be

play31:51

either of them so it's this one or this

play31:54

one this is what we call a some type

play31:56

thanks to sealed glasses we can finally

play31:59

have other some types instead of enm in

play32:03

Java which means that for example the

play32:06

Java optional uh could now be

play32:09

reimplemented uh as a seal type using uh

play32:12

none and some as um which now if you

play32:16

return an optional it's either an

play32:17

instance of none or it's an instance of

play32:19

some containing something which allows

play32:21

you to do the whole branching pattern

play32:23

matching over it so this is for example

play32:25

how language like Scala does this kind

play32:27

of stuff

play32:28

yeah I should sorry I just read chat

play32:32

this is for example how languages like

play32:33

Scala do it all the time but this

play32:36

feature has been in Scala from the start

play32:38

so they had years and years of

play32:40

experience in this way of thinking and

play32:43

Java 2 we just had this like Java is

play32:46

what 25 years old now or something we

play32:48

never had this before so we have this

play32:50

whole Legacy of thinking not in this

play32:54

part not in this way of thinking so now

play32:56

we finally get to death point Point um

play32:59

so I think it's going to take me a while

play33:01

before we adapted our way of thinking to

play33:03

see how powerful this way of working can

play33:05

be yes yes and just curious maybe I my

play33:09

question may be stupid question so um

play33:11

can you tell me the the use cases where

play33:14

you see you can see some immediate

play33:16

benefit for example is this relevant to

play33:18

our or related items where we interact

play33:21

with databases do you see data oriented

play33:23

programming will have an impact on these

play33:25

core fundamental areas in interface

play33:27

appli

play33:28

in what kind of in Enterprise

play33:30

application uh object relational mapping

play33:33

database jdbc interactions and all those

play33:35

stuff where we do lot of data

play33:37

transformation um in practice I don't

play33:41

think so um I see this way more as a the

play33:45

control flow of your software will can

play33:47

be quite different in practice I don't

play33:50

see um all the reflection being changed

play33:54

Going to hashmaps and such and this

play33:56

stuff doesn't really

play33:58

help a lot with object relational

play34:00

mapping so in practice in most

play34:03

enterprise software I don't think people

play34:05

use it in that way um however uh using

play34:10

some types and using Des for control

play34:11

flow like using the branch operation

play34:13

based on type I think that's going to be

play34:15

a thing um I already started using that

play34:19

in some applications and people who

play34:21

didn't know the concept uh didn't

play34:24

actually need that much explanation to

play34:26

see oh this is actually quite useful

play34:29

uh also in practice it's very easy to do

play34:31

since most interfaces in most G most

play34:35

even class or interfaces that you create

play34:37

can become sealed it doesn't have any

play34:40

downside of making them

play34:42

sealed um since you're not really using

play34:46

the like if if you have a library um you

play34:49

can even use it to add compile them

play34:51

safety uh because you can normally you

play34:54

have to make classes final and such to

play34:56

prevent others to extend it now with

play34:58

seals you you have a different way to uh

play35:00

prevent people from extending from it so

play35:03

I'm yes thinking out loud uh so it might

play35:06

not be completely organized all right so

play35:08

I see can we add annotations to

play35:09

parameter of record class oh yeah yeah

play35:11

that works if you have a record uh that

play35:13

just works uh I just saw this

play35:16

question we can name the method to say

play35:18

FEA pay in the dator approach um I don't

play35:23

that would break the pay comes from the

play35:26

interface so that would break the

play35:28

interface so I don't really fully

play35:31

understand the question yeah can you

play35:33

show me that slide where you had switch

play35:36

case yeah okay yeah you're saying credit

play35:40

card is still an interface then why

play35:41

should I even put a case and then call M

play35:45

mastercard. pay I can directly call

play35:47

credit card.pay right

play35:50

um yeah uh why by you put it in a

play35:54

separate variable yeah instead of two

play35:56

switch cases yeah uh yeah well um if I'm

play36:01

depending only on the credit card

play36:03

interface yeah in this case M Vis don't

play36:06

have any uh yeah as in you uh don't have

play36:10

any uh methods by itself so right now

play36:15

this is doing all the logic and this all

play36:18

this takes only a MasterCard which

play36:21

that's why we need to do uh pattern

play36:23

matching on instance off so which means

play36:26

you know the question I asked you know I

play36:28

can name this Visa card in the different

play36:30

method name um yeah which which method

play36:35

are you referring to now the second case

play36:40

you mean this thing uh exactly I can

play36:43

yeah I oh yeah yeah yeah but this this

play36:45

is uh okay then I B this is just a

play36:48

dependency of the component that calls

play36:50

this this right now it's a um static

play36:53

call so this this is something different

play36:56

from this

play36:58

um this could be called Visa pay if you

play37:00

like but for this example it wouldn't

play37:03

make a difference because any credit

play37:06

card is just a record it is yeah yeah

play37:09

this is just a record and this is just

play37:11

data that's being passed to basically

play37:14

this is in case it's a Master Card I

play37:16

want you to do this with a Master Card

play37:18

if it's a Visa card I want you to do

play37:20

that with a Visa card oh T I have one

play37:22

question on this right so um given that

play37:24

record is more often we we will be using

play37:26

mostly

play37:28

uh in the data classes which are more of

play37:32

an immutable in nature right

play37:34

most now given that we also have

play37:37

something called as a builder where we

play37:39

also try to do some immutable classes

play37:42

there as well with the state what you

play37:43

state we Define the Builder and we just

play37:45

pass on the different attribute that

play37:47

will become my once object I can't modif

play37:49

so with the builder as we try to a the

play37:51

immutability there although it gives a a

play37:54

better fluent way of building the

play37:56

objects with the optional But ultimately

play37:58

it provides me the immutable objects

play38:00

right now

play38:02

I I have the debate on the these two

play38:05

things now whether you should go with

play38:07

the record class in that Cas or whether

play38:09

to go with the builder classes

play38:12

on yeah um this is all on immutability

play38:17

and Java is not good in immutability

play38:20

right now um one thing that uh ROK does

play38:25

add it has inde an add Builder

play38:26

annotation I personally don't like

play38:28

Builders uh because that's uh uh in a

play38:32

lot of cases it's a missing language

play38:34

feature since Builders uh allow you to

play38:37

build an object without passing all the

play38:41

the properties it needs to pass and then

play38:43

it will fill at runtime which means if

play38:45

you call a Constructor if you would add

play38:48

something to that Constructor you now

play38:50

get a compilation time check that this

play38:52

is not going to work or these are the

play38:53

places where you need to fix something

play38:56

uh by using a builder you lose that

play38:57

compile time safety so that that's the

play38:59

bit what I don't like about Builders um

play39:03

that being said lbox builder at Builder

play39:06

has uh something where you can take an

play39:08

existing object call a two Builder

play39:11

method on it and then you can set one

play39:13

property to be different and then call

play39:15

build again that would work but in Java

play39:19

we will get a better way to do this in a

play39:21

newer version of java and

play39:25

basically chains only one property of an

play39:28

existing object uh if it's a record so

play39:31

this way um we do get around a bit about

play39:36

bit of um Quality of Life features

play39:38

regarding immutable objects so this

play39:41

stuff like this is coming in Java okay

play39:45

does that help bit in answering your

play39:47

question bit of help there yeah uh I had

play39:50

one debate yesterday with my senior

play39:53

person and and his thought also valid I

play39:55

just want to get your opinion one of the

play39:57

question that we have we have been

play39:59

discussing in PR if you look at the past

play40:02

we now if you look at the oriented

play40:04

program as well where we Define a data

play40:06

model we Define some attributes and then

play40:09

we say okay at theate getter at theate

play40:10

Setter we Define the get Setter and

play40:13

whenever I need that object I just use

play40:15

the get method get attribute and then

play40:17

set attribute right now yeah if you

play40:20

think of the data programming we

play40:22

generally used to have this set method

play40:24

get method where you want to have some

play40:25

control over my attribute that I'm going

play40:28

to set from externally to model where

play40:30

you can write some business logic to

play40:32

validate my some of the values that I'm

play40:33

passing it right that was a intentional

play40:36

in in in the past nowadays it's more

play40:38

about just transforming the object one

play40:40

state to another state one object to

play40:42

another object just setting of those

play40:43

some attributes right um and rather than

play40:47

having this Setter and getter methods

play40:49

itself why not to have but object dot

play40:52

attribute name is equal to something

play40:54

some different value set meth because

play40:57

now noways it's just the data

play40:59

transformation is just only holding the

play41:00

object for temporary my purpose and then

play41:02

I'm transforming to different state with

play41:03

a different model all together right so

play41:06

he has a thought that okay if you're not

play41:08

writing any business logic it's just a

play41:09

data model that I have he he likes to

play41:12

write okay the object do attribute name

play41:14

is equal to something it gives more

play41:15

readability as well in terms of it and I

play41:17

know that there's not going to hold any

play41:19

business logic it's just as a Val I'm

play41:21

going to set and just passing along the

play41:23

my different functional or different use

play41:25

cases so yeah so I just want to

play41:28

understand your thought on this model of

play41:30

parad that's what they trying to say say

play41:32

p do set name he doesn't like it but he

play41:36

he WR but he like but like he like this

play41:38

this way yeah I I don't like mutable

play41:41

State um since it's almost five um the

play41:46

thing what I hate about mutable state if

play41:49

our car here was a mutable car and we

play41:53

have this bit of software where I print

play41:54

out the car I pass the car to my paint

play41:57

method and then I print out the car

play42:00

again do these two things print out the

play42:03

same color we don't know we don't know

play42:06

if if the car is mutable we don't know

play42:08

if this method changes it or not so the

play42:10

only way to know is going into this

play42:12

method hopefully see it there and hope

play42:14

that method doesn't call another method

play42:16

with the car such as in we need to know

play42:18

quite a bit of our software in order to

play42:20

know if these two things print out the

play42:22

same thing if we however would create an

play42:24

immutable car and now ask the same

play42:28

question do these two Sprint out

play42:30

statements print out the same thing the

play42:32

answer must be yes because the car is

play42:34

immutable correct so this is where

play42:37

immutable objects really reduce the

play42:39

cognitive load of software engineering

play42:42

because I can instantly say information

play42:45

things about this code uh that has to be

play42:47

true and I don't have to read everything

play42:50

that this does uh the downside is yeah

play42:54

if you want to change values you have to

play42:56

create a new instance but which means

play42:58

you have to return something what

play43:00

happens now is if you now ask question

play43:03

do these two things print out the same

play43:05

thing probably not because you can see

play43:09

the state change going on here because

play43:11

it needs to be a return value of this

play43:13

function uh therefore uh or you can

play43:17

either instantly tell uh nothing changed

play43:20

or you can immediately tell something

play43:22

must have changed and this is what I

play43:25

really like about reducing cognitive FL

play43:28

uh by using immutability uh because if

play43:30

things are mutable uh yeah you don't

play43:33

know where in the software variables can

play43:35

get set which means you can't rely on it

play43:38

which means you have to know have to be

play43:40

in control of your entire program well

play43:43

here I can instantly see oh I probably

play43:46

need to maybe check some things after

play43:48

this or at least see a state change uh

play43:51

has taken place so I don't like mutable

play43:54

state it has it it has some uh benefits

play43:59

in terms of um performance uh but if

play44:03

you're going to do mutable state for

play44:05

performance sake I kind of like my um

play44:09

functions to have an immutable state in

play44:12

and an immutable object out and only mut

play44:14

mut all the mutable state is within

play44:16

function scope uh this way the mutable

play44:19

State doesn't escape this function and

play44:21

therefore the rest of the software um

play44:25

can think in terms of immutable in

play44:26

immutable out and has all the safety of

play44:30

immutable software of immutable objects

play44:33

so in this case because this mutable

play44:34

variable and this mutable thing doesn't

play44:36

escape the function the rest of the

play44:39

software doesn't know if mutable or

play44:40

immutable state is being used here I

play44:42

could completely change its

play44:43

implementation to an immutable one the

play44:45

software wouldn't know I can change it

play44:47

back the software wouldn't know so

play44:49

mutable States in my opinion like you

play44:52

can use it but be careful with it and if

play44:54

you do it try not to have it Escape

play44:57

function scope uh because that's the

play44:59

point where the cognitive load becomes

play45:03

way way higher than it it can be got it

play45:06

got it y thank you yeah so so my I mean

play45:10

pleas my understanding correct me if I'm

play45:11

wrong uh my perspective toward sunil's

play45:14

question is uh Sunil uh we as a

play45:17

programmer very familiar with writing

play45:20

mutable code right sets and gets correct

play45:23

corre correct okay actually the sets and

play45:26

gets when these were introduced we used

play45:29

to develop UI applications in Javas

play45:31

swing and all those early days of java

play45:34

at that point in time sets and gets were

play45:36

crucial part of our user development at

play45:38

that point in time the default choice of

play45:40

a language was

play45:42

mutability but later as the hardware

play45:46

accelerations all this modern

play45:48

Technologies came people realized

play45:49

particularly web publication developers

play45:51

realize that immutability is the real

play45:53

strength immutability is supposed to be

play45:55

the default and you can break and then

play45:57

go to mutability not the reverse that is

play45:59

the realization that is that is the

play46:01

direction where now going with record

play46:04

you get immutability by default but the

play46:07

drawback is your

play46:08

programming uh perspective that is so

play46:11

far we have been comfortable with iners

play46:13

and gets which will take some time in

play46:15

fact this was one of the question to the

play46:17

jdk development team the answer to that

play46:19

question is that the person was so

play46:21

critical the one who implemented it he

play46:23

simply said no it is supposed to be done

play46:25

in this way but again again and again

play46:27

Java Community people are asking T's

play46:30

answer was actually refreshing to me

play46:31

also because uh because of the community

play46:34

demand I think now Java is coming up

play46:35

with a new structure where they are

play46:37

matching both the performance benefit of

play46:39

immutability as well as that syntactical

play46:42

thing they are adding something new

play46:43

which is a new thing for me also because

play46:45

at that point in time the team was

play46:47

entirely against going with that sets

play46:48

and gets again that it's not the

play46:50

direction that we should go fully agree

play46:52

um this talk is a very if you're

play46:55

struggling with that this is a very good

play46:57

talk to watch a simple mid E from Rich

play47:00

hickey um because he puts it in uh a

play47:04

completely different

play47:05

perspective uh since simple is something

play47:09

else than easy because easy is a easy

play47:12

versus heart and simple is simple versus

play47:14

complex so uh the thing is that if you

play47:17

know something it's easy if you don't

play47:19

know something it's hard so simple

play47:23

versus easy easy versus hard is very

play47:25

subjective that's different from person

play47:27

to person uh while immutable state is

play47:31

inherently simpler than uh is than IM

play47:34

mutable State mutable state by

play47:36

definition is complexer than immutable

play47:38

state so that's an objective thing you

play47:41

can't disagree with that because it's

play47:44

it's it is a fact um but it's also if if

play47:49

you can see in that way that this

play47:50

discussion is a easy versus hard thing

play47:54

uh that just means if you start using it

play47:57

more and more and more if you start

play47:59

learning it it will become easy so um

play48:03

that that's with a lot of things where

play48:04

people are like oh this is this is way

play48:07

too complex or something then you have

play48:09

to take look does that person mean is

play48:11

it's actually more complex or is it hard

play48:14

for that person because if it's hard for

play48:16

that person that's something you can fix

play48:19

that's that's just teachable and then it

play48:22

will slowly become easy or is it

play48:24

actually more complex yeah then it's

play48:26

wrong because we should favor simple

play48:28

approaches so this is a very very good

play48:31

talk about that uh this has really

play48:33

changed my view on software engineering

play48:36

sure I'll also go go through this talk

play48:38

definitely uh M you have a question

play48:40

right you can unmute and speak yes

play48:42

please goad sure yeah so my question was

play48:45

more on the sealed interface so I have

play48:49

experience in Escala so I I you seal

play48:51

trade there so do we get um you know

play48:56

more than a

play48:57

in in the Java 21 feature that along

play49:00

with this sealed interface and another

play49:02

thing inala we Define all these in one

play49:04

file and generally in Java we will have

play49:07

one class in a file right so uh how do

play49:10

we put these sealed interface and

play49:13

Records in one file or multiple

play49:16

files works that actually does it better

play49:19

than Scala uh you can choose um right

play49:22

now um I I did something here that

play49:25

that's not fully correct

play49:27

um I can omit this if it's in the same

play49:30

file um I can just write it like this if

play49:33

I want these things to be in different

play49:35

files then you need the permit then you

play49:37

need the permit keyword so with this

play49:40

Visa card can be in a completely

play49:41

different file and Master Card can be a

play49:42

completely different file or you can

play49:45

write it without this and then declare

play49:46

them in the same file and that would

play49:47

also work right so thank you sorry one

play49:51

more followup question so like what I

play49:53

see uh nowadays right so in Java so all

play49:57

the features that that is there in the

play49:58

cotlin OR Scala they are slowly adding

play50:01

it to Java yeah so uh if if we look into

play50:05

functional programming uh so I just

play50:07

wanted to know your opinion uh is it uh

play50:10

good to uh to still stick with Scala

play50:13

because that is much more powerful and

play50:16

gives more features in functional

play50:18

programming so should should should

play50:20

developers stick to that or should start

play50:23

looking to Java side because I I think

play50:24

still there are a lot of things that are

play50:27

not added to the Java even at this

play50:29

moment so basically that's what this

play50:32

whole talk is about um that's uh about

play50:36

functional programming in Java um in a

play50:40

way functional programming can be done

play50:43

in any language uh it's mostly a way of

play50:46

thinking and a way of a certain approach

play50:48

to problems and so therefore yes you can

play50:52

perfectly do functional program in Java

play50:55

that being said uh language features do

play50:57

help with it so Scala is way more

play51:01

powerful than J basically any other

play51:05

language on the jvm um regarding to

play51:09

functional programming so if you really

play51:12

just want to go heavy into that Paradigm

play51:15

Scala will help you more than Java if

play51:18

you however are programming Java U the

play51:21

fact that you're not using skyar culon

play51:23

doesn't mean you can't apply all these

play51:25

principles it's just things like

play51:28

everything is knowable by default in

play51:30

Java so you kind of have to do um be

play51:36

what's the word um uh you kind of just

play51:39

have to make sure you don't abuse that

play51:42

and you kind of have to assume other

play51:44

people don't abuse that but given a few

play51:46

assumptions here and there you you can

play51:48

still do functional programming in Java

play51:51

you can always do better in Scala but

play51:54

you can get not decently close in Java

play51:57

these days um the thing about Scala is

play52:00

that uh because it's so

play52:02

powerful um uh people tend to write

play52:06

things in 10 different ways because you

play52:09

can write it in 10 different ways which

play52:11

means that you kind of have to know

play52:12

Scala quite well and it took me my first

play52:16

attempt to learn Scala I couldn't get

play52:18

through it then I learned cotlin and

play52:20

then using that knowledge I learned SC

play52:22

tried Scala again and then I could get

play52:25

Scala uh but scar is is uh not an easy

play52:29

language to get introduced to I do think

play52:31

scar is a beautiful language yeah I do

play52:34

and I was uh I mean the only only uh

play52:38

thing that I felt uh many times uh

play52:41

compilation is a bit slow compared to

play52:44

Java that I felt otherwise the the power

play52:47

the features that we get in scalar is

play52:48

much better but but what I see now in

play52:51

Java uh most of the you know useful

play52:54

features we are getting to the Java

play52:57

and yeah so I was looking into that like

play53:01

whether I should Explore More on what is

play53:03

coming in Java and and try to learn that

play53:07

and we need people like you because you

play53:09

worked with sealed classes before and

play53:11

sealed interfaces you know how to use

play53:13

those and most Java Engineers don't know

play53:15

that so we need people like you with

play53:17

experience in this to teach all the

play53:20

whole Java community and show people the

play53:22

power of using some types like this so

play53:26

uh please share your knowledge with

play53:28

other people about Scala because it's

play53:30

relevant to Java people right now yeah

play53:32

so thanks a lot sorry we we took extra

play53:35

time from UT uh and thanks for uh the

play53:38

very useful session uh it was very

play53:41

useful for every one of us

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Data-Oriented ProgrammingJava ParadigmsSoftware EngineeringImmutabilityPolymorphismFunctional ProgrammingObject-Oriented ProgrammingJava FeaturesRecord ClassesSealed Interfaces
¿Necesitas un resumen en inglés?