Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Establish Symfony coding standards for private readonly properties and readonly classes #57602

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

Closed
ghost opened this issue Jun 29, 2024 · 18 comments

Comments

@ghost
Copy link

ghost commented Jun 29, 2024

Description

AFAIR, when Symfony bumped the minimum PHP version to 8.1 and people started using private readonly properties in pull requests the response from the core team was negative. I even remember a comment from @nicolas-grekas who said that private readonly properties don't make sense.
Now I can see that we have 257 occurences https://github.com/search?q=repo%3Asymfony%2Fsymfony+%22private+readonly%22&type=code.

I fully agree that private readonly properties do not make sense and only make visual noise. I think people don't understand why this keyword was introduced to the language (I bet 99% don't read RFCs, only New Features section πŸ˜„) and started using it everywhere when they expect a property to be set once, doesn't matter it's a private api, so this keyword became more like a validation/protection use case.
What's more, 99.9% of private readonly properties are dependencies. Have you ever override a property that holds a dependency?

The same goes for readonly classes. Readonly on the class indicates a class is, surprise, readonly. But it makes sense only for structures like DTOs, VO etc. For classes that do a job, i.e. so called services, the readonly keyword doesn't make sense. You don't read anything from this class because they are not designed for reading (by "reading" I mean reading properties, not methods that return some data).

I think having a standard would be nice at least for consistency, otherwise some code will have private readonly properties and some not.
Please add a comment if you vote πŸ‘Ž.

P.S. I bet the readonly keyword would have never been introduced to the PHP if we has asymmetric visibility already.

Reference:
NIcolas' feedback on private readonly properties:
#46674 (comment)
#53279 (comment)

@derrabus
Copy link
Member

Sorry, but your issue description is filled with speculation about how "people" don't understand certain language concepts. I don't want to participate in those speculations, nor do I find them relevant for this project.

I'd like to close this issue.

@ghost
Copy link
Author

ghost commented Jun 29, 2024

@derrabus So you understand this language concept differently? Please elaborate.

Also I would have thought twice before creating this issue if Nicolas hadn't added this comment I remember until today. Taking into account his authority I think there must be something to this topic.

btw. I'm not surprised you voted "no" because I saw many private readonly properties in doctrine/dbal for example.

@derrabus
Copy link
Member

Please elaborate.

No.

@ghost
Copy link
Author

ghost commented Jun 29, 2024

No.

So please delete your previous comment if you don't want to take a part in the discussion.

P.S. I'm sorry to say that but from all the core team members that I respect and like, you are the one that I place in "not so kind" box. If I had to point one person from the core team that needs to work on personal skills I would point you. And I'm saying this based on watching your communication with community since you became a core team member.

@derrabus
Copy link
Member

derrabus commented Jun 29, 2024

No.

So please delete your previous comment

I will do no such thing. I think I made clear why I disapproved of your opening comment and that disapproval will stay for the record.

P.S. I'm sorry to say that but from all the core team members that I respect and like, you are the one that I place in "not so kind" box. If I had to point one person from the core team that needs to work on personal skills I would point you. And I'm saying this based on watching your communication with community since you became a core team member.

I don't know why you feel like you need to take this to the personal level here. Also, I don't think that your personal feelings towards me are relevant for the topic that you allegedly wanted to discuss.

That being said, I do support the initiative to…

Establish Symfony coding standards for private readonly properties and readonly classes

I believe we need some kind of guidelines on when and how we want to use readonly in this codebase. So, if someone were to start a discussion on this topic in a respectful and objective way, I'm sure I'd participate in that discussion.

@ro0NL
Copy link
Contributor

ro0NL commented Jun 29, 2024

private readonly properties don't make sense

i like we cant reset/mutate services/deps.. whats wrong with that?

@ghost
Copy link
Author

ghost commented Jun 29, 2024

i like we cant reset/mutate services/deps.. whats wrong with that?

What is the thing is a trade off. How many times in you life have you overwritten a private property that you haven't supposed to? I've been in PHP world for many years and I've never heard about such a mistake. Also haven't seen a single person asking for feature to protect against overriding private properties. Thus a question is whether we want having all the codebase with readonly keyword everywhere or it's an uncecessary noise that doesn't bring much value if any.

