Skip to content
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

[Hackathon 2024][Niobium][IOS] Avoid useless multiple instantiations of formater in a loop #106

Open
raphaelbnpp opened this issue May 30, 2024 · 0 comments
Assignees
Labels
Hackathon 2024 New issues tagged during the hackathon 2024 spotter Techno: iOS

Comments

@raphaelbnpp
Copy link

raphaelbnpp commented May 30, 2024

Rule title

Avoid useless multiple instantiations of formater in a loop

Language and platform

Swift - IOS

Rule description

What does your rule do? Why is this a green coding issue?

My rule checks if there is any formater instantiation inside a loop in the code.
We should rather initiate it upstream the loop to avoid multiple instantiation that are useless.

  • Triggered code:
var date = Date()

for _ in 1...1_000_000 {
    let formater = DateFormatter()
    formater.dateFormat = "dd/MM/yyyy"
    let result = formater.string(from: date)
}

Instead, the following code can be proposed:

  • Suggested code V1 (Instantiate the formater upstream) :
var date = Date()
let formater = DateFormatter()

for _ in 1...1_000_000 {
    formater.dateFormat = "dd/MM/yyyy"
    let result = formater.string(from: date)
}
  • Suggested code V2 (New formatted API):
var date = Date()

for _ in 1...1_000_000 {
    let result = date.formatted(date: .numeric, time: .omitted)
}

Rule short description

Avoid useless multiple instantiations of formater in a loop.

Rule justification

Analyse of the duration of execution by CPU usage.
By taking the triggered and the two untrigerred codes and analyzing the execution time with the use of the CPU we obtain the following results. These measures were done with Instruments.

  • Triggered code
Capture d’écran 2024-05-29 à 17 34 21
  • Suggested code V1 (Instantiate the formatter upstream)
Capture d’écran 2024-05-29 à 17 35 18
  • Suggested code V2 (Using new formatted API)
Capture d’écran 2024-05-29 à 17 36 05

We can observe that the triggered code takes much more time (24s) to be executed than the two other suggested code that takes a similar time to run (1.5s and 1.7s).

Severity / Remediation Cost

Severity: Major (depends of on the quantity of iteration of the loop)

Remediation: Medium (formater should not be declared inside a loop.)

Implementation principle

In any loop, ensure that no formater is instantiated.

@raphaelbnpp raphaelbnpp added Hackathon 2024 New issues tagged during the hackathon 2024 spotter labels May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hackathon 2024 New issues tagged during the hackathon 2024 spotter Techno: iOS
Projects
None yet
Development

No branches or pull requests

2 participants