I have a class that sets up an array of nodes and connects them to each other in a graph-like structure. Is it best to:
- Keep the functionality to initialize and connect the nodes in one function
- Have the initialization and connection functionality in two different functions (and have a dependent order on which the functions must be called -- though keep in mind these functions are private.)
Method 1: (Bad in that one function is doing two things, BUT it keeps dependent functionality grouped together -- the nodes should never be connected without being initialized first.)
init() {
setupNodes()
}
private func setupNodes() {
// 1. Create array of nodes
// 2. Go through array, connecting each node to its neighbors
// according to some predefined constants
}
Method 2: (Better in the sense that it is self-documenting, BUT connectNodes() should never be called before setupNodes(), so anyone working with the class internals needs to know about this order.)
init() {
setupNodes()
}
private func setupNodes() {
createNodes()
connectNodes()
}
private func createNodes() {
// 1. Create array of nodes
}
private func connectNodes() {
// 2. Go through array, connecting each node to its neighbors
// according to some predefined constants
}
Excited to hear any thoughts.