welcome back you beautiful people in

this episode I want to show you guys

this technique and this technique I

learned after years and years of doing

programming writing functions and then

you know when I’m trying to add new

arguments the functions break and so

this is hopefully going to help you guys

out to keep your code maintainable and

you can do this on JavaScript or PHP or

any other language that has functions so

it’s more of a technique

that you should consider at some point

in time it might make your life easier

in the long run when you have to you

know edit code that you’ve been building

for years and years or even months upon

months right because you don’t want to

do much more edits than you should and

I’ll get into the technique and I’ll

explain it and hopefully you’ll get to

see how powerful it is

as we go along in the process so let’s

turn over to the code and here what we

have is a function that I created in

JavaScript called sum and sum the

function sum use uses these arguments so

we have set these that these required

arguments that are needed to be

submitted in order to get the sum okay

and you could you know these are random

things I just used it don’t think of it

as any particular logic all that we’re

doing is going we’re just going to

calculate sum so that’s what we’re doing

here we get this we have we create some

variable and then we add up all these

arguments here and then we return some

and when we do that without unknown

because this is the example I want to

show you this should work totally fine

okay so as you can see we’re going to

console.log whatever the sum is of these

values here and that should work so

great we got one five seven one okay so

we have this sum

now of course sometimes what we want to

do is we want to adjust a function we

want to add in an argument right so all

we do is if we have an argument a new

argument we put you know new let’s call

it just new okay and in order to

of course we need to sum the new

argument now so we add new right so

that’s you know it should be it let’s

name it new value right new underscore

value yeah it doesn’t really

uh just so we understand so I’m saving

that I’m going to run back some again

and see what happens

okay so we got none not a number right

um now the reason why we likely got the

not in number none is because there’s a

missing argument here so wherever sum is

being called it requires all of these


unless you set them as null or a default

value of some sort it’s going to return

that non-value if it does not have all

of the arguments that are available here

all right and

um I want to show you this example by on

because obviously this is a very simple

example this simple example we are only

calling the sum function once right we

just cancel login one time but let’s say

you have a full program

and so I have I’m going to add in these

functions operation one operation two

operation tree and these also call this

sum function

all of these are going to fill okay so

let me show you what I mean you’re going

to get the Nan value

if I run this operation 1 is not

operation 2 is not operation three is

none all because we added a new


within the function right

so what can we do about this how can we

optimize this so that we don’t always

have to refer back to

um the arguments here being

you know these these things are going to

change in this case it did change

because we added new value now

everything all of this is broken because

we did we did that one little change

that we did well instead of using

an argument or you know multiple

arguments within here why don’t we use

something like an object for example or

in PHP an associative array that might

help things so what we could do for

example is we could change all of this

to args

to arcs right

and then we could instead of doing or

you know doing this where we manually

add this we could dynamically

change this but let’s let’s start one

thing let’s let’s start one thing which

is let’s change it to X as I said and


let’s see

we have we created we create an object

and we call this one example args right

just to make it easy and this is an


okay you can use uh you can use perhaps

a an array but in this case I’m using

objects for a reason and let’s see here

we do Heights

and in this case we look at the values

here 800 blah blah blah so we create

that 800 we do width

600 we do X

which is I said 10 we do Y which is 20.

we do a which is one

we do p which is 2

we do age

which is 38

and we do velocity

which is a hundred

okay so now we have now we have an

object that we can pass into this sums

but this is going to break all right so

here’s what we could do right

we could make this more Dynamic we could

say sum is zero we set some to be equal

to zero and then we go through

the keys of this args

right because we want to we’ll push this

in but we have to go through the keys of

this Alex this e x args will become args

when we when we use it in a in the

actual function except when we call the

actual function

so we change our code

let’s go for let’s let’s get the keys

first which is object

dot keys

of ax okay

and now we can go through each key

so let’s save our bar I equals zero key

I is less than

Keys dot length

I plus plus

and here now

we get the value okay so we get VAR


is equal to


well let’s get a key right we have to

actually get the actual key so the

actual key will be key is I


and then we get VAR value

of Val which is equal to


the key that will give us a value

and now that we have the value right so

the key all these Keys we got was

actually here right these are the keys

on the left hand side and then these are

the values on the right hand side so

when we what we got was when we said

object.keys we got the keys height with

X Y A B all of that was returned as an

array and this is the keys array and

then what we did was we checked we went

through each one of the keys so we

checked the keys at length we went with

I the index is I then we got what each


name is right so we got a key name here

which is height in this case

um on on I equals zero and then we got

the actual value which is Rags key right

so args or whatever the key name is

would give us the value and that’s what

we have and now we do the sum which is

we take sum plus equal Val

I know what we should have is it will

return sum after it does all of that and

that’s fine

but now we have to change of course how

this function works so we already have

this example args and all we have to do

now is we put in ex arcs here ex arcs


