Click here to Skip to main content
15,888,461 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Dear Apple, Why do you hate grandma? Pin
jochance10-Apr-24 5:16
jochance10-Apr-24 5:16 
GeneralLocality of Behavior: term you've been looking for Pin
raddevus9-Apr-24 4:06
mvaraddevus9-Apr-24 4:06 
GeneralRe: Locality of Behavior: term you've been looking for Pin
Greg Utas9-Apr-24 5:04
professionalGreg Utas9-Apr-24 5:04 
GeneralRe: Locality of Behavior: term you've been looking for Pin
raddevus9-Apr-24 5:12
mvaraddevus9-Apr-24 5:12 
GeneralRe: Locality of Behavior: term you've been looking for Pin
Greg Utas9-Apr-24 5:18
professionalGreg Utas9-Apr-24 5:18 
GeneralRe: Locality of Behavior: term you've been looking for Pin
jochance9-Apr-24 9:10
jochance9-Apr-24 9:10 
GeneralRe: Locality of Behavior: term you've been looking for Pin
raddevus9-Apr-24 9:37
mvaraddevus9-Apr-24 9:37 
GeneralRe: Locality of Behavior: term you've been looking for Pin
Sander Rossel9-Apr-24 23:24
professionalSander Rossel9-Apr-24 23:24 
I think you're taking "locality of behaviour" a little too literal.
It's not like all code has to be in a single method or something like that.
Quite the contrary.

Let's say we have a function that fetches an entity from some service, inserts that entity into the database and then returns the ID.
Now, if everything was "local" you'd create an HttpClient, do the fetching, then establish a database connection and do the insert while returning the ID.
You now have to look at one function to understand what it does and to change the behaviour (for example, add a property).

However, you've also coupled your service to your database.
One can't exist without the other.
I probably don't have to explain it would make a lot more sense to create a class that deals with the service and a class that deals with the database insert.
Your function could now look something like:
C#
var something = service.Fetch();
return repository.Insert(something);
That's a gross oversimplification, but you get the gist.
In this example, service and repository are probably injected.
Now you could argue that you don't understand what this code does anymore, but I'd say that's not true.
You still understand the code, it's just that its internals are hidden from you so you don't know how it does it.

Now, let's say a property is added to the service and you also need to store it in the database.
This is where the true locality comes into play.
Because both changes are local to their respective function.
So the service change is a different one from your database change because they're now isolated units of code.
They could be implemented by different teams, the service change could already be implemented while waiting for the database team to add a new column to a table.
And now other parts of the code can also use service.Fetch and repository.Insert in the same way.

Yes, you'll still need to figure out what type of service and repository is (probably some instance of IService and IRepository), but that should be easier than maintaining a long function that does multiple things.

Now how would this code not be "local"?
Let's say the implementation of service.Fetch would do something like "&type=CUST" and then in your repository.Insert you also do a check on "CUST", or some other check that has to do with the internals of service.Fetch.
You'll now always have to change two functions that seem isolated, but really aren't (and you have to know this or you'll break the code!).
Or when service.Fetch does the call to repository.Insert, because you'd think you can fetch, but it's now still coupled to a specific database action.

So in conclusion, don't think of "locality of code" as "code has to be local", but "code has to be as local as possible", which is not the same Smile | :)
The author mentions it too, saying it's often a trade-off Wink | ;)

GeneralRe: Locality of Behavior: term you've been looking for Pin
raddevus10-Apr-24 1:59
mvaraddevus10-Apr-24 1:59 
GeneralRe: Locality of Behavior: term you've been looking for Pin
jschell10-Apr-24 12:07
jschell10-Apr-24 12:07 
GeneralQuestion about english Pin
Nelek9-Apr-24 1:00
protectorNelek9-Apr-24 1:00 
GeneralRe: Question about english Pin
Jo_vb.net9-Apr-24 1:30
mvaJo_vb.net9-Apr-24 1:30 
GeneralRe: Question about english Pin
Nelek9-Apr-24 2:32
protectorNelek9-Apr-24 2:32 
GeneralRe: Question about english Pin
GKP19929-Apr-24 2:09
professionalGKP19929-Apr-24 2:09 
GeneralRe: Question about english Pin
Nelek9-Apr-24 2:32
protectorNelek9-Apr-24 2:32 
GeneralRe: Question about english Pin
GKP19929-Apr-24 3:02
professionalGKP19929-Apr-24 3:02 
GeneralRe: Question about english Pin
Nelek9-Apr-24 5:30
protectorNelek9-Apr-24 5:30 
GeneralRe: Question about english Pin
RickZeeland9-Apr-24 2:29
mveRickZeeland9-Apr-24 2:29 
GeneralRe: Question about english Pin
Nelek9-Apr-24 2:32
protectorNelek9-Apr-24 2:32 
GeneralRe: Question about english Pin
Amarnath S9-Apr-24 2:41
professionalAmarnath S9-Apr-24 2:41 
GeneralRe: Question about english Pin
Nelek9-Apr-24 5:33
protectorNelek9-Apr-24 5:33 
GeneralRe: Question about english Pin
Amarnath S9-Apr-24 6:25
professionalAmarnath S9-Apr-24 6:25 
GeneralRe: Question about english Pin
peterkmx9-Apr-24 6:12
professionalpeterkmx9-Apr-24 6:12 
GeneralRe: Question about english Pin
Jeremy Falcon9-Apr-24 8:18
professionalJeremy Falcon9-Apr-24 8:18 
GeneralRe: Question about english Pin
jmaida9-Apr-24 13:09
jmaida9-Apr-24 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.