rspec/rules/S1952/flex/rule.adoc

59 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
It can be expensive to instantiate a new object, and doing so inside a loop is typically an error. Instead, create the object once, before the loop.
2020-06-30 12:47:33 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,flex]
2020-06-30 12:47:33 +02:00
----
for (var i:int = 0; i < 10; i++) {
var temp:MyObj = new MyObject(); // Noncompliant
//...
}
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,flex]
2020-06-30 12:47:33 +02:00
----
var temp:MyObj = new MyObject();
for (var i:int = 0; i < 10; i++) {
//...
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move the instantiation of this "xxx" outside the loop.
'''
== Comments And Links
(visible only on this page)
=== on 7 Apr 2015, 23:24:31 Evgeny Mandrikov wrote:
I'm wondering if this rule is really relevant for C# , Java , C/{cpp}?
\[~dinesh.bolkensteyn], [~nicolas.peru], [~samuel.mercier], [~massimo.paladin], [~pierre-yves.nicolas], [~linda.martin] any thoughts?
=== on 8 Apr 2015, 05:14:57 Dinesh Bolkensteyn wrote:
I don't think it is [~evgeny.mandrikov]
=== on 9 Apr 2015, 13:00:35 Nicolas Peru wrote:
Neither for java. this one is a deprecation of a pmd rule which was already controversial : \http://stackoverflow.com/questions/17340421/pmd-avoid-instantiating-new-objects-inside-loops
So, [~evgeny.mandrikov] I agree with [~dinesh.bolkensteyn] I don't think it is worth to implement in Java.
=== on 29 Mar 2016, 23:48:20 Evgeny Mandrikov wrote:
This case is mentioned in "Google {cpp} Style Guide", so CPP-1448.
endif::env-github,rspecator-view[]