Flaw: Constructor does Real Work

前端 未结 6 1614
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 04:15

I have a class which represents a set of numbers. The constructor takes three arguments: startValue, endValue and stepSize. The class

6条回答
  •  自闭症患者
    2020-12-17 04:40

    If you want to use Microsoft's .NET Framework was an example here, it is perfectly valid both semantically and in terms of common practice, for a constructor to do some real work.

    An example of where Microsoft does this is in their implementation of System.IO.FileStream. This class performs string processing on path names, opens new file handles, opens threads, binds all sorts of things, and invokes many system functions. The constructor is actually, in effect, about 1,200 lines of code.

    I believe your example, where you are creating a list, is absolutely fine and valid. I would just make sure that you fail as often as possible. Say if you the minimum size higher than the maximum size, you could get stuck in an infinite loop with a poorly written loop condition, thus exhausting all available memory.

    The takeaway is "it depends" and you should use your best judgement. If all you wanted was a second opinion, then I say you're fine.

提交回复
热议问题