R
Reactiflux

🎃 Spookyberb 🎃 – 14-44 Oct 13

🎃 Spookyberb 🎃 – 14-44 Oct 13

S⛄Snowberb⛄10/13/2022
How many paremeters would be considered bad code while passing to a function? 5? 10? Whats the unwritten rule?
LBLiil' Boo10/13/2022
Hello, @⛄Snowberb⛄ very good question Depends on your definition from clean code; My understand is that : - 2 is optimal - 3 maximum - if more than 2 or 3 , use a configuration object I've recently become a clean code enthusiast and that's what I learned
S⛄Snowberb⛄10/13/2022
what's a configuration object? @Liil' Boo
const updateFavoriteProp = async (UUID, accountId, isFavorite, isShared, services, entities, accounts, setErrors) => {
try {
const updatedAccount = await updatePartialAccountInfo(UUID, accountId, { isFavorite: !isFavorite });

return {
...updatedAccount,
serviceType: getServiceType(updatedAccount.serviceType.serviceTypeId, services),
accountType: updatedAccount.accountType.description['es-ES'],
entity: getEntityType(updatedAccount.accountType.accountTypeId, entities),
isShared,
};
} catch (e) {
e instanceof Array
? setErrors(e)
: setErrors(['No se ha podido actualizar la cuenta favorita, por favor intentelo mas tarde']);
}
};
const updateFavoriteProp = async (UUID, accountId, isFavorite, isShared, services, entities, accounts, setErrors) => {
try {
const updatedAccount = await updatePartialAccountInfo(UUID, accountId, { isFavorite: !isFavorite });

return {
...updatedAccount,
serviceType: getServiceType(updatedAccount.serviceType.serviceTypeId, services),
accountType: updatedAccount.accountType.description['es-ES'],
entity: getEntityType(updatedAccount.accountType.accountTypeId, entities),
isShared,
};
} catch (e) {
e instanceof Array
? setErrors(e)
: setErrors(['No se ha podido actualizar la cuenta favorita, por favor intentelo mas tarde']);
}
};
This is the function im talking about I took it out of the main component to a service file to atomize and structure it better
S⛄Snowberb⛄10/13/2022
but then I have this in the main componenet
S⛄Snowberb⛄10/13/2022
(screenshot because its more readable)
LBLiil' Boo10/13/2022
Hmm... I'm not strong enough to explain what' a configuration object If you do some research on : IntersectionObserver , or ResizeObserver You'll see both take a configuration object I'mma try to show you by refactoring your function
S⛄Snowberb⛄10/13/2022
thank you! Im also trying to write the cleanest code possible
LBLiil' Boo10/13/2022
type UUID = string;
type Services = any[]
type Entities = any[]
type Accounts = any[]
type ID = number | string
type configurationObject = {
isFavorite : boolean,
isShared : boolean,
services : Services,
entities : Entities,
accounts: Accounts,
setErrors : () => void,
}

function updateFavoriteProp(uuid : UUID, accountId : ID, object: configurationObject){
//...your code
}

updateFavoriteProp('uuid', 2, { isShared: true, isFavorite: true});
type UUID = string;
type Services = any[]
type Entities = any[]
type Accounts = any[]
type ID = number | string
type configurationObject = {
isFavorite : boolean,
isShared : boolean,
services : Services,
entities : Entities,
accounts: Accounts,
setErrors : () => void,
}

function updateFavoriteProp(uuid : UUID, accountId : ID, object: configurationObject){
//...your code
}

updateFavoriteProp('uuid', 2, { isShared: true, isFavorite: true});
A configuration object allows you to only pass in what you need and not bother about the order of arguments/parameters Looks like you already have one for the function updateAccount
S⛄Snowberb⛄10/13/2022
oh okay yeah I get it now
LBLiil' Boo10/13/2022
Your original function might be missing some 'null checks' also
S⛄Snowberb⛄10/13/2022
the whole project is missing null checks its js not ts xd
LBLiil' Boo10/13/2022
TS > JS
S⛄Snowberb⛄10/13/2022
literally try telling that to my coworkers
LBLiil' Boo10/13/2022
hard to do clean code without typescript
S⛄Snowberb⛄10/13/2022
pretty damn hard yes well thank you so much for the info
LBLiil' Boo10/13/2022
Javascript is like, dirty by default, and fight to make it cleaner
S⛄Snowberb⛄10/13/2022
appreciated it
LBLiil' Boo10/13/2022
Gotta give back to the community
UUUnknown User10/14/2022
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

🎃 Spookyberb 🎃 – 14-44 Oct 13

Join Server