For me marking private property as readonly is like setting a password on a password, where private is the first password (nobody but me can set this property) and readonly is the second password (even though it's private I will add another level of security to protect from myself so I don't set it twice by accident).

@wouterj
Copy link
Member

wouterj commented Jun 29, 2024

Take in mind that the Symfony Code of Conduct is in effect to GitHub issues. This is no place for (unconstructive) personal attacks to anybody, we're having a purely technical discussion.


I personally don't feel the need for a strict standard here. Things like this can be decided on a case-by-case basis.

I also can't remember Nicolas comment nor do I see a reference to it. What would the downsides of going with readonly properties?

@ghost
Copy link
Author

ghost commented Jun 30, 2024

@wouterj I hope this note about Code of Conduct was directed to derrabus who first violated the CoC.

Things like this can be decided on a case-by-case basis.

I don't think the readonly keyword next to the private property is case-by-case specific. This is a standard or not. I also don't think this is something that developers keep an eye on when doing code reviews so it will end up being a mess eventually.

I also can't remember Nicolas comment

I have finally found this comment and even second comment. Issue description updated.

What would the downsides of going with readonly properties?

I think I've explained this in issue description and in this comment #57602 (comment)

@wouterj
Copy link
Member

wouterj commented Jun 30, 2024

The comments tell us that they don't add enough value to make it worth fighting merge conflicts for 2 years. So changing existing properties is probably something we don't want.

However, I don't see any arguments for not adding this to new properties/code.


Personally, I only see a personal attack from you in this issue. Alexander merely stated that he is against this issue and doesn't want to elaborate on that (which is in his full rights).
But if you feel like the CoC is violated, these matters can be handled through the CARE team and not through an attack in the comments.

@wouterj
Copy link
Member

wouterj commented Jun 30, 2024

To further support this, the first hit in the search result is a property introduced by Nicolas: https://github.com/symfony/symfony/pull/48642/files#diff-b24b2d134d203cc7851340a7155af5639befdbe019fcf3bcd8bcf270b1122254R26

So I think our general stance is yes for new properties, no for existing properties (which we may change once all support branches require PHP 8.1+).

@ghost
Copy link
Author

ghost commented Jun 30, 2024

To further support this, the first hit in the search result is a property introduced by Nicolas:

Yes, I have seen that. If you look at the code review in that PR you will find out why Nicolas added readonly and how he commented on that (the emoji is essential) #48642 (comment).
Let's wait until tomorrow as Nicolas seems not to work on the weekends and I'm really curious what his statment will be on this discussion.

@wouterj
Copy link
Member

wouterj commented Jun 30, 2024

Going further, at least 6 different core team members introduced readonly private properties: #48362, #54432, #47112, #51169, #54589, #50112

But sure, let's leave this open for a bit longer to see if other core team members want to confirm the opinion.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 1, 2024

I'm not comfortable with this issue at all. It makes things way too personal. My supposed authority is not an argument on its own. And derrabus just stated his opinion - then you made it personal.

On the topic itself: I don't like readonly props because they are flawed by design (or at least too many think they provide immutability, while that's just false). But they do have merits, so in the end: I don't care. It would be just me, I wouldn't use them much. And also: it would be just me, Symfony wouldn't be as great.

In short: I have other topics more important to care about and given the polemic nature of such topics (not to say the general tone of the discussion), I wouldn't like to enforce any rule on the specific issue of private readonly.

@wouterj
Copy link
Member

wouterj commented Jul 1, 2024

I agree. Let's close here then.

@wouterj wouterj closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
@ghost

This comment was marked as off-topic.

@ghost

This comment was marked as off-topic.

@wouterj
Copy link
Member

wouterj commented Jul 1, 2024

Again, please follow through the CARE team if you think action is required. I'll lock down this issue to prevent any more deviation from the technical discussions that need to happen on GitHub.

@symfony symfony locked as too heated and limited conversation to collaborators Jul 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants