-
Notifications
You must be signed in to change notification settings - Fork 130
PAN-2592: Rename methods that create and return streams away from getX() #1368
Conversation
Change all Stream<?> getX() methods to Stream<?> X methods
This may be one for a slightly broader audience. Personally I would have used Ultimately we'll want to be consistent with how we do this so should make a bit of noise about it and probably update the code style doc. |
Update style guide.
I disagree with this. After talking with @shemnon the logic behind this is that getters return references to properties. I feel similarly to @ajsutton that getters (in fact all public methods) should be an abstraction that hides the implementation details. Exposing direct access to properties through getters and expecting it to be direct access makes the code more tightly coupled. |
What then is the difference between a property that uses a Methods that encapsulate operations on the underlying property are just that, methods. By exposing a stream method instead of a property and letting the user stream the property we gain some encapsulation by not exposing the mutable collection that the stream is derived from. But the The Java standards on properties provides zero guidance as to the difference between a getter and a no-args method. Google's internal standards do, and align with this, but they are not available externally. Microsoft has properties vs method guidance (https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms229054(v=vs.100)#section-5). The most relevant two in this case are
|
Making all zero argument methods that don't perform mutations a getter sounds at least consistent. I don't see how it becomes an impediment to understanding. If we suggest that a getter retrieves a value without causing a change to the object you're requesting it from then that's straightforward to understand. Maybe we need to talk more about how you're defining methods and properties. Being able to define a corresponding setter doesn't really factor into whether it should be prefixed get or not, there is plenty of scope for read only values that don't have setters. If the example is logically a collection that's being represented as a stream then perhaps we need For the relevant examples from Microsoft:
I think more relevant points from the Microsoft literature around when a method should be used include:
|
Perhaps we should rephrase the standard to prefer method names unless the method represents the getting or setting of a property. Putting the preference to methods so we don't get into strange cases where "it could be seen as a property" like I feel is going on here. In the Microsoft standards there is a section before providing a positive test for properties over methods: "Do use a property, rather than a method, if the value of the property is stored in the process memory and the property would just provide access to the value." - we are not returning the value of the field but instead an operation on the value of that field. By that standard this is not a property. From the "Not a property" list the second and fifth standard fail, and the third mostly fails.
I've tried searching to see what other projects do in this case. After filtering out I/O Streams all the cases I've seen just return the list and have the user stream off that object. So if we were to follow industry practice we would actually replace the method with one that returns a |
Couple of things:
I'm struggling to see a discernible impact on productivity or code quality so long as the method name makes sense. |
The reason I used a Microsoft standard is because I cannot find any Java based guidance on when to use a getter/setter and when to use a non-property method name. The closest I can find is one that says only use it for returning fields, a very unsatisfying standard. The concern I have is we have a case where the standard is essentially "what is your opinion" rather than an objective standard. This leads to a case such as this where one developer has one opinion, and another developer is blocking approval based on their opinion. That's why I went to dot net, which was heavily influenced by java. Actually I'de love to use Google's recommendations for Java readability, but those are not available outside the chocolate factory. So this creates a governance issue, where one developer has blocked the submission of a PR based on their opinion where the authors feels strongly about their opinion. One opinion has to lose, or it needs to be removed from the realm of opinion. |
# Conflicts: # ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/manager/EthPeers.java # ethereum/eth/src/test/java/tech/pegasys/pantheon/ethereum/eth/sync/fullsync/IncrementerTest.java # ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/PeerDiscoveryAgent.java # ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetworkTest.java
After a zoom call the consensus was
|
# Conflicts: # ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/PeerDiscoveryControllerTest.java
…X() (PegaSysEng#1368) * Change all Stream<?> getX() and Stream<?> x() methods to Stream<?> streanX methods, such as `Stream<Peer> streamIdlePeers()` * Update coding conventions to reflect this.
PR description
Change all Stream getX() methods to Stream streamX() methods