How not to use OOP
5th December 2003
Via Hans Nowak, Understanding Object Oriented Programming, or how to turn 19 lines of easily maintained code in to an OO monstrosity spanning 7 class files. This is not the way to make code more maintainable. For comparison, here’s how I would implement a solution to the same problem in Python, assuming the availability of an equivalent function to Java’s System.getProperty("os.name")
(os.name
is similar but inappropriate for this example):
MESSAGES = (
('SunOS', 'Linux'), 'This is a UNIX box and therefore good.'),
('Windows NT', Windows 95'), 'This is a Windows box and therefore bad.),
)
DEFAULT_MESSAGE = 'This is not a box.'
def get_os():
name = System.getProperty("os.name")
for names, message in MESSAGES:
if name in names:
return message
return DEFAULT_MESSAGE
if __name__ == "__main__":
print get_os()
In my book, smart data-driven programming beats over-engineered class-based programming any day of the week.
More recent articles
- Notes from Bing Chat—Our First Encounter With Manipulative AI - 19th November 2024
- Project: Civic Band - scraping and searching PDF meeting minutes from hundreds of municipalities - 16th November 2024
- Qwen2.5-Coder-32B is an LLM that can code well that runs on my Mac - 12th November 2024