okay now I’m going to close I’m just

going to stop uh running this for a

while so I’m going to uncomment it out

what did I do

I mean comment to talk that’s it right

and now this should work it should


the value again one five seven one okay

now all of these operations we have to

adjust these operations one time and

let’s say we do the same thing so

instead we do

e x ax we you’ll get the point in a

minute it’s just taking a little long e

x arcs okay

and we do this

for all of them

we got operation one two three two

succeeded which we expect okay so here’s

the thing now we want to add that new

value right now let’s add that new value

so let’s say in ex arcs we want to add

the new value we just put the new value

then this is a thousand just so we see

the difference or let’s put that let’s

put ten thousand right so we added in

the new value

and now as you can see we don’t actually

have to edit any of our operations

anymore because the logic takes care of

it the logic is going to call

um you know call the algorithm the sum

algorithm is going to go through Each

One automatically figured out I’ll sum

it up and

now if we run this

I made an error somewhere here

okay yeah

now if we run this

it works

right and we could do the same thing so

let’s say we had a b let’s go for C

10 d 20. e

30 okay

again now all of these functions that

we’re using it you don’t have to do

anything right isn’t that great that you

don’t have to edit now the functions

uh well the function arguments anymore

in multiple spots remember let’s say

this is ten thousand lines of code you

wouldn’t go through changing that all

the time so it’s best to really

in in some circumstances I know that’s

it’s not always reasonable for all but

in some circumstances like this

um to use something like an object or

inside an array instead of using

multiple arguments in here and that

means that the code becomes more

maintainable Dynamic and then you know

you just have an easier time working

with it now there is another scenario

okay so there’s another scenario which

is this

let’s see that we wanted to add like a

label let’s say we wanted to do some

different sort of


I don’t know output that that requires

you to add additional arguments but you

don’t want these arguments to be summed

parts right this will obviously not work

so for example let’s say that we wanted

to name this label for some reason let’s

say we wanted to add some text to this

object right this would not work

because what we should get is likely


or we get tests okay so it actually


it added test the string to the end of

it which is not exactly what we want

right because we wanted it to return an


value in this case might be an integer

or something so you change it to a

string when you added this text in

or you again you would have to do some

altering but you can do that ultimate

all without editing

a lot of your functions you just have to

focus on your main function

and here’s what we do here’s what we

could do so this is something you could

do all right so we could

change how this works

and instead add a new object and call

these object numbers

and you might be wondering well what is

that going to do I’ll show you

and now you should be able to add label

test but this will break some

but that’s a good thing it only breaks

the main function it doesn’t break all

the other functions so now instead of

looking for

object dot object.keys on args right

remember ARG is going to push in

whatever is in here e x args and all of

these examples right what you would now

want to do is you would want this to be

numbers now you could do it that way or

you could just type you could just do

period numbers as long as it exists okay

and then you would get the keys

and then you get the keys here and then

you have to make sure that you use

numbers here

and this

should work

and also

it would then allow you to do something

like this

we output the label right so we wanted

to Output the label we could output

label so let’s say we put test test

instead was the sum of all values ah

we could use aux label here we could

comma and then we could use sum and what

that would do


the sum of all values are the number the

sum of values are number c and that way

we didn’t break all the other functions

that we would have created before right

um isn’t that kind of cool right so as

long as it’s it it now as I said it

doesn’t always make sense to do this but

many times it does

right and one of the challenges that

comes in with using something like this

where you use an entire object is that

sometimes you may forget what the

argument should be

so what I learned to do was I usually

have some comments above

um where I say what args is so args

should contain

or sometimes I just I just copy and

paste you know I was like Ada explain it

like that should contain blah blah blah

or you just say args is an object right

and then

you could likely copy and paste what an

example object looks like

that’s not too hard


just copy and paste what that object

looks like in here

and now you have a way of remembering

what the object actually has in it right

and that’s how you could kind of solve

this problem and what that will also do

is it will also you’ll also add

documentation to each of your function

anyways which is nice as well

um so I hope this helps you guys out I

know that most times you may not need

this if you’re running small programs

but if you’re going to run thousands

upon thousands of lines it’s really

important to decide whether you want to

do it now as opposed to having it to do

it later on where it requires a lot more

time to to be spent you know trying to

reconvert code so it’s better to just

make the code AS dynamic as possible

with that being said if you did like

this video please like

Please Subscribe it goes a really really

long way and hopefully this will help

someone else out as well it’s gonna also

help of course obviously the channel

um I know most people don’t really care

too much about the channel but it does

really help me out as well in the

process of reaching more people like you

that love coding and of course it’s

going to help someone from and see if

maybe save them yes out of their life

um when instead of breaking code that

they could have just easily not broken

they could have they thought about it in

terms of using these objects but that

being said have a wonderful day you guys

and see you guys again soon thanks bye