mercredi 14 septembre 2022

How should I handle function dependencies? [closed]

Let's say I have the following Golang code:

type MyStruct struct {
    data string
}

func (s *MyStruct) DoSomething() string {
    // Doing stuff...
    s.data = "" // s.data init is a side effect of DoSomething, and not its main purpose
    return "something"
}

func (s *MyStruct) DoSomethingElse() string {
    // Doing stuff...
    somethingElse := s.data + "something else"
    return somethingElse
}

As you can see, we must call DoSomething before calling DoSomethingElse, or else data will not be initialized. What's a good practice of dealing with these kind of situations (Engineering-wise, and not specifically in Golang)?

Aucun commentaire:

Enregistrer un commentaire