Press "Enter" to skip to content

Code duplication vs generalisation – Q&A P3 – Fun Fun Function


code metrics I don’t know no no they’re

like what is it an interesting

discussion of of why no maybe is it just

does do people think that oh yeah it’s a

who you care about any code metrics for

a project is the question

I like lines of code number of files

number of offers we can talk a little

bit about it see if it’s a picture it’s

interesting because are you for me like

the question is no and I don’t know any

eye developer that does that cares but

yeah I mean so I think when you are

beginning developers sometimes you

fetishize the number of lines of code

something is right yeah and you could

either say I am so productive today I

wrote two thousand lines of code it was

great

it just sprung into existence I’m such a

badass or you can say ah I you know I

expressed this super sort of you know

complex thing in only two lines of

reactive area or some shit like that

and no one can read it but do you think

you’re so clever but this something

that’s maybe not but I wanted to explore

a little bit why write it because why do

you feel I think it is because it is so

tangible it’s very tangible quality

right it’s it’s a convicted wrong

quality it doesn’t matter but it is a

number that can go up or down yes it

makes a great alarm code today or I’ve

removed a lot of code today or I’ve

written this in sixty seven characters

and it used to be four hundred right it

makes you feel good oh yeah absolutely

I love just sitting down you know in the

morning and by lunch time I have you

know fifteen new classes and it’s all

dances together and it worked on the

first try yeah feels great but I don’t

know like I I think maybe in the way the

question is is based it’s like oh these

are metrics yeah so

I think it my I took some courses in you

know computer science and I don’t

remember some where they were like oh

hey you know for these serious projects

we’re tracking all the metrics to

somehow say something about code quality

or productivity or something like that

yeah but you know they also pointed out

the number of black lines of code is a

terrible metric yeah I don’t know what a

better one is but it’s like I mean lines

of code just like when you get like down

a little bit an abstraction level it’s

like it can be a smell if you have a

class is 7,000 15,000 lines long that

smells right that’s like oh this is not

as like for instance like it’s saying

that you there’s also in in programming

like when you’re working on a product

that has been for a long time like you

like it’s very common that you change

code oh yeah and what you must you do

and then like if you count like remove

when I change code is basically change

code is business to remove lines and out

of lines and if you like if you change

something and you do a big refactoring

and you’d apply this a lot of code

change that it’s also like a code like a

code smell then if you have to make it

might be that you in order to make

change you have to like change all over

the place like that means that you have

a non orthogonal system if you listen to

the guy is a gang of four that talked

about the normal terminal system or is

it clean code I haven’t read a book in

years an orthogonal system is where yeah

adding it changing and writing means

that you can change this area but it

right and there’s a good principle it

kind of boils down to if you have one oh

yeah oh yeah this is what it is I think

it’s like a class or a method you’d have

exactly one reason to change yeah it’s

one of these one reason to change all

right and I don’t think that’s always

true no but it is a good sort of you

know a good statement of intense

a sort of go-to heuristic yeah I am I

mean yeah because I mean it’s like if

you take something as simple as

duplication of code or a concept oh I

could talk about that a lot yeah yeah

yeah but if you don’t have to because

it’s so I universally agree that that’s

bad right are you duplicate anything

reasonably what are you doing yeah

anyway no but I mean just like for the

basis of this discussion yeah if you

duplicate things and then you apply this

principle oh I think should have exactly

one reason to change if it’s duplicated

now you have to change it in more than

one place yes if you want to change

whatever the overall thing is it’s like

I love the genre but Google does like

capturing a concept yes like I’ve

captured this concept here and this

concept now like I can speak like in a

class I mean I know you JavaScript guys

don’t use classes anymore a lot of

people use classes I don’t know if that

segue but no but it’s like oh you’ve

captured it in like either a method or a

class or something and it’s like oh now

I can ask the disk guy intelligent

questions about what it is and what it

should be doing right and it’s like you

can extend the concept because it’s in

one place and that’s really good I want

to make like a news come because I think

that removing duplication can also lead

to an honor for all system interesting

let’s say that like I have a very good

way of decoupling things is copy and

paste like and let’s say that yeah let’s

say that you have two pieces of code

right and you like they do different

things and they say that it’s a ordering

system and the payment system right

right and you let’s say you may need to

make a change in the payment system and

that there are some code duplication

between those two right there’s not

