what are the pros and cons of generalisation for programming??

Badges: 16
Report Thread starter 1 year ago
I cant seem to find any.
Badges: 19
Report 1 year ago
The more usual term for a generalisation in programming is abstraction, so try searching for that instead - but more importantly make sure that you understand what 'generalisation' actually means in real terms when writing code.

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)
So it's pretty obvious from looking at that code where the pattern is and how you could generalise it with an abstraction.

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
Last edited by winterscoming; 1 year ago

Quick Reply

Attached files
Write a reply...
new posts
to top
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.


Should there be a new university admissions system that ditches predicted grades?

No, I think predicted grades should still be used to make offers (706)
Yes, I like the idea of applying to uni after I received my grades (PQA) (889)
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (393)
I think there is a better option than the ones suggested (let us know in the thread!) (93)

Watched Threads

View All