2

Should code that positions a form be in a seperate class or within the form itself. It is assumed, that there will be only one form using it.

A class definitely encapsulates the behavior and the state, but maybe it is unnecessary indirection in this case?

2
  • The code that is performing actions on a WinForm should be separated.
    – Dorin
    Commented Apr 6, 2016 at 12:48
  • 2
    This isn't about SRP. The problem is that you don't necessarily want a form to be aware of its outside environment. Commented Apr 6, 2016 at 14:19

1 Answer 1

2

This would depend on the size and complexity of the form/application.

We can keep it simple until the code uses fixed values and stretches to minimum lines.

Listing few points below, if answer to any of them is 'yes' then it would be better to separate the concern.

  • You have started to repeat the code.
  • The logic of finding the position is complex e.g. calculate using certain algorithm.
  • It needs to talk to external interfaces, e.g. get size of display, find a value saved in database etc.
  • The form code has grown substantially because it is doing many other things apart from positioning e.g. data binding, event binding, handling display etc.
  • You want to write unit tests for the form and want to mock the form position.
  • You want to unit test the form position.

This would be a minimum check before I decide to keep the code in same class. But I also remember that it might not be valid in all the cases. When in doubt I would go for the simplest solution.

HTH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.