πŸŽƒ Spookyberb πŸŽƒ – 14-44 Oct 13

How many paremeters would be considered bad code while passing to a function? 5? 10? Whats the unwritten rule?
LB
Liil' Booβ€’553d ago
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
Sniberbβ€’553d ago
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
Sniberbβ€’553d ago
but then I have this in the main componenet
S
Sniberbβ€’553d ago
(screenshot because its more readable)
LB
Liil' Booβ€’553d ago
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
Sniberbβ€’553d ago
thank you! Im also trying to write the cleanest code possible
LB
Liil' Booβ€’553d ago
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
Sniberbβ€’553d ago
oh okay yeah I get it now
LB
Liil' Booβ€’553d ago
Your original function might be missing some 'null checks' also
S
Sniberbβ€’553d ago
the whole project is missing null checks its js not ts xd
LB
Liil' Booβ€’553d ago
TS > JS
S
Sniberbβ€’553d ago
literally try telling that to my coworkers
LB
Liil' Booβ€’553d ago
hard to do clean code without typescript
S
Sniberbβ€’553d ago
pretty damn hard yes well thank you so much for the info
LB
Liil' Booβ€’553d ago
Javascript is like, dirty by default, and fight to make it cleaner
S
Sniberbβ€’553d ago
appreciated it
LB
Liil' Booβ€’553d ago
Gotta give back to the community
UU
Unknown Userβ€’553d ago