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

Agent456
Badges: 16
Rep:
?
#1
Report Thread starter 8 months ago
#1
I cant seem to find any.
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 8 months ago
#2
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
Code:
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.

Code:
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; 8 months ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
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.

Personalise

Have you made up your mind on your five uni choices?

Yes I know where I'm applying (121)
65.05%
No I haven't decided yet (39)
20.97%
Yes but I might change my mind (26)
13.98%

Watched Threads

View All