A safe zone is a region where code is trusted to preserve data-safety and immutability guarantees. Copy-on-write defines the zone; defensive copying protects its borders. section 7.8
Keep the boundary clear with two complementary disciplines:
- Inside the safe zone, use copy-on-write: make a shallow copy, modify the copy, and return it. section 7.8
- When data enters or leaves the safe zone, use defensive copying: make a deep copy at each border. section 7.8
- Treat copy-on-write as relatively cheap and defensive copying as relatively expensive, reflecting shallow versus deep copies. section 7.8
- Use defensive copying when exchanging data with untrusted code, including existing code and libraries. section 7.11
Choosing the discipline
Modifying data you control calls for copy-on-write; exchanging data with untrusted code calls for defensive copying. Neither discipline is a complete immutability solution alone. section 7.8 section 7.11
