Selsoft since 2008. • My interest areas are software development and design, programming languages, OOP and FP. • I’ve been working with Java since its arrival. • You can reach me through various ways: • Email: [email protected] & [email protected] • LinkedIn: www.linkedin.com/in/akinkaldiroglu/ & Twitter: @kaldiroglu About Me
fi rst been introduced as a preview feature in Java 14 and then became standard as part of Java 16. • Record is a speci fi c type of class with some restrictions. 4 • It provides nice opportunities for modeling and coding.
record requires only a name and list of components as JLS says are declared and rest is handled by the Java compiler: • A private and final fi eld for each component so records are all immutable objects! • Notice that a record can have a component of a mutable object type, so it should be avoided for truly immutable records 6
public accessor method with the name of the component • State of a record is always reachable! • A canonical constructor that receives arguments for components • equals(), hashCode() and toString() method implementations based on the components 7
extends clause but they can implement interfaces • They can’t be abstract, sealed or non-sealed • Other than declared components no more fi elds except static ones can be de fi ned, • Even components themselves can’t be rede fi ned as fi elds 8
But they need to call the canonical constructor, though • New methods can be added but they can’t change the state of the object: • So records should always have an immutable interface! 9
be overridden but the implementations should not violate the semantics of the record • equals() and hashCode() should always work on the components of the record 10
abstraction, JLS de fi nes a record as a simple aggregate of values. • And information hiding is not applied, components are always reachable! • So records can be used as carrier of data like DTOs 11
values i.e. if the value of any of its member variables changes the object becomes another object. • That’s not a state change! Values don’t change! • So the differentiating aspect of those objects is their state i.e. totality of the values in their fi elds. 16 record Name(String firstName, String middleName, String lastName){}
value objects. • Entities vs. value objects! • Value objects are frequently smaller, don’t have a complex lifecycle as entities and are mostly used to model immutable things such as numbers as money or date or strings as name. • They don’t have any identity as entities and thus are compared by their values. 17
is embedded in a larger non-value semantics: a confusing semantics! • We tend to think in terms of reference semantics so noticing value semantics is kind of hard. • Entities through their reference semantics mostly overweight and shadow value semantics. • Data-oriented thinking also emphasizes objects with ids. 21
means we need a retirement party Date partyDate = retirementDate; // but that date is a Tuesday, let's party on the weekend partyDate.setDate(5); assertEquals(new Date(Date.parse("Sat 5 Nov 2016")), retirementDate); // oops, now I have to work three more days :-( https://martinfowler.com/bliki/AliasingBug.html
vocabulary for better mental models: • More functional thinking • OOPLs tend to become more functional and their programmers should too! • Better object-oriented thinking • DDD has entity, value and service objects 25
always created by extracting details and keeping related, minimal core together. • Extracting parts of an entity that are used together into another object always helps keep objects small and cohesive. 29
final String middleName; private final String lastName; private final String zipCode; private final String city; private final String neighbourhood; private final String street; private final String building; ... } class Employee{ private final Tckn tckn; private final Name name; private final Address address; ... }
final String middleName; private final String lastName; private final String zipCode; private final String city; private final String neighbourhood; private final String street; private final String building; ... } • Where do you think the logic in records would be placed if the records were not used?
in clean code circles. • Primitives lacks cohesion and causes coupling by allowing logic about themselves to get scattered. • So adding behaviors to records increases the cohesion and reduces the coupling of the system by avoiding scattered logic. 35
ontology can only be achieved by rich naming. • When number of names increases our language gets richer and in turn our world enlarges. There are only two hard things in Computer Science: cache invalidation and naming things. Phil Karlton 37