To round out my comment above:
The guiding principle involed - The Law of Demeter:
The principle in question here is the Law of Demeter, which very loosely states (taken from Wikipedia):
[Objects should] only talk to [their] immediate friends.
In this sense, your code seems to be violating a fairly important guiding principle of object orientation. So one possible solution here would be to only talk to ScreenManager
, and ask it to give you the ClientBounds
. Unfortunately, you may not be in control of the code (i.e. ScreenManager
, etc), so this may be impossible. Even if it is possible, you can see that ScreenManager
cannot simply call Game.Window.ClientBounds
without breaking the Law again, so it needs to ask Game
to get the ClientBounds
. Depending on who you ask, this gets unmaintainable real quick. Wikipedia's article goes on to give the following warning:
Although the LoD increases the adaptiveness of a software system, it may also result in having to write many wrapper methods to propagate calls to components; in some cases, this can add noticeable time and space overhead.
A different take on the principle - as DTOs:
Robert Martin's generally excellent book Clean Code calls the above chaining of calls a "train wreck", both for its looks, as well as the ability for something to go wrong. However, a distinction is made (hopefully Uncle Bob doesn't mind my quoting here, with some modifications):
Are these two snippets of code violations
of the Law of Demeter? Certainly
the containing module knows [snip]... That’s a lot of knowledge
for one function to know. The calling function knows how to navigate through
a lot of different objects. Whether this is a violation of Demeter depends on whether or not [list of objects, in your case: Game and Window] are objects or data structures. If they are objects, then their internal structure should be hidden rather than exposed, and so knowledge of their innards is a clear violation of the Law of Demeter. On the other hand, if [they] are just data structures with no behavior, then they naturally expose their internal structure, and so
Demeter does not apply.
So, in summary, the Law of Demeter might not be violated, if we see the intermediate objects as primarily DTOs (objects with no behavior).
A possible suggestion: IoC
If you are getting to the point where this type of thing becoming a concern, you can always consider Inversion of Control. In short, we're talking about taking out the logic of how to obtain the ClientBounds
object out of the TitleScreen
. In general then, TitleScreen
would expect to be provided with a ClientBounds
around the time of its construction (although there are a variety of other approaches, such as property or method parameter injection); shifting the responsibility of knowing about the full object hierarchy somewhere else (typically some wiring code).
While this doesn't necessarily solve the violation of the LoD, it does give you an opportunity to remove some duplication from your project. You also make TitleScreen
less dependent on the entire object chain, and potentially more unit testable.
It's completely possible to do this by hand, but to make IoC more realistic and bearable, people normally use dependency injection frameworks, such as Unity or Ninject.