enough about value added tax or

something to your orders yeah exactly

relevant to both fair and

you just change that in the but if if

the change is just related to the

payment system then you can just change

that there right because the the code

that is duplicated wasn’t really they

were they’re very big couple right but

they just happen to be the same at a

point in time

exactly changing exactly that is the

happy case for duplication because the

first one then it’s important to

recognize the bad case is an if the

change that you want to make is

applicable to both of those systems yeah

then it is very good to have that right

in in a in a non duplicated place like a

function or or another class or a back

oh there’s a you know calculated total

price of the order needs to be done both

to pay the order and to display what it

should be and the case that I want to

talk about is the case where you pull

this thing up in a shirt function right

but you’re kind of pulling a little bit

too much yes you over generalize you

misrepresent what is actually common

yes exactly you miss the the actual that

you don’t a good way of seeing the

general case is to allow duplication to

arise yes like in order for you to like

wait the general problem to evolve

wrecks but sometimes you don’t do that

you just feel like I don’t know I see

the general right you can pull this up

and and I can foresee in the future

because I’ve done this together yeah but

you like that that is it’s extremely

common than that fails yeah so you end

up with this thing that you know sweet

like when you find yourself in the

situation where you want to change the

payment system but due to the fact that

so much stuff lives in the the general

thing because you don’t pull too much

things into the general thing right you

now have to oh shit now I have to change

the the general thing instead and yeah I

have

change the API of that thing right now I

have to change both the payment system

the general thing and the ordering

system and maybe even ten others it has

a knock-on effect because you have said

that this thing is you know generally

applicable yep and then it starts not

being generally applicable orders like

oh there’s like one difference between

them yeah and you have failed to

elegantly capture what other premises in

some way yeah so you think that like

everybody agrees that duplication is is

generally a dangerous thing but I think

that we also have to recognize that

there is a certain risk with

generalization like people think that

removing duplication is free and risk

free no like yeah and that is that

that’s a big one for me yeah I agree

I mean you often end up with very if

it’s not a slam dunk sort of concept you

end up with these I mean I think the the

the the sibling of this of this problem

or concept is naming right yes oh yes if

you don’t name this thing that you’ve

broken out and it’s very very earlier

where if it doesn’t say exactly what it

is it’s like oh yeah process order

process order payment data like some

shit like that what is that yes that

dude exactly gonna do stuff sum up total

for German dat like whatever like yeah

but that’s at least we’re vague name

oh yeah worst one is where it’s like

gift order but the fucker does some

mutation under because owner I need to

add this mutation and oh it’s in this

function and you add it and now the

commune’s is sort of placed order but

not really yeah query command

separations is really good you should

use that so what is that Oh query

command separation it just means that

you can have queries yeah like you ask

things like get order yep it returns the

order and you have commands which is

changes it oh we perform a mutation on

the system and a good principle is that

you should separate queries and commands

so

I mean yeah that’s very that way you may

have a command that’s something like I

don’t know

please order for example this young man

but in that method you might have

queries like Oh gets the cost of the

order or you know stuff like that

yep but yeah the overarching kind of

reasoning for this is that if the case

you describe where you have a query ya

get order and it does something else

yeah

it changes the mutated states yep

that is an onion and unintended

consequence a side effect

yeah right and that’s how bugs arise

because you father just like returned

something but it also did something that

you didn’t anticipate and now you have a

back but we like to be playing devil’s

advocate here like nobody is arguing for

those things like no that’s an argument

would say like oh you but you should

just not have made that mistake in the

first oh yeah yeah it’s true but the

problem is that like I just like the

architecture zealot of Christmas past

came down and I just had to shout about

separating queries yeah like I

the thing with naming Frederick’s is

that naming is very hard yes so once you

start okay I you think that ah this is

I’m just gonna break this out into a

variable or amiright a break this out

into a function just yeah like make

things and then you have to name it then

you have yeah give it a cohesive name

and that is surprisingly hard and

time-consuming to get right yeah and

sometimes it’s not even possible because

it that you might not know what it is

you know it’s a place in the system but

you haven’t written enough of the rest

of the code to know how this thing will

be yes later on

exactly yeah I think my favorite

principle for naming things said she is

that you must always tell the truth

everything must always tell the truth

like a name should not say something

that it is not or you know it has to

work both when you

is it on like a global level yeah ask it

