rspec/rules/S3887/rule.adoc

37 lines
923 B
Plaintext
Raw Normal View History

2020-06-30 12:48:39 +02:00
Using the <code>readonly</code> keyword on a field means that it can't be changed after initialization. However, when applied to collections or arrays, that's only partly true. <code>readonly</code> enforces that another instance can't be assigned to the field, but it cannot keep the contents from being updated. That means that in practice, the field value really can be changed, and the use of <code>readonly</code> on such a field is misleading, and you're likely to not be getting the behavior you expect.
This rule raises an issue when a non-private, <code>readonly</code> field is an array or collection.
== Noncompliant Code Example
----
public class MyClass
{
public readonly string[] strings; // Noncompliant
// ...
----
== Compliant Solution
----
public class MyClass
{
public string[] strings;
// ...
----
or
2020-06-30 12:48:39 +02:00
----
public class MyClass
{
private readonly string[] strings;
// ...
----