SlideShare a Scribd company logo
1 of 34
The Science and Art of 
Backwards Compatibilty 
Ian Robertson
Agenda 
â—Ź Where does backwards compatibility matter, and why? 
â—Ź Evolving Serializable types 
â—Ź Ways a library can evolve 
â—Ź Strategies for evolving a library in a backwards compatible 
fashion 
â—Ź Best practices
About Me 
Application Architect at Myriad Genetics 
14 years experience of working on large-scale Java projects 
Amateur carpenter 
Blog: http://www.artima.com/weblogs/?blogger=ianr 
Twitter: @nainostrebor 
https://github.com/irobertson
Where Compatibility Matters: Serialization 
â—Ź Distributed computing 
– Web Services, RMI, JMS, data storage, caching, etc. 
– Not all clients can upgrade at the same time 
– Old data can live for awhile (JMS, caches, files) 
– Backwards compatible serialization changes allows 
communications between different versions of writer and 
reader 
– Not just Java serialization – also XML, JSON, Avro, Protocol 
Buffers, etc.
Evolving Serialized Classes 
â—Ź Step 0: 
public class Foo implements Serializable { 
private static final long serialVersionUID = 1L; 
… 
} 
– If you forget this, you can use serialver later 
â—Ź Change this only if you wish to intentionally break backwards 
compatibility
Ways a Serialization Form Can Evolve 
â—Ź Adding a new field 
â—Ź Removing an existing field 
â—Ź Changing allowed values for a field 
â—Ź Changing the type of a field 
â—Ź Moving fields in inheritance hierarchy 
â—Ź Change the values for an Enum
Adding a Field 
â—Ź Reader upgrades first 
– Reader needs to accept absence of new field 
â—Ź Writer upgrades first 
– Reader needs to already have been designed to ignore 
unknown fields 
â—Ź Java Serialization handles both cases well, as do JaxB, Jackson, 
and many others 
– Unknown fields are ignored 
– Missing fields receive default values
Adding a Field 
â—Ź Semantic Compatibility 
– Behave well when new field has default value 
– New field should not change meaning of old fields! 
Version 1 
public class Dimensions { 
// measurements in meters 
public int length; 
public int width; 
public int height; 
} 
Version 2 
public class Dimensions { 
public boolean useMetric; 
public int length; 
public int width; 
public int height; 
}
Removing a Field 
● Similar to adding a field – switch the role of reader and writer 
● Reader upgrades first – ignore the old field 
● Writer upgrades first – reader gets default value 
â—Ź Again, also pay attention to semantic compatibility! 
â—Ź Renaming a field is an add and remove
Changing Allowed Values for a Field 
â—Ź In general, this is a semantic issue 
â—Ź If allowing more values, readers need to either upgrade first, or 
be able to handle previously unaccepted values 
â—Ź If the set of allowed values is constrained, writers must upgrade 
first 
â—Ź Default serialization bypasses all constructors!!!
Changing the Type of a Field 
â—Ź Java Serialization: 
– Writer can send subtype of what reader expects (Liskov) 
– Reader cannot require a subtype of what writer sends 
â—Ź More generally: 
– The reader needs to be able to process what the writer 
sends 
â—Ź Alternative: add a new field with the new type 
– Have the old field “forward”
Forwarding Field Version 1 
public class Invoice implements Serializable { 
private float amount; 
public float getAmount() { 
return amount; 
} 
public void setAmount(float amount) { 
this.amount = amount; 
} 
}
Forwarding Field Version 2 
public class Invoice implements Serializable { 
private float amount; 
private BigDecimal totalAmount; 
public void setAmount(float amount) { 
this.amount = amount; 
this.totalAmount = new BigDecimal(amount); 
} 
public BigDecimal getTotalAmount() { 
return totalAmount != null 
? totalAmount : new Big Decimal(amount); 
} 
}
Moving Fields in Class Hierarchy 
â—Ź Fields in a serialzed form belong to a particular class 
Version 1 
public class Person 
implements Serializable { 
} 
public class Employee 
extends Person { 
String name; 
} 
Version 2 
public class Person 
implements Serializable { 
String name; 
} 
public class Employee 
extends Person { 
} 
Unrelated Fields!!!
Enums 
Multiple options for encoding: 
â—Ź Encode the name (Java Serialization does this) 
– Refactoring the name of a value will cause incompatiblity 
â—Ź Encode the ordinal (Protocol Buffers does this) 
– Refactoring the order or names of enum values can cause a 
hard to diagnose incompatibility! 
â—Ź Best practice: use independent names for en/decoding 
– Don’t use these for anything else!
Detecting Incompatible Changes 
â—Ź For each new version, write a file consisting of serialized forms 
of various instances of your serialized classes 
â—Ź Write unit tests that verify ability to read these classes 
â—Ź Only delete tests for versions no longer supported for 
backwards compatibility 
● Alternative – unit test loads older version of library to create 
serialized form on the fly
Custom Serialization and Deserialization 
â—Ź Useful for otherwise backwards-incompatible changes 
private void readObject(ObjectInputStream ois) 
â—Ź ObjectInputStream.readFields() 
– Easy to use 
– O(n2) in the number of fields! 
â—Ź To allow older versions to read newer version, implement 
private void writeObject(ObjectOutputStream oos) 
â—Ź Externalizable: Developer has near total control and responsibility
Where Compatibility Matters: APIs 
â—Ź Library APIs 
– Why can’t my clients just upgrade? 
CSVParser 
WeatherStatsReader 
CommuteRecommender 
TrafficDataReader 
– CSVParser releases new, backwards incompatible version 
– WeatherStatsReader adds new functionality, upgrades its 
dependence on CSVParser 
– TrafficDataReader has not updated to use new CSVParser 
– CommuteRecommender wants to use new version of 
WeatherStatsReader, but cannot!
API Backwards Compatibility 
â—Ź Source compatible: 
– Existing source code compiles against new library 
â—Ź Binary compatible: 
– Code compiled against the old version will successfully link 
against the new version 
● Neither implies code will continue to successfully run, but it’s a 
good start
Dangers of Source Compatibility 
â—Ź Good enough for APIs only used by applications, but not for 
libraries used by other libraries 
â—Ź Easy to make source compatible, binary incompatible changes 
and not notice
JVM Method Calls 
â—Ź The JVM has two (traditional) ways of calling instance methods: 
– InvokeInterface – interface-defined methods 
– InvokeVirtual – class-defined methods 
â—Ź Clients call a specific signature of a method, including 
parameter types and the return type 
â—Ź No type conversions are performed in looking up methods
Example Byte Code 
ArrayList<String> arrayList = new ArrayList<String>(); 
List<String> list = arrayList; 
arrayList.add("x"); 
// ldc #1 - String x 
// invokevirtual #2 - ArrayList.add:(LObject;)Z 
list.size(); 
// invokeinterface #3 - List.size()I 
â—Ź Method signatures must match on parameter types, return type, 
and interface or class
Binary Compatibility: Adding Methods 
â—Ź Adding a method to a class is fine 
– but consider whether subclasses may already have 
implemented the same method with different meaning 
â—Ź Adding a method to an interface which clients do not implement 
is fine 
â—Ź Adding a method to a client-implemented interface can lead to 
runtime NoSuchMethodError, but only if called! 
– Allows adding methods to the javax.sql interfaces
Default methods 
â—Ź Allows adding new methods to client-implemented interfaces 
public interface Person { 
double getHeightInFeet(); 
/** 
* @since 1.1 
*/ 
default double getHeightInMeters() { 
return getHeightInFeet() * 0.3048; 
} 
}
Binary Compatibility: Removing methods 
â—Ź Removing a method which clients do not call is fine 
● Best practice – deprecate a method long before removing it 
(and javadoc the preferred alternatives!) 
â—Ź Note: changing a method signature has the same impact as 
removing it!
Binary Compatibility: Changing methods 
â—Ź When parameter types change, method overloading allows you 
to keep old signatures around 
â—Ź The Java language does not allow overloading on return type 
â—Ź The Java Virtual Machine does allow overloading on return type 
â—Ź In fact, it does this for you behind the scenes for subclasses
Specializing Return Types 
public class SelfCaused extends Exception { 
@Override 
public SelfCaused getCause() { return this; } 
} 
public SelfCaused getCause(); 
0: aload_0 
1: areturn 
public Throwable getCause(); - marked “synthetic” 
0: aload_0 
1: invokevirtual #2 - getCause:()LSelfCaused; 
4: areturn
Bridge Method Injector 
â—Ź Written by Kohsuke Kawaguchi (creator of Jenkins) 
â—Ź http://bridge-method-injector.infradna.com/ 
â—Ź Uses an annotation processor combined with a Maven plugin to 
add synthetic methods for overloading on return type
Bridge Method Injector Example 
Version 1.0: 
public Foo getFoo() { … } 
public Bar getBar() { … } 
Version 1.1: 
@WithBridgeMethods(Foo.class) 
public FooChild getFoo() { … } 
@WithBridgeMethods(value = Bar.class, 
checkCast = true) 
public BarParent getBar() { … }
Binary Compatibility: Class Hierarchy 
â—Ź Adding to the list of implemented interfaces is fine 
â—Ź Changing the direct superclass can be fine: 
– Provided no client-referenced super class is removed from 
the hierarchy, and 
– Provided no superclass defining a non-overridden client-called 
method is removed 
â—Ź Adding method to the child class will help 
â—Ź Similarly, removing interfaces is fine, subject to client references 
and calls
Binary Compatibility: Class vs Interface 
â—Ź Recall that JVM has separate byte codes for invoking interface 
and class methods 
â—Ź Just as the caller must have the right signature, it must also 
have the right invocation type 
â—Ź Changing a class to an interface, or visa-versa, is a binary 
incompatible change
Detecting Incompatible Changes 
â—Ź Writing compatibility tests 
– Exercise clients compiled against each older version 
– Also a useful strategy for web services, JMS, etc. 
â—Ź Clirr: http://clirr.sourceforge.net/ 
– Compares two versions of a jar, looking for incompatibilities 
– Plugins for ant, maven and gradle
If Compatibility Must be Broken 
● Don’t! 
â—Ź Really? 
â—Ź Change your major version number 
– http://semver.org/ 
â—Ź Consider changing project and package names 
– Allows clients to use libraries depending on both versions 
– JarJarLinks can allow clients to do this, but it puts the 
burden on them
Questions?

More Related Content

Recently uploaded

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 

Recently uploaded (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data ScienceChristy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

The Science and Art of Backwards Compatibility

  • 1. The Science and Art of Backwards Compatibilty Ian Robertson
  • 2. Agenda â—Ź Where does backwards compatibility matter, and why? â—Ź Evolving Serializable types â—Ź Ways a library can evolve â—Ź Strategies for evolving a library in a backwards compatible fashion â—Ź Best practices
  • 3. About Me Application Architect at Myriad Genetics 14 years experience of working on large-scale Java projects Amateur carpenter Blog: http://www.artima.com/weblogs/?blogger=ianr Twitter: @nainostrebor https://github.com/irobertson
  • 4. Where Compatibility Matters: Serialization â—Ź Distributed computing – Web Services, RMI, JMS, data storage, caching, etc. – Not all clients can upgrade at the same time – Old data can live for awhile (JMS, caches, files) – Backwards compatible serialization changes allows communications between different versions of writer and reader – Not just Java serialization – also XML, JSON, Avro, Protocol Buffers, etc.
  • 5. Evolving Serialized Classes â—Ź Step 0: public class Foo implements Serializable { private static final long serialVersionUID = 1L; … } – If you forget this, you can use serialver later â—Ź Change this only if you wish to intentionally break backwards compatibility
  • 6. Ways a Serialization Form Can Evolve â—Ź Adding a new field â—Ź Removing an existing field â—Ź Changing allowed values for a field â—Ź Changing the type of a field â—Ź Moving fields in inheritance hierarchy â—Ź Change the values for an Enum
  • 7. Adding a Field â—Ź Reader upgrades first – Reader needs to accept absence of new field â—Ź Writer upgrades first – Reader needs to already have been designed to ignore unknown fields â—Ź Java Serialization handles both cases well, as do JaxB, Jackson, and many others – Unknown fields are ignored – Missing fields receive default values
  • 8. Adding a Field â—Ź Semantic Compatibility – Behave well when new field has default value – New field should not change meaning of old fields! Version 1 public class Dimensions { // measurements in meters public int length; public int width; public int height; } Version 2 public class Dimensions { public boolean useMetric; public int length; public int width; public int height; }
  • 9. Removing a Field â—Ź Similar to adding a field – switch the role of reader and writer â—Ź Reader upgrades first – ignore the old field â—Ź Writer upgrades first – reader gets default value â—Ź Again, also pay attention to semantic compatibility! â—Ź Renaming a field is an add and remove
  • 10. Changing Allowed Values for a Field â—Ź In general, this is a semantic issue â—Ź If allowing more values, readers need to either upgrade first, or be able to handle previously unaccepted values â—Ź If the set of allowed values is constrained, writers must upgrade first â—Ź Default serialization bypasses all constructors!!!
  • 11. Changing the Type of a Field â—Ź Java Serialization: – Writer can send subtype of what reader expects (Liskov) – Reader cannot require a subtype of what writer sends â—Ź More generally: – The reader needs to be able to process what the writer sends â—Ź Alternative: add a new field with the new type – Have the old field “forward”
  • 12. Forwarding Field Version 1 public class Invoice implements Serializable { private float amount; public float getAmount() { return amount; } public void setAmount(float amount) { this.amount = amount; } }
  • 13. Forwarding Field Version 2 public class Invoice implements Serializable { private float amount; private BigDecimal totalAmount; public void setAmount(float amount) { this.amount = amount; this.totalAmount = new BigDecimal(amount); } public BigDecimal getTotalAmount() { return totalAmount != null ? totalAmount : new Big Decimal(amount); } }
  • 14. Moving Fields in Class Hierarchy â—Ź Fields in a serialzed form belong to a particular class Version 1 public class Person implements Serializable { } public class Employee extends Person { String name; } Version 2 public class Person implements Serializable { String name; } public class Employee extends Person { } Unrelated Fields!!!
  • 15. Enums Multiple options for encoding: â—Ź Encode the name (Java Serialization does this) – Refactoring the name of a value will cause incompatiblity â—Ź Encode the ordinal (Protocol Buffers does this) – Refactoring the order or names of enum values can cause a hard to diagnose incompatibility! â—Ź Best practice: use independent names for en/decoding – Don’t use these for anything else!
  • 16. Detecting Incompatible Changes â—Ź For each new version, write a file consisting of serialized forms of various instances of your serialized classes â—Ź Write unit tests that verify ability to read these classes â—Ź Only delete tests for versions no longer supported for backwards compatibility â—Ź Alternative – unit test loads older version of library to create serialized form on the fly
  • 17. Custom Serialization and Deserialization â—Ź Useful for otherwise backwards-incompatible changes private void readObject(ObjectInputStream ois) â—Ź ObjectInputStream.readFields() – Easy to use – O(n2) in the number of fields! â—Ź To allow older versions to read newer version, implement private void writeObject(ObjectOutputStream oos) â—Ź Externalizable: Developer has near total control and responsibility
  • 18. Where Compatibility Matters: APIs â—Ź Library APIs – Why can’t my clients just upgrade? CSVParser WeatherStatsReader CommuteRecommender TrafficDataReader – CSVParser releases new, backwards incompatible version – WeatherStatsReader adds new functionality, upgrades its dependence on CSVParser – TrafficDataReader has not updated to use new CSVParser – CommuteRecommender wants to use new version of WeatherStatsReader, but cannot!
  • 19. API Backwards Compatibility â—Ź Source compatible: – Existing source code compiles against new library â—Ź Binary compatible: – Code compiled against the old version will successfully link against the new version â—Ź Neither implies code will continue to successfully run, but it’s a good start
  • 20. Dangers of Source Compatibility â—Ź Good enough for APIs only used by applications, but not for libraries used by other libraries â—Ź Easy to make source compatible, binary incompatible changes and not notice
  • 21. JVM Method Calls â—Ź The JVM has two (traditional) ways of calling instance methods: – InvokeInterface – interface-defined methods – InvokeVirtual – class-defined methods â—Ź Clients call a specific signature of a method, including parameter types and the return type â—Ź No type conversions are performed in looking up methods
  • 22. Example Byte Code ArrayList<String> arrayList = new ArrayList<String>(); List<String> list = arrayList; arrayList.add("x"); // ldc #1 - String x // invokevirtual #2 - ArrayList.add:(LObject;)Z list.size(); // invokeinterface #3 - List.size()I â—Ź Method signatures must match on parameter types, return type, and interface or class
  • 23. Binary Compatibility: Adding Methods â—Ź Adding a method to a class is fine – but consider whether subclasses may already have implemented the same method with different meaning â—Ź Adding a method to an interface which clients do not implement is fine â—Ź Adding a method to a client-implemented interface can lead to runtime NoSuchMethodError, but only if called! – Allows adding methods to the javax.sql interfaces
  • 24. Default methods â—Ź Allows adding new methods to client-implemented interfaces public interface Person { double getHeightInFeet(); /** * @since 1.1 */ default double getHeightInMeters() { return getHeightInFeet() * 0.3048; } }
  • 25. Binary Compatibility: Removing methods â—Ź Removing a method which clients do not call is fine â—Ź Best practice – deprecate a method long before removing it (and javadoc the preferred alternatives!) â—Ź Note: changing a method signature has the same impact as removing it!
  • 26. Binary Compatibility: Changing methods â—Ź When parameter types change, method overloading allows you to keep old signatures around â—Ź The Java language does not allow overloading on return type â—Ź The Java Virtual Machine does allow overloading on return type â—Ź In fact, it does this for you behind the scenes for subclasses
  • 27. Specializing Return Types public class SelfCaused extends Exception { @Override public SelfCaused getCause() { return this; } } public SelfCaused getCause(); 0: aload_0 1: areturn public Throwable getCause(); - marked “synthetic” 0: aload_0 1: invokevirtual #2 - getCause:()LSelfCaused; 4: areturn
  • 28. Bridge Method Injector â—Ź Written by Kohsuke Kawaguchi (creator of Jenkins) â—Ź http://bridge-method-injector.infradna.com/ â—Ź Uses an annotation processor combined with a Maven plugin to add synthetic methods for overloading on return type
  • 29. Bridge Method Injector Example Version 1.0: public Foo getFoo() { … } public Bar getBar() { … } Version 1.1: @WithBridgeMethods(Foo.class) public FooChild getFoo() { … } @WithBridgeMethods(value = Bar.class, checkCast = true) public BarParent getBar() { … }
  • 30. Binary Compatibility: Class Hierarchy â—Ź Adding to the list of implemented interfaces is fine â—Ź Changing the direct superclass can be fine: – Provided no client-referenced super class is removed from the hierarchy, and – Provided no superclass defining a non-overridden client-called method is removed â—Ź Adding method to the child class will help â—Ź Similarly, removing interfaces is fine, subject to client references and calls
  • 31. Binary Compatibility: Class vs Interface â—Ź Recall that JVM has separate byte codes for invoking interface and class methods â—Ź Just as the caller must have the right signature, it must also have the right invocation type â—Ź Changing a class to an interface, or visa-versa, is a binary incompatible change
  • 32. Detecting Incompatible Changes â—Ź Writing compatibility tests – Exercise clients compiled against each older version – Also a useful strategy for web services, JMS, etc. â—Ź Clirr: http://clirr.sourceforge.net/ – Compares two versions of a jar, looking for incompatibilities – Plugins for ant, maven and gradle
  • 33. If Compatibility Must be Broken â—Ź Don’t! â—Ź Really? â—Ź Change your major version number – http://semver.org/ â—Ź Consider changing project and package names – Allows clients to use libraries depending on both versions – JarJarLinks can allow clients to do this, but it puts the burden on them