So i'm having a hard time with handling a large amount of class properties. Im making a "component" and want to make this "component" as customizable as possible. In doing so, I end up having to deal with this:
constructor({
embeds = [],
message = null,
client = null,
isSelectable = false,
skip = true,
timeout = 10000,
isCallback = true,
max = 10,
results = [],
hasPageNumber = true,
start = 0,
identifiers = ['1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟','1️⃣1️⃣','1️⃣2️⃣','1️⃣3️⃣','1️⃣4️⃣','1️⃣5️⃣','1️⃣6️⃣','1️⃣7️⃣','1️⃣8️⃣','1️⃣9️⃣','2️⃣0️⃣'],
undefinedResultValue = 'Unknown Result',
useIdentifiers = true,
innerPageOptions = {
message: message,
client: client,
hasPageNumber: hasPageNumber,
skip: skip,
timeout: timeout,
start: start,
isSelectable: isSelectable,
embeds: null
}
})
The code above works fine but is horrible to looks at and I feel people have encountered similar issues to this. I want to limit the number of properties whilst also making the "component" as customizable as possible. Im also trying to heavily focus on making my code as clean as possible and dont really have anyone to help out with these types of issues and is were experience is needed to solve.
Each property is used, some more than others and some with more affect. I thought about collapsing some values into another sub-object but this just is an alternate outcome that is very similar to what I already have.
Class structure for more detail on what I have, class methods have been excluded due to being obselete for this problem:
export class PageEmbed{
constructor(options: Options | SearchOptions)
private options: Options | SearchOptions
private content: MessageEmbed[]
private display: SearchDisplayProperties
private index: number
}
export class Page extends PageEmbed{
constructor(options: PageOptions)
private message : Message
}
export class SearchPage extends PageEmbed{
constructor(_options: SearchOptions)
private inner: Options
public page: Page
}
You could look for some common or default parameters combinations and add them somewhere in your code. Also you could implement the factories to simplify the instances creation. The general idea is:
const DEFAULT_PROPS = {
embeds = [],
message = null,
client = null,
isSelectable = false,
skip = true,
timeout = 10000,
isCallback = true,
max = 10,
results = [],
hasPageNumber = true,
start = 0,
}
class MyFactory {
props = DEFAULT_PROPS
mutate (newProps) {
this.props = {
...this.props,
...newProps
}
return this
}
create() {
return new MyClassWithProps (this.props)
}
}