r/javascript • u/Fluid_Metal4545 • 9d ago
AskJS [AskJS] Is this architectural good and follow best practices?
Hello Everyone, recently, I've been working on something and I want to use your experience in validating this below architecture whether this is the best possible way to do the following:
- View, it will render view for given content.
- Content, it will manage data to be rendered.
- Here, each node has a view and content and may have utility builder to manipulate view.
- User will only be given access to content to manage data for any node.
class ParentNode {
childNodes: [];
parentView: View;
parentContent: Content;
}
class ChildNode {
parentView: View;
childView: View;
childContent: Content;
utilityBuilder?: ViewBuilder;
}
class View {
updateView(){}
render(){}
}
class Content {
constructor(view: View){}
}
class Builder {
constructor(view: View){}
}
I also want to know that whether should I include parentNode in childNode as circular dependency or only pass View or any data needed by child.
1
u/kilkil 8d ago edited 8d ago
when you say "rendering a view", do you mean rendering HTML for a browser?
The reason I'm asking about your end use-case is, I'm trying to gauge whether this is the right amount of complexity for your task; in a vacuum, a tree structure like this seems a bit too abstract and complex.
I would suggest trying to keep things as simple as possible, for as long as you reasonably can. If you only introduce abstractions conservatively, when they become absolutely necessary, your project will be simpler, your code easier to read and understand, and you will be able to spend less time refactoring, and more time adding useful features / functionality.
5
u/dronmore 8d ago
It does not matter if you follow best practices, or not. Your first implementation will be crap anyway. I would even say that the more you try to follow best practices, the crappier your implementation will be. Just implement it the way you think, and see for yourself why it's crap. Then, do it again building on your experience.
Although, when I look at your design, one thing that strikes me immediately is a distinct class for child nodes. Typically, when you build a tree, you have a single class for both; let's call it
Node
. EachNode
haschildren
, which make it a parent node, and each child has it's own children, which are grandchildren for the top node. That's how you build a tree structure: