Quantcast
Channel: Law of Demeter and OOP confusion - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Law of Demeter and OOP confusion

$
0
0

I've been doing some reading recently and have encountered the Law of Demeter. Now some of what I've read makes perfect sense e.g. the paperboy should never be able to rifle through a customers pocket, grab the wallet and take the money out. The wallet is something the customer should have control of, not the paperboy.

What throws me about the law, maybe I'm just misunderstanding the whole thing, is that stringing properties together with a heirarchy of functionality/information can be so useful. e.g. .NETs HTTPContext class.

Wouldn't code such as :

If DataTable.Columns.Count >= 0 Then   DataTable.Columns(0).Caption = "Something"End If

Or

Dim strUserPlatform as string = HttpContext.Current.Request.Browser.Platform.ToString()

Or

If NewTerm.StartDate >= NewTerm.AcademicYear.StartDate And    NewTerm.EndDate <= NewTerm.AcademicYear.EndDate Then' Valid, subject to further tests.Else' Not valid.End If

be breaking this law? I thought (perhaps mistakenly) the point of OOP was in part to provide access to related classes in a nice heirarchical structure.

I like, for example, the idea of referencing a utility toolkit that can be used by page classes to avoid repetitive tasks, such as sending emails and encapsulating useful string methods:

Dim strUserInput As String = "London, Paris, New York"For Each strSearchTerm In Tools.StringManipulation.GetListOfString(strUserInput, ",")    Dim ThisItem As New SearchTerm    ThisItem.Text = strSearchTerm Next

Any clarity would be great...at the moment I can't reconcile how the law seems to banish stringing properties and methods together...it seems strange to me that so much power should be disregarded? I'm pretty new to OOP as some of you might have guessed, so please go easy :)


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images