rspec/rules/S3604/rule.adoc

38 lines
714 B
Plaintext
Raw Normal View History

2020-06-30 12:48:39 +02:00
Fields, properties and events can be initialized either inline or in the constructor. Initializing them inline and in the constructor at the same time is redundant; the inline initialization will be overridden.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
class Person
{
int age = 42; // Noncompliant
public Person(int age)
{
this.age = age;
}
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
class Person
{
int age;
public Person(int age)
{
this.age = age;
}
}
----
== Exceptions
This rule doesn't report an issue if not all constructors initialize the field. If the field is initialized inline to its default value, then S3052 already reports an issue on the initialization.