Sniberb
Sniberb•3y ago

🎃 Spookyberb 🎃 – 14-44 Oct 13

How many paremeters would be considered bad code while passing to a function? 5? 10? Whats the unwritten rule?
18 Replies
liil'boo
liil'boo•3y 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
Sniberb
Sniberb•3y 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
Sniberb
Sniberb•3y ago
but then I have this in the main componenet
Sniberb
Sniberb•3y ago
(screenshot because its more readable)
liil'boo
liil'boo•3y 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
Sniberb
Sniberb•3y ago
thank you! Im also trying to write the cleanest code possible
liil'boo
liil'boo•3y 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
Sniberb
Sniberb•3y ago
oh okay yeah I get it now
liil'boo
liil'boo•3y ago
Your original function might be missing some 'null checks' also
Sniberb
Sniberb•3y ago
the whole project is missing null checks its js not ts xd
liil'boo
liil'boo•3y ago
TS > JS
Sniberb
Sniberb•3y ago
literally try telling that to my coworkers
liil'boo
liil'boo•3y ago
hard to do clean code without typescript
Sniberb
Sniberb•3y ago
pretty damn hard yes well thank you so much for the info
liil'boo
liil'boo•3y ago
Javascript is like, dirty by default, and fight to make it cleaner
Sniberb
Sniberb•3y ago
appreciated it
liil'boo
liil'boo•3y ago
Gotta give back to the community
reactibot
reactibot•3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1030128979648204911