Self portability
I’ve been thinking about code portability. How can we write code that works across different environments?
Key Principles
- Abstract platform-specific code - Use interfaces to hide platform differences
- Dependency injection - Pass in dependencies rather than creating them directly
- Feature detection - Check for feature availability before using it
Example
# Instead of:
import platform
if platform.system() == 'Windows':
path = 'C:\\temp\\file.txt'
else:
path = '/tmp/file.txt'
# Use:
import os
path = os.path.join(os.temp_dir(), 'file.txt')
Writing portable code from the start saves time later.