a question and it has to work locally

yeah if you like without the surrounding

context like without the assumptions

basically whatever you name something or

do anything you have a lot of

assumptions about what does this yeah

like if you say order there’s like a

bunch of assumptions about what is that

yeah well what are we doing this is an

ordering system and there’s I don’t know

yes

so anyway but I want it like the thing

with that I think that most people can

you should not have your functions lie I

think that most people can oh yeah I say

oh I like lying but I would like in

order for that to work I would add a

second commandment like no we giving

Commandments as overwrites right if I am

methods can also be very vague yes I

must say exactly what it is because

phrases like yeah I’ll have this

terminology that I’d like to refer to I

learned it from Wikipedia Oh recent

works Oh talked about this before

haha and it’s just terms that kindness

order censored like it’s like appeal to

this undefined Authority it sounds good

and yeah yeah it sounds like it means

something but really doesn’t and

developer by weasel words are manager

utils like you just kind of obvious that

it is it’s kind of honest like yes this

is my pile of shine but a manager like

it’s it deals with other objects or some

like it’s very I think if you’re gonna

use manager you have to on a project

level agree what a manager is yeah in

game programming you do use the word

manager yeah sometimes yeah if you

actually have a definition in other

words basically means it’s a singleton

and it deals with stuff you know that is

valid you know I would in a context as

the game progresses it doesn’t get any

better this ought to go away you have

like you have an actual definitional

manager sure I mean

in my project yes like the global okay

but basically like an example let’s see

I remember I remember I use her data

manager boom would be like Oh what does

that do it probably keeps track of the

users data and saves it in those that

enemies spawning manager except

ambiguous what you think it does enemies

spawning manager enemies spawning

manager like it spawns and it’s like

okay it’s I guess all right it’s I

imagine it being a thing that sits in

place and is responsible for spawning

the enemies it feels like it’s something

that is is there but it’s not like could

you just say enemy enemy smaller or yeah

I could but now we’re getting now we

could unless manager has a specific

meaning yeah so I mean your project so

if I came across my own project or you

know a product I didn’t know and I saw

enemies spawner versus enemies spawning

manager yeah I would think that enemy

spawning manager has the you know the

the logic for how do you spawn an enemy

you know it would be like it would know

what what needs to happen with the enemy

for it to to be in a good state and so

on yeah or delegate that or be

responsible for that enemies spawner

might be you might have more than one

enemy spawner you could have like an

enemy that’s like makes other enemies

and that could have an enemy spawner but

yet to me the meaning when you say

manager I think singleton yeah but

that’s like very specific to gaming us

no always true no but a single bonus is

on the other hand a term that but I very

specifically mean something right

exactly yeah so it’s basically something

that has more of an overall

responsibility or managers like the life

cycle that is independent of any any one

thing it’s a little bit a little bit

sure but it’s like basically the

important thing is that all the words

leading up to the word manager is this

part descriptive yeah

so you could say you look for the

enemies spawning example you could

imagine that the enemies spawner

actually is the guy who knows when an

enemy should be spawned yes but that

might not be true it might be more like

an enemy’s factory or something like

that

sure but yeah anyway I don’t always use

manager for these banks but I I’m just

saying that it can be a good word if you

agree on what it mean if it is really is

reminder with some very specific stuff

you avoid confusion other words and the

name as well but if you’re just an enemy

manager what the fuck does that do I

don’t know Jesus Christ

what was the orange I think we we were

out of time

oh really oh yeah well that was it you

have us watched an episode of fun and

fun and function this was a given

episode it’s not usually like this but

sometimes it is it was fun yeah fun

function yeah well I put talk about

programming concepts every week you

should subscribe if you haven’t yes ah I

like the video as well oh do you know

give it the thumbs up do all you to do

all these social media thing and I

really sees every Monday morning Oh 800

GMT time oh ah it’s great are super hard

ah because when when they do these like

the normal episodes I have like this

list of things that the Savior and I can

say the intro like a hundred times until

I get it right but when we do these

episodes it’s kind of like this week

Chloe thing it is very jarring to go

from from the free flow walking mode to

the very exact alpha mode yeah like Nina

I guess you have a spiel but if you just

give your speed – it’s here it’s super

weird like if I just sit here and look

into the camera and say you know yeah

it’s super with either way until next

Monday morning stay curious yes but I did

Please follow and like us: