what are the pros and cons of generalisation for programming??Watch
Firstly - think about what a program's source code looks like without generalisations; whenever you have some kind of re-occurring 'pattern', the code ends up containing similar things duplicated in different ways and in different places across the code. At the simplest level, that might be a block of code which could have been written using copy+paste followed by only changing a small handful of things in the code. For example
forename1 = input("enter forename 1: ") surname1 = input("enter surname 1: ") fullname1 = forename1 + " " + surname1 print("full name 1 is: ", fullname1) forename2 = input("enter forename 2: ") surname2 = input("enter surname 2: ") fullname2 = forename2 + " " + surname2 print("full name 2 is: ", fullname2) forename3 = input("enter forename 2: ") surname3 = input("enter surname 2: ") fullname3 = forename3 + " " + surname3 print("full name 3 is: ", fullname3)
for i in range(1,4): forename = input("enter forename " + str(i) + ": ") surname = input("enter surname " + str(i) + ": ") fullname = forename + " " + surname print("full name ", i, " is: ", fullname)
So, consider things like:
- How easy is code to read, understand, modify and reason over before/after the generalisation?
- How much does reading/understanding/modifying and reasoning depend upon the experience and ability of the programmer, and their familiarity with the patterns/generalisations?
- How much time and effort does it take to work with the generalised code versus the specialised code?
- What happens if the code has clear "patterns" but also differences and special cases?
- How does complexity enter the mix?
- Good-vs-bad generalisations - e.g. what if an abstraction has a bug?
- What if the generalisation turns out to be 'wrong'? (e.g. "if it looks like a duck, and quacks like a duck, but it takes batteries, then maybe it is not really a duck but something else..")
- What if lots of different bits of code rely upon the generalisation to work properly?
- What if programmers keep changing the way the generalisation works (a.k.a. "instability")
- What effects do 'special cases' have on the code?
Have a look at:
DRY Principle - https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
Code Reuse - https://en.wikipedia.org/wiki/Code_reuse.
KISS Principle - https://en.wikipedia.org/wiki/KISS_principle
YAGNI Principle - https://en.wikipedia.org/wiki/You_ar..._gonna_need_it
Overengineering - https://en.wikipedia.org/wiki/Overengineering
Leaky Abstraction - https://en.wikipedia.org/wiki/Leaky_abstraction
Breaking Change (in a generalisation) - https://en.wiktionary.org/wiki/breaking_change
Maintainability - https://en.wikipedia.org/wiki/Maintainability