From fef94902c0d2b3a67b5c5eb1950b4f185e519f68 Mon Sep 17 00:00:00 2001 From: bronifty Date: Wed, 4 Sep 2024 11:46:44 -0400 Subject: [PATCH] this --- docs/opinion/codes/_meta.json | 2 +- docs/opinion/codes/oop-v-fp.mdx | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 docs/opinion/codes/oop-v-fp.mdx diff --git a/docs/opinion/codes/_meta.json b/docs/opinion/codes/_meta.json index f917ad2..eb72299 100644 --- a/docs/opinion/codes/_meta.json +++ b/docs/opinion/codes/_meta.json @@ -1 +1 @@ -["abstraction", "fundamentals-ch3"] +["abstraction", "fundamentals-ch3", "oop-v-fp"] diff --git a/docs/opinion/codes/oop-v-fp.mdx b/docs/opinion/codes/oop-v-fp.mdx new file mode 100644 index 0000000..82cd842 --- /dev/null +++ b/docs/opinion/codes/oop-v-fp.mdx @@ -0,0 +1,19 @@ +# OOP v FP + +## Chat with AI about reusing and extending Web APIs + +Me: + +> would it be accurate to state that fp is good for logic that is flat and oop is more hierarchical? for instance, working with the Web API, such as extending the File API, it makes more sense to subclass that and get its methods for free than rewrite it using an interface as would be required with fp, correct? + +ChatGPT: + +> Yes, your statement captures a key difference between functional programming (FP) and object-oriented programming (OOP). + +OOP is well-suited for hierarchical and complex systems where you want to model entities with attributes and behaviors. In OOP, subclassing and inheritance allow you to extend existing functionality by creating specialized versions of objects. For example, extending the File API by subclassing allows you to reuse and extend its methods without duplicating code. This hierarchical structure aligns with the way OOP organizes code around objects and their interactions. + +FP, on the other hand, tends to favor immutability, stateless functions, and composability over inheritance and object hierarchies. It's more common in FP to define logic as a set of pure functions that operate on data rather than encapsulating both data and behavior in objects. This approach is "flatter" because it doesn't rely on a deep hierarchy of classes and inheritance, but rather on composing small, reusable functions. + +When dealing with something like the Web API, where extending existing functionality (like the File API) is common, OOP's hierarchical nature makes it easier and more natural to subclass and inherit behavior. In contrast, doing the same in FP would require you to redefine behavior through composition, often using interfaces or higher-order functions, which can be less intuitive when dealing with something inherently hierarchical. + +So yes, OOP is typically more hierarchical and is a better fit for scenarios where you want to extend and reuse existing, complex structures. FP is more suited to scenarios where you want to keep logic flat, modular, and composed of small, reusable functions.