Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

space: Confusing API with methods with similar names #980

Open
rht opened this issue Jan 28, 2021 · 12 comments
Open

space: Confusing API with methods with similar names #980

rht opened this issue Jan 28, 2021 · 12 comments
Assignees

Comments

@rht
Copy link
Contributor

rht commented Jan 28, 2021

The class Grid has:

  • neighbor_iter: "Iterate over position neighbors"
  • iter_neighborhood: "Return an iterator over cell coordinates that are in the neighborhood of a certain point"
  • iter_neighbors: "Return an iterator over neighbors to a certain point"

neighbor_iter and iter_neighbors seem to be a duplicate of each other.
iter_neighborhood should probably be called iter_neighbor_coordinates due to the type of its return value.

@Corvince
Copy link
Contributor

neighbor_iter and iter_neighbors seem to be a duplicate of each other.

Yes, except neighbor_iter has a restricted API, but HexGrid only implements neighbor_iter and not iter_neighbors 🤦

iter_neighbors should be the only function IMO.

iter_neighborhood should probably be called iter_neighbor_coordinates due to the type of its return value.

Disagree. For me it was always clear that neighbors => Agents, neighborhood => Coordinates

@rht
Copy link
Contributor Author

rht commented Jan 28, 2021

Disagree. For me it was always clear that neighbors => Agents, neighborhood => Coordinates

Is it just me or that the dictionary definition of "neighborhood" refers to people, not geographical coordinates?

From https://en.wikipedia.org/wiki/Neighbourhood:

A neighbourhood (British English, Australian English and Canadian English) or neighborhood 
(American English; see spelling differences—u is omitted in American English) is a geographically 
localised community within a larger city, town, suburb or rural area. Neighbourhoods are often 
social communities with considerable face-to-face interaction among members.

@Corvince
Copy link
Contributor

Is it just me or that the dictionary definition of "neighborhood" refers to people, not geographical coordinates?

Well maybe look it up in a dictionary!?

From https://www.dictionary.com/browse/neighborhood?s=t

the area or region around or near some place or thing; vicinity

If you would have read the wikipedia just a little bit further, it would also say this:

Neighbourhood is generally defined spatially as a specific geographic area and functionally as a set of social networks.

So both definition exists and I definitely see your point, but actually both definitions are irrelevant in this context.

The origin of neighborhoods in the context of ABMs relies on the mathematical definition of neighborhoods, which is just an open set of points that contains a specific point. No people involved

@rht
Copy link
Contributor Author

rht commented Jan 29, 2021

If iter_neighborhood is find, then the problematic wording is the neighbor_iter and iter_neighbors. They should be called iter_neighborhood_content for clarity.

@rht
Copy link
Contributor Author

rht commented Jan 30, 2021

Since a good method name pattern is something along the line of "verb_object", iter_neighbors sounds better for me than neighbor_iter. But iter_neighbors is never used in the examples.

@dmasad dmasad self-assigned this Feb 11, 2021
@dmasad
Copy link
Member

dmasad commented Mar 26, 2021

My suggestion for an alternative API is to make it explicit what's being returned. So get_neighbors would become get_neighboring_agents and get_neighborhood would become get_neighboring_coordinates. We could keep the old signatures as well for a while, and have them be pass-throughs to the new methods along with a DeprecationWarning.

@jackiekazil
Copy link
Member

RE: #1184 - this I was looking it over. I really appreciate the clean up. @rht is there a reason why you chose iter_neighbors over get_neighbors? The reason I ask is because to newer users, get_neighbors is easier to understand.

@tpike3
Copy link
Member

tpike3 commented Mar 22, 2022

RE: #1184 - this I was looking it over. I really appreciate the clean up. @rht is there a reason why you chose iter_neighbors over get_neighbors? The reason I ask is because to newer users, get_neighbors is easier to understand.

There is a subtle distinction here as get_neighbors is a function as well:

get_neighbors: Returns the objects surrounding a given cell.
get_neighborhood: Returns the cells surrounding a given cell.
iter_neighbors: Iterates over position neighbors.
iter_neighborhood: Returns an iterator over cell coordinates that are in the neighborhood of a certain point.

I concur new users will use get_neighbors as more intuitive but as they get more sophisticated iter_neighbors will provide more flexibility with an iterator.

@rht
Copy link
Contributor Author

rht commented Mar 22, 2022

I didn't "choose" iter_neighbors in #1184, since both iter_neighbors and get_neighbors are already present. That PR specifically deletes only neighbor_iter.

The further deletion of iter_* in favor of get_* can be done in a separate PR. Regarding with returning iterator being confusing, I think it is fine, since people are already used to range(5) being an iterator anyway.

@jackiekazil
Copy link
Member

The pr was merged. Ty.

@rht
Copy link
Contributor Author

rht commented Apr 4, 2022

#815 (comment) contains argument on why it is beneficial to use get_* over iter_*: a list can be cached, but an iterator cant. With this, I'm convinced that get_* that returns list is the way to go.

@rht
Copy link
Contributor Author

rht commented Apr 4, 2022

On reviewing #815, I realized that the caching mechanism of neighborhood has already been incorporated in current main. This means that both iter_neighbors and get_neighbors use cached result of the neighborhood. OK, this means that it is still performant to use only get_neighbors that returns an iterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants