From f14fbeecdead614ee692dc5788adbe152b006bef Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Tue, 30 Apr 2024 09:39:17 +0200 Subject: [PATCH 01/31] Added Swift port for rule EC513 --- CHANGELOG.md | 1 + .../src/main/rules/EC513/swift/EC513.asciidoc | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC513/swift/EC513.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index ba6adfee..c64901bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#286](https://github.com/green-code-initiative/ecoCode/issues/286) [EC83] [C#] Replace Enum ToString() with nameof - [#27](https://github.com/green-code-initiative/ecoCode-csharp/issues/27) [EC84] [C#] Avoid async void methods - [#34](https://github.com/green-code-initiative/ecoCode-csharp/issues/34) [EC85] [C#] Make type sealed +- [#293](https://github.com/green-code-initiative/ecoCode/issues/293) [EC513] Swift port ### Changed diff --git a/ecocode-rules-specifications/src/main/rules/EC513/swift/EC513.asciidoc b/ecocode-rules-specifications/src/main/rules/EC513/swift/EC513.asciidoc new file mode 100644 index 00000000..efcb0b78 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC513/swift/EC513.asciidoc @@ -0,0 +1,57 @@ +Most iOS devices come equipped with a variety of sensors that measure motion, orientation, and various environmental conditions. +In addition to these, the devices include advanced sensors such as the image sensor (commonly referred to as the Camera) and the geo-positioning sensor (commonly referred to as GPS). + +The common point of all these sensors is that they are power-intensive while in use. A typical issue arises when these sensors continue to process data unnecessarily after the application enters an idle state, typically when paused or when the user stops interacting with it. + + Consequently, calls must be carefully paired: `CLLocationManager.startUpdatingLocation()` and `CLLocationManager.stopUpdatingLocation()`. + Failing to do so can drain the battery in just a few hours. + +== Noncompliant Code Example + +[source,swift] +---- +import CoreLocation + +class LocationTracker: NSObject, CLLocationManagerDelegate { + var locationManager: CLLocationManager? + + override init() { + super.init() + locationManager = CLLocationManager() + locationManager?.delegate = self + locationManager?.requestAlwaysAuthorization() // Request appropriate authorization + locationManager?.startUpdatingLocation() // Start location updates + } + + // LocationManager Delegate Methods + func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + // Process new locations + } +} +---- + +== Compliant Code Example + +[source,swift] +---- +import CoreLocation + +class LocationTracker: NSObject, CLLocationManagerDelegate { + var locationManager: CLLocationManager? + + override init() { + super.init() + locationManager = CLLocationManager() + locationManager?.delegate = self + locationManager?.requestAlwaysAuthorization() // Request appropriate authorization + locationManager?.startUpdatingLocation() // Start location updates only when needed + } + + // LocationManager Delegate Methods + func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + // Process new locations + // Possibly stop updates if they are no longer needed + locationManager?.stopUpdatingLocation() + } +} +---- From 344db238ffb52cd1d67e074b09d3b9be3d1cdc27 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Wed, 29 May 2024 15:16:34 +0200 Subject: [PATCH 02/31] Added EC514 for Swift --- .../src/main/rules/EC514/java/EC514.asciidoc | 48 +++++++++++++------ .../src/main/rules/EC514/swift/EC514.asciidoc | 24 ++++++++++ 2 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc index 2f06dd28..67acb5c8 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc @@ -1,24 +1,44 @@ -Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. - In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). +Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS). -The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. +The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. - Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. - Failing to do so can drain the battery in just a few hours. + Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). + Failing to do so can drain the battery quickly. ## Noncompliant Code Example -```java -SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); -Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); -sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} ``` ## Compliant Code Example -```java -SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); -Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); -sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); -sManager.unregisterListener(this); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} + +func stopMotionUpdates() { + if motionManager.isAccelerometerActive { + motionManager.stopAccelerometerUpdates() + } +} ``` diff --git a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc new file mode 100644 index 00000000..2f06dd28 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -0,0 +1,24 @@ +Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). + +The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. + + Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. + Failing to do so can drain the battery in just a few hours. + +## Noncompliant Code Example + +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); +sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +``` + +## Compliant Code Example + +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); +sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +sManager.unregisterListener(this); +``` From b93f39953bf7447165833ef2fe4caa8e1f6b0e92 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Wed, 29 May 2024 15:18:52 +0200 Subject: [PATCH 03/31] Corrected EC514 content --- .../src/main/rules/EC514/java/EC514.asciidoc | 48 ++++++------------- .../src/main/rules/EC514/swift/EC514.asciidoc | 48 +++++++++++++------ 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc index 67acb5c8..2f06dd28 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc @@ -1,44 +1,24 @@ -Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS). +Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). -The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. +The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. - Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). - Failing to do so can drain the battery quickly. + Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. + Failing to do so can drain the battery in just a few hours. ## Noncompliant Code Example -```swift -import CoreMotion - -let motionManager = CMMotionManager() - -func startMotionUpdates() { - if motionManager.isAccelerometerAvailable { - motionManager.startAccelerometerUpdates(to: .main) { data, error in - // Handle accelerometer updates - } - } -} +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); +sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); ``` ## Compliant Code Example -```swift -import CoreMotion - -let motionManager = CMMotionManager() - -func startMotionUpdates() { - if motionManager.isAccelerometerAvailable { - motionManager.startAccelerometerUpdates(to: .main) { data, error in - // Handle accelerometer updates - } - } -} - -func stopMotionUpdates() { - if motionManager.isAccelerometerActive { - motionManager.stopAccelerometerUpdates() - } -} +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); +sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +sManager.unregisterListener(this); ``` diff --git a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc index 2f06dd28..67acb5c8 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -1,24 +1,44 @@ -Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. - In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). +Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS). -The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. +The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. - Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. - Failing to do so can drain the battery in just a few hours. + Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). + Failing to do so can drain the battery quickly. ## Noncompliant Code Example -```java -SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); -Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); -sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} ``` ## Compliant Code Example -```java -SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); -Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); -sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); -sManager.unregisterListener(this); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} + +func stopMotionUpdates() { + if motionManager.isAccelerometerActive { + motionManager.stopAccelerometerUpdates() + } +} ``` From 4cfe35165df3a91b6a9f10fcd0871deec27b9026 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Wed, 29 May 2024 15:25:21 +0200 Subject: [PATCH 04/31] Updated CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21a7ee4b..a5ba9f1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [#306] (https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 + ### Changed ### Deleted From fd9d6f4c6966843e48302cc609692cc32000dfaa Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 09:09:40 +0200 Subject: [PATCH 05/31] Changelog update --- CHANGELOG.md | 1 + .../src/main/rules/EC515/swift/EC515.asciidoc | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index 21a7ee4b..0cfb54ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port ### Changed diff --git a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc new file mode 100644 index 00000000..2053d051 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc @@ -0,0 +1,16 @@ +Creation of a Media Recorder object with `new MediaRecorder()` is used to record audio and video. Class own a `release()` method. + +In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a media object is no longer needed may also lead to continuous battery consumption for mobile devices. + +## Noncompliant Code Example + +```java +MediaRecorder mr = new MediaRecorder(); +``` + +## Compliant Solution + +```java +MediaRecorder mr = new MediaRecorder(); +mr.release(); +``` From 94933bca4d3e0ddf7e880bede708565c0694b9d5 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 09:16:35 +0200 Subject: [PATCH 06/31] Corrected EC515 content --- .../src/main/rules/EC515/swift/EC515.asciidoc | 132 ++++++++++++++++-- 1 file changed, 123 insertions(+), 9 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc index 2053d051..ebe4754c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc @@ -1,16 +1,130 @@ -Creation of a Media Recorder object with `new MediaRecorder()` is used to record audio and video. Class own a `release()` method. +Creation of an `AVAudioRecorder` or `AVCaptureSession` object is used to record audio and video. These classes have methods to stop recording and release resources. -In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a media object is no longer needed may also lead to continuous battery consumption for mobile devices. +In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to properly stop and release these objects if they are no longer needed may also lead to continuous battery consumption for mobile devices. -## Noncompliant Code Example -```java -MediaRecorder mr = new MediaRecorder(); +## Noncompliant Code Examples + +```swift +import AVFoundation + +var audioRecorder: AVAudioRecorder? + +func startRecording() { + let settings = [ + AVFormatIDKey: Int(kAudioFormatMPEG4AAC), + AVSampleRateKey: 12000, + AVNumberOfChannelsKey: 1, + AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue + ] + + do { + audioRecorder = try AVAudioRecorder(url: getDocumentsDirectory().appendingPathComponent("recording.m4a"), settings: settings) + audioRecorder?.record() + } catch { + // Handle error + } +} +``` + +```swift +import AVFoundation + +var captureSession: AVCaptureSession? + +func startVideoRecording() { + captureSession = AVCaptureSession() + captureSession?.beginConfiguration() + + let videoDevice = AVCaptureDevice.default(for: .video) + let audioDevice = AVCaptureDevice.default(for: .audio) + + do { + let videoInput = try AVCaptureDeviceInput(device: videoDevice!) + let audioInput = try AVCaptureDeviceInput(device: audioDevice!) + + if (captureSession?.canAddInput(videoInput) ?? false) { + captureSession?.addInput(videoInput) + } + + if (captureSession?.canAddInput(audioInput) ?? false) { + captureSession?.addInput(audioInput) + } + + captureSession?.commitConfiguration() + captureSession?.startRunning() + } catch { + // Handle error + } +} ``` -## Compliant Solution +## Compliant Solutions -```java -MediaRecorder mr = new MediaRecorder(); -mr.release(); +```swift +import AVFoundation + +var audioRecorder: AVAudioRecorder? + +func startRecording() { + let settings = [ + AVFormatIDKey: Int(kAudioFormatMPEG4AAC), + AVSampleRateKey: 12000, + AVNumberOfChannelsKey: 1, + AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue + ] + + do { + audioRecorder = try AVAudioRecorder(url: getDocumentsDirectory().appendingPathComponent("recording.m4a"), settings: settings) + audioRecorder?.record() + } catch { + // Handle error + } +} + +func stopRecording() { + if let recorder = audioRecorder, recorder.isRecording { + recorder.stop() + audioRecorder = nil + } +} ``` + +```swift +import AVFoundation + +var captureSession: AVCaptureSession? + +func startVideoRecording() { + captureSession = AVCaptureSession() + captureSession?.beginConfiguration() + + let videoDevice = AVCaptureDevice.default(for: .video) + let audioDevice = AVCaptureDevice.default(for: .audio) + + do { + let videoInput = try AVCaptureDeviceInput(device: videoDevice!) + let audioInput = try AVCaptureDeviceInput(device: audioDevice!) + + if (captureSession?.canAddInput(videoInput) ?? false) { + captureSession?.addInput(videoInput) + } + + if (captureSession?.canAddInput(audioInput) ?? false) { + captureSession?.addInput(audioInput) + } + + captureSession?.commitConfiguration() + captureSession?.startRunning() + } catch { + // Handle error + } +} + +func stopVideoRecording() { + if let session = captureSession, session.isRunning { + session.stopRunning() + captureSession = nil + } +} +``` \ No newline at end of file From 1fc0f7b372f979f8e990b4d4e776525d724f588f Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 10:06:20 +0200 Subject: [PATCH 07/31] EC515 Swift update --- .../src/main/rules/EC515/swift/EC515.asciidoc | 79 +------------------ 1 file changed, 4 insertions(+), 75 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc index ebe4754c..953d30f4 100644 --- a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc @@ -1,9 +1,9 @@ -Creation of an `AVAudioRecorder` or `AVCaptureSession` object is used to record audio and video. These classes have methods to stop recording and release resources. +Creation of an `AVAudioRecorder` object is used to record audio. These class has a method to stop recording and release resources. -In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to properly stop and release these objects if they are no longer needed may also lead to continuous battery consumption for mobile devices. +In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to properly stop and release these object if it is no longer needed may also lead to continuous battery consumption for mobile devices. -## Noncompliant Code Examples +## Noncompliant Code Example ```swift import AVFoundation @@ -27,39 +27,7 @@ func startRecording() { } ``` -```swift -import AVFoundation - -var captureSession: AVCaptureSession? - -func startVideoRecording() { - captureSession = AVCaptureSession() - captureSession?.beginConfiguration() - - let videoDevice = AVCaptureDevice.default(for: .video) - let audioDevice = AVCaptureDevice.default(for: .audio) - - do { - let videoInput = try AVCaptureDeviceInput(device: videoDevice!) - let audioInput = try AVCaptureDeviceInput(device: audioDevice!) - - if (captureSession?.canAddInput(videoInput) ?? false) { - captureSession?.addInput(videoInput) - } - - if (captureSession?.canAddInput(audioInput) ?? false) { - captureSession?.addInput(audioInput) - } - - captureSession?.commitConfiguration() - captureSession?.startRunning() - } catch { - // Handle error - } -} -``` - -## Compliant Solutions +## Compliant Solution ```swift import AVFoundation @@ -88,43 +56,4 @@ func stopRecording() { audioRecorder = nil } } -``` - -```swift -import AVFoundation - -var captureSession: AVCaptureSession? - -func startVideoRecording() { - captureSession = AVCaptureSession() - captureSession?.beginConfiguration() - - let videoDevice = AVCaptureDevice.default(for: .video) - let audioDevice = AVCaptureDevice.default(for: .audio) - - do { - let videoInput = try AVCaptureDeviceInput(device: videoDevice!) - let audioInput = try AVCaptureDeviceInput(device: audioDevice!) - - if (captureSession?.canAddInput(videoInput) ?? false) { - captureSession?.addInput(videoInput) - } - - if (captureSession?.canAddInput(audioInput) ?? false) { - captureSession?.addInput(audioInput) - } - - captureSession?.commitConfiguration() - captureSession?.startRunning() - } catch { - // Handle error - } -} - -func stopVideoRecording() { - if let session = captureSession, session.isRunning { - session.stopRunning() - captureSession = nil - } -} ``` \ No newline at end of file From d0b2e8cc89a03367d4d845ff40559c3e218708cd Mon Sep 17 00:00:00 2001 From: E000391 Date: Thu, 30 May 2024 11:24:09 +0200 Subject: [PATCH 08/31] add rule EC1369 --- .../src/main/rules/EC1369/EC1369.json | 15 +++++++++++++ .../main/rules/EC1369/java/EC1369.asciidoc | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json b/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json new file mode 100644 index 00000000..aea5000b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json @@ -0,0 +1,15 @@ +{ + "title": "Performance: orElseGet instead of orElse", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1min" + }, + "tags": [ + "ecocode", + "eco-design", + "performance" + ], + "defaultSeverity": "Minor" +} diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc b/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc new file mode 100644 index 00000000..77dfc09f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc @@ -0,0 +1,21 @@ +Parameter of orElse() is evaluated, even when having a non-empty Optional. + +Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. + +Therefore, using orElseGet() will save computing time. + +## Noncompliant Code Example + +```java +Optional.of("ecoCode").orElse(getUnpredictedMethod()); +``` + +## Compliant Code Example + +```java +Optional.of("ecoCode").orElseGet(() -> getUnpredictedMethod()); +``` + +```java +randomClass.orElse(getUnpredictedMethod()); +``` From e27bea452b95ca39b3316c217de45c64a12e1392 Mon Sep 17 00:00:00 2001 From: Benjamin DELAHAIS Date: Thu, 30 May 2024 13:28:03 +0200 Subject: [PATCH 09/31] [Python #26][EC1337] Avoid unlimited cache --- CHANGELOG.md | 2 ++ .../src/main/rules/EC1337/EC1337.json | 17 +++++++++++ .../main/rules/EC1337/python/EC1337.asciidoc | 29 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC1337/EC1337.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index 21a7ee4b..12d8bf76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +[Python #26](https://github.com/green-code-initiative/ecoCode-python/issues/26) [EC1337] [Python] Avoid unlimited cache + ### Changed ### Deleted diff --git a/ecocode-rules-specifications/src/main/rules/EC1337/EC1337.json b/ecocode-rules-specifications/src/main/rules/EC1337/EC1337.json new file mode 100644 index 00000000..a4b4753c --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC1337/EC1337.json @@ -0,0 +1,17 @@ +{ + "title": "Avoid suspect unlimited cache size", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "network", + "cache", + "ecocode" + ], + "defaultSeverity": "Major" +} diff --git a/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc b/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc new file mode 100644 index 00000000..e986428d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc @@ -0,0 +1,29 @@ +The cache is unlimited which is suspect. An unlimited cache can take a lot of RAM / Disk space. + +## Noncompliant Code Examples + +```python + @cache # Noncompliant + def cached_function(): + ... +``` + +```python + @lru_cache(maxsize=None) # Noncompliant + def cached_function(): + ... +``` + +## Compliant Solution + +```python + @lru_cache() # By default, the max size of the cache is 128 + def cached_function(): + ... +``` + +```python + @lru_cache(maxsize=16) # Define a max size to the cache + def cached_function(): + ... +``` From 1cb9478c5f8409b09fdd3bce3cb3c376757d9d3b Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 13:30:05 +0200 Subject: [PATCH 10/31] Add EC522 to javascript (#321) --- CHANGELOG.md | 1 + .../src/main/rules/EC522/EC522.json | 10 +------ .../src/main/rules/EC522/java/EC522.json | 9 ++++++ .../rules/EC522/javascript/EC522.asciidoc | 30 +++++++++++++++++++ .../main/rules/EC522/javascript/EC522.json | 13 ++++++++ .../src/main/rules/EC522/swift/EC522.json | 9 ++++++ 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC522/java/EC522.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 73939e07..13ef138e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript +- [#321](https://github.com/green-code-initiative/ecoCode/pull/321) Add rule EC522 for javascript (avoid brightness override) ### Changed diff --git a/ecocode-rules-specifications/src/main/rules/EC522/EC522.json b/ecocode-rules-specifications/src/main/rules/EC522/EC522.json index c333af54..7835b7a6 100644 --- a/ecocode-rules-specifications/src/main/rules/EC522/EC522.json +++ b/ecocode-rules-specifications/src/main/rules/EC522/EC522.json @@ -6,14 +6,6 @@ "func": "Constant\/Issue", "constantCost": "20min" }, - "tags": [ - "sobriety", - "environment", - "ecocode", - "android", - "ios", - "eco-design" - ], "ecoScore": "0.4", "defaultSeverity": "Minor" -} \ No newline at end of file +} diff --git a/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.json b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.json new file mode 100644 index 00000000..639ebdd0 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.json @@ -0,0 +1,9 @@ +{ + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.asciidoc b/ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.asciidoc new file mode 100644 index 00000000..94e8c6f5 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.asciidoc @@ -0,0 +1,30 @@ +:!sectids: + +== Why is this an issue? + +To avoid draining the battery, iOS and Android devices adapt the brightness of the screen depending on the environment light. + +For some reasons, developers may disable this feature programmatically. + +This feature was introduced to improve battery life, be careful when deactivating it. + +Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary. + +== Example of non compliant code + +```js +// Example with expo-brightness (Expo framework library) +import React, { useEffect } from 'react'; +import { View, Text } from 'react-native'; +import * as Brightness from 'expo-brightness'; + +export default function App() { + useEffect(() => { + (async () => { Brightness.setSystemBrightnessAsyn(1); })(); // Brightness is forced here + }, []); + return ( + + Brightness Module Example + + ); +} diff --git a/ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.json b/ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.json new file mode 100644 index 00000000..e32eae11 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC522/javascript/EC522.json @@ -0,0 +1,13 @@ +{ + "tags": [ + "sobriety", + "environment", + "ecocode", + "react-native", + "eco-design" + ], + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.json b/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.json new file mode 100644 index 00000000..bfcb6581 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.json @@ -0,0 +1,9 @@ +{ + "tags": [ + "sobriety", + "environment", + "ecocode", + "ios", + "eco-design" + ] +} From 3b57ae5c5844c81d8f5e1fb24280ba6d447c81d3 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 13:50:58 +0200 Subject: [PATCH 11/31] Update CHANGELOG.md --- CHANGELOG.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0500a9be..8e6376cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,11 +47,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Swift rules cleanup and updates (removed duplicated rules, added [EC602]) -- [#18](https://github.com/green-code-initiative/ecoCode-csharp/issues/18) [EC81] [C#] Specify struct layout -- [#285](https://github.com/green-code-initiative/ecoCode/pull/285) [EC82] [C#] Cariable can be made constant -- [#286](https://github.com/green-code-initiative/ecoCode/issues/286) [EC83] [C#] Replace Enum ToString() with nameof -- [#27](https://github.com/green-code-initiative/ecoCode-csharp/issues/27) [EC84] [C#] Avoid async void methods -- [#34](https://github.com/green-code-initiative/ecoCode-csharp/issues/34) [EC85] [C#] Make type sealed - [#293](https://github.com/green-code-initiative/ecoCode/issues/293) [EC513] Swift port - [C# #18](https://github.com/green-code-initiative/ecoCode-csharp/issues/18) [EC81] [C#] Specify struct layout - [C# #285](https://github.com/green-code-initiative/ecoCode/pull/285) [EC82] [C#] Variable can be made constant From 07ebadb693a83886d5447a772ae1a9418f585e08 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 13:57:18 +0200 Subject: [PATCH 12/31] Update EC514.asciidoc --- .../src/main/rules/EC514/swift/EC514.asciidoc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc index 67acb5c8..37eb6dcb 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -5,9 +5,10 @@ The common point of all these sensors is that they consume significant power whi Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). Failing to do so can drain the battery quickly. -## Noncompliant Code Example +== Noncompliant Code Example -```swift +[source,swift] +---- import CoreMotion let motionManager = CMMotionManager() @@ -19,11 +20,12 @@ func startMotionUpdates() { } } } -``` +---- -## Compliant Code Example +== Compliant Code Example -```swift +[source,swift] +---- import CoreMotion let motionManager = CMMotionManager() @@ -41,4 +43,4 @@ func stopMotionUpdates() { motionManager.stopAccelerometerUpdates() } } -``` +---- From f20825ceb2cb44ce61b4d4e97b059115dc50c876 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 13:58:49 +0200 Subject: [PATCH 13/31] Update EC515.asciidoc --- .../src/main/rules/EC515/swift/EC515.asciidoc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc index 953d30f4..3770fded 100644 --- a/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC515/swift/EC515.asciidoc @@ -3,9 +3,10 @@ Creation of an `AVAudioRecorder` object is used to record audio. These class has In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to properly stop and release these object if it is no longer needed may also lead to continuous battery consumption for mobile devices. -## Noncompliant Code Example +== Noncompliant Code Example -```swift +[source,swift] +---- import AVFoundation var audioRecorder: AVAudioRecorder? @@ -25,11 +26,12 @@ func startRecording() { // Handle error } } -``` +---- -## Compliant Solution +== Compliant Solution -```swift +[source,swift] +---- import AVFoundation var audioRecorder: AVAudioRecorder? @@ -56,4 +58,4 @@ func stopRecording() { audioRecorder = nil } } -``` \ No newline at end of file +---- From 4066e213f88ef43eac186f15f89aaf9e0ba34ea7 Mon Sep 17 00:00:00 2001 From: Pima-Dev Date: Thu, 30 May 2024 14:37:08 +0200 Subject: [PATCH 14/31] Add EC523 to javascript (#319) * Merge EC523 with EC8 --- CHANGELOG.md | 3 + RULES.md | 30 ++++---- .../src/main/rules/EC523/EC523.json | 10 +-- .../src/main/rules/EC523/java/EC523.json | 9 +++ .../rules/EC523/javascript/EC523.asciidoc | 68 +++++++++++++++++++ .../main/rules/EC523/javascript/EC523.json | 13 ++++ .../src/main/rules/EC8/EC8.json | 25 ------- .../main/rules/EC8/javascript/EC8.asciidoc | 42 ------------ 8 files changed, 110 insertions(+), 90 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC523/java/EC523.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.json delete mode 100644 ecocode-rules-specifications/src/main/rules/EC8/EC8.json delete mode 100644 ecocode-rules-specifications/src/main/rules/EC8/javascript/EC8.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ef138e..4b200693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [#318](https://github.com/green-code-initiative/ecoCode/issues/318) Implement the rule EC523 for React Native + ### Deleted +- [#318](https://github.com/green-code-initiative/ecoCode/issues/318) Merge the rule EC8 with EC523 ## [1.5.4] - 2024-05-24 diff --git a/RULES.md b/RULES.md index 05310c4b..9c4a2257 100644 --- a/RULES.md +++ b/RULES.md @@ -1,10 +1,10 @@ # Rules - [Rules](#rules) - - [Rules support matrix by techno](#rules-support-matrix-by-techno) - - [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified) - - [Deprecated rules](#deprecated-rules) - - [Refused / Deleted rules](#refused--deleted-rules) + - [Rules support matrix by techno](#rules-support-matrix-by-techno) + - [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified) + - [Deprecated rules](#deprecated-rules) + - [Refused / Deleted rules](#refused--deleted-rules) ## Rules support matrix by techno @@ -30,7 +30,6 @@ Some are applicable for different technologies. | EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | βœ… | πŸš€ | βœ… | πŸš€ | 🚫 | 🚫 | | EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | βœ… | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | | EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | 🚫 | πŸš€ | βœ… | πŸš€ | 🚫 | 🚫 | -| EC8 | Avoid using high accuracy geolocation | ESLint key : @ecocode/avoid-high-accuracy-geolocation /// type : Front-end | | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | 🚫 | | EC9 | No import all from library | ESLint key : @ecocode/no-import-all-from-library /// type : Common | | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | 🚫 | | EC10 | Use unoptimized vector images | Less heavy SVG images using less bandwidth | [cnumr best practices (3rd edition) BP_036](https://github.com/cnumr/best-practices/blob/main/chapters/BP_036_fr.md) | 🚧 | πŸš€ | πŸš€ | βœ… | πŸš€ | 🚫 | ❓ | | EC11 | Call a DOM element multiple times without caching | ESLint key : @ecocode/no-multiple-access-dom-element /// type : Front-end | | 🚫 | ❓ | βœ… | 🚫 | 🚫 | ❓ | 🚫 | @@ -78,7 +77,8 @@ Some are applicable for different technologies. ## Rules to be reworked / measured / clarified -This table lists rules proposed by the community but they have to be reworked / measured / clarified before being implemented in ecoCode plugins. +This table lists rules proposed by the community but they have to be reworked / measured / clarified before being +implemented in ecoCode plugins. (Issues and PR are closed, but they can be reopen once rework launched) | Rule key | Language | Name | Description | Invalidation | @@ -93,7 +93,8 @@ This table lists rules proposed by the community but they have to be reworked / ## Deprecated rules -This table lists rules proposed by the community but deprecated in ecoCode plugins with the justification. These rules will be completely deleted in next releases and moved to bottom deleted rules array. +This table lists rules proposed by the community but deprecated in ecoCode plugins with the justification. These rules +will be completely deleted in next releases and moved to bottom deleted rules array. | Rule key | Language | Name | Description | Invalidation | |----------|---------------------|----------------------------------------------------|----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| @@ -104,10 +105,11 @@ This table lists rules proposed by the community but deprecated in ecoCode plugi This table lists rules proposed by the community but refused or/and deleted in ecoCode plugins with the justification. -| Rule key | Language | Name | Description | Invalidation | -|----------|----------|---------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| CRDOM203 | HTML | HTML page must contain a doctype tag | The difference in performance is negligible, this rule is more related to the user experience. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/103) | -| CRPYT | Python | Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | -| EC4 | Java | Avoid using global variables | Global variables do not exist in Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/233) | -| EC63 | Java | Unnecessarily assigning values to variables | There are already 3 native SonarQube rules for Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/pull/258) | -| EC75 | Java | Don't concatenate strings in loops | Optimizations on Java concatenation Strings are useless since JDK8 | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/246) | +| Rule key | Language | Name | Description | Invalidation | +|----------|------------|---------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------| +| CRDOM203 | HTML | HTML page must contain a doctype tag | The difference in performance is negligible, this rule is more related to the user experience. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/103) | +| CRPYT | Python | Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | +| EC4 | Java | Avoid using global variables | Global variables do not exist in Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/233) | +| EC63 | Java | Unnecessarily assigning values to variables | There are already 3 native SonarQube rules for Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/pull/258) | +| EC75 | Java | Don't concatenate strings in loops | Optimizations on Java concatenation Strings are useless since JDK8 | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/246) | +| EC8 | JavaScript | Avoid using high accuracy geolocation | The rule has been merged with the rule EC523 | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/318) | diff --git a/ecocode-rules-specifications/src/main/rules/EC523/EC523.json b/ecocode-rules-specifications/src/main/rules/EC523/EC523.json index 5ac0edef..3ae99eb6 100644 --- a/ecocode-rules-specifications/src/main/rules/EC523/EC523.json +++ b/ecocode-rules-specifications/src/main/rules/EC523/EC523.json @@ -6,14 +6,6 @@ "func": "Constant\/Issue", "constantCost": "10min" }, - "tags": [ - "sobriety", - "environment", - "ecocode", - "android", - "ios", - "eco-design" - ], "ecoScore": "0.4", "defaultSeverity": "Major" -} \ No newline at end of file +} diff --git a/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.json b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.json new file mode 100644 index 00000000..639ebdd0 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.json @@ -0,0 +1,9 @@ +{ + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.asciidoc b/ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.asciidoc new file mode 100644 index 00000000..a6e81bb7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.asciidoc @@ -0,0 +1,68 @@ +:!sectids: + +== Why is this an issue? + +High-precision geolocation typically requires more power from the device's GPS hardware. +By requesting less accurate geolocation, you can reduce the power consumption, leading to extended battery life, which +is crucial for mobile devices. + +Obtaining highly accurate geolocation often involves more complex calculations and processing, which can increase CPU +usage. +If the application or service does not critically require pinpoint accuracy, opting for a less accurate geolocation can +help minimize the strain on the device's CPU. + +== Web + +[source,js] +---- +var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; // Non-compliant +navigator.geolocation.getCurrentPosition( + (pos) => console.log(pos), + (err) => console.warn(err), + options +); +---- + +In these examples, the enableHighAccuracy option is set to false (the default), indicating that the application prefers +lower-accuracy geolocation to conserve resources: + +[source,js] +---- +navigator.geolocation.getCurrentPosition((pos) => console.log(pos)); // Compliant by default +---- + +[source,js] +---- +var options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 }; // Compliant +navigator.geolocation.getCurrentPosition( + (pos) => console.log(pos), + (err) => console.warn(err), + options +); +---- + +== React Native + +In this example, we ask the user to turn on high accuracy location mode which enables network provider that uses Google Play services to improve location accuracy and location-based services: + +[source,js] +---- +import * as Location from 'expo-location'; + +Location.enableNetworkProviderAsync(); // Non-compliant +---- + +Prefer to ask the user to turn on lower-accuracy geolocation to conserve resources: + +[source,js] +---- +import * as Location from 'expo-location'; + +Location.requestPermissionsAsync(); // Compliant +---- + +== Resources + +=== Documentation + +- https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition[getCurrentPosition() method] - Mozilla Web Technology for Developers diff --git a/ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.json b/ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.json new file mode 100644 index 00000000..e32eae11 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC523/javascript/EC523.json @@ -0,0 +1,13 @@ +{ + "tags": [ + "sobriety", + "environment", + "ecocode", + "react-native", + "eco-design" + ], + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC8/EC8.json b/ecocode-rules-specifications/src/main/rules/EC8/EC8.json deleted file mode 100644 index 3d5e7565..00000000 --- a/ecocode-rules-specifications/src/main/rules/EC8/EC8.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "title": "Avoid using high accuracy geolocation in web applications.", - "type": "CODE_SMELL", - "code": { - "impacts": { - "MAINTAINABILITY": "MEDIUM" - }, - "attribute": "EFFICIENT" - }, - "status": "ready", - "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" - }, - "tags": [ - "ecocode", - "eco-design", - "performance" - ], - "defaultSeverity": "Major", - "compatibleLanguages": [ - "JAVASCRIPT", - "TYPESCRIPT" - ] -} diff --git a/ecocode-rules-specifications/src/main/rules/EC8/javascript/EC8.asciidoc b/ecocode-rules-specifications/src/main/rules/EC8/javascript/EC8.asciidoc deleted file mode 100644 index 71b5146f..00000000 --- a/ecocode-rules-specifications/src/main/rules/EC8/javascript/EC8.asciidoc +++ /dev/null @@ -1,42 +0,0 @@ -:!sectids: - -== Why is this an issue? - -High-precision geolocation typically requires more power from the device's GPS hardware. -By requesting less accurate geolocation, you can reduce the power consumption, leading to extended battery life, which is crucial for mobile devices. - -Obtaining highly accurate geolocation often involves more complex calculations and processing, which can increase CPU usage. -If the application or service does not critically require pinpoint accuracy, opting for a less accurate geolocation can help minimize the strain on the device's CPU. - -[source,js,data-diff-id="1",data-diff-type="noncompliant"] ----- -var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; // Non-compliant -navigator.geolocation.getCurrentPosition( - (pos) => console.log(pos), - (err) => console.warn(err), - options -); ----- - -In these examples, the enableHighAccuracy option is set to false (the default), indicating that the application prefers lower-accuracy geolocation to conserve resources: - -[source,js,data-diff-id="1",data-diff-type="compliant"] ----- -navigator.geolocation.getCurrentPosition((pos) => console.log(pos)); // Compliant by default ----- - -[source,js,data-diff-id="1",data-diff-type="compliant"] ----- -var options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 }; // Compliant -navigator.geolocation.getCurrentPosition( - (pos) => console.log(pos), - (err) => console.warn(err), - options -); ----- - -== Resources - -=== Documentation - -- https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition[Mozilla Web Technology for Developers] - getCurrentPosition() method From 68a0e612d8c115056e69ecdea79412c426f7eaa1 Mon Sep 17 00:00:00 2001 From: Benjamin DELAHAIS Date: Thu, 30 May 2024 15:11:09 +0200 Subject: [PATCH 15/31] [Python #26][EC1337] Avoid unlimited cache --- .../src/main/rules/EC1337/python/EC1337.asciidoc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc b/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc index e986428d..dd4ebc3f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc @@ -1,4 +1,11 @@ -The cache is unlimited which is suspect. An unlimited cache can take a lot of RAM / Disk space. +Python doesn't have TTL on cache with default libraries. + +With functools library @cache acts the same way than @lru_cache(maxsize=None). +@lru_cache(maxsize=None) create an unlimited cache and must be a source of memory leak, an unlimited cache can take a lot of RAM / Disk space, resulting to a big energy consumption. + +By default, lru_cache set the size of cache to 128 entries. + +In this case, the cache is unlimited which is suspect. ## Noncompliant Code Examples From 032f4d678bf029388f731574e2d98a7977a910f7 Mon Sep 17 00:00:00 2001 From: E000391 Date: Thu, 30 May 2024 15:23:04 +0200 Subject: [PATCH 16/31] update RULES.md --- RULES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RULES.md b/RULES.md index 05310c4b..6a66a5dd 100644 --- a/RULES.md +++ b/RULES.md @@ -75,6 +75,7 @@ Some are applicable for different technologies. | | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | πŸš€ | | | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | | | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC1369 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | ## Rules to be reworked / measured / clarified From 8f03608bd19616416020172b3121ee36eb35eabc Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 30 May 2024 21:17:08 +0200 Subject: [PATCH 17/31] changelog updates --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e3729a7..9bb16b80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port +- [#293](https://github.com/green-code-initiative/ecoCode/issues/293) [EC513] Swift port +- [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port - [#306](https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 - [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript - [#321](https://github.com/green-code-initiative/ecoCode/pull/321) Add rule EC522 for javascript (avoid brightness override) @@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#318](https://github.com/green-code-initiative/ecoCode/issues/318) Implement the rule EC523 for React Native ### Deleted + - [#318](https://github.com/green-code-initiative/ecoCode/issues/318) Merge the rule EC8 with EC523 ## [1.5.4] - 2024-05-24 @@ -52,7 +54,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Swift rules cleanup and updates (removed duplicated rules, added [EC602]) -- [#293](https://github.com/green-code-initiative/ecoCode/issues/293) [EC513] Swift port - [C# #18](https://github.com/green-code-initiative/ecoCode-csharp/issues/18) [EC81] [C#] Specify struct layout - [C# #285](https://github.com/green-code-initiative/ecoCode/pull/285) [EC82] [C#] Variable can be made constant - [C# #286](https://github.com/green-code-initiative/ecoCode/issues/286) [EC83] [C#] Replace Enum ToString() with nameof From 6daf90911e24752747c96235a51a7be9f6ee0515 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sun, 2 Jun 2024 23:21:36 +0200 Subject: [PATCH 18/31] [ISSUE 26][PYTHON] update RULES.md : Avoid using function cache without limit --- RULES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RULES.md b/RULES.md index 9c4a2257..b401c743 100644 --- a/RULES.md +++ b/RULES.md @@ -65,6 +65,7 @@ Some are applicable for different technologies. | EC86 | GC.Collect should not be called | In most cases, the cost of calling GC.Collect far outweighs the benefits | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | +| EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | 🚧 | ❓ | ❓ | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | From b917c9726e67ca24e29afa2351f6e566266e2160 Mon Sep 17 00:00:00 2001 From: Benjamin DELAHAIS Date: Mon, 3 Jun 2024 17:43:51 +0200 Subject: [PATCH 19/31] [Python #26][EC89] Change EC1337 to EC89 --- CHANGELOG.md | 2 +- .../src/main/rules/{EC1337/EC1337.json => EC89/EC89.json} | 0 .../python/EC1337.asciidoc => EC89/python/EC89.asciidoc} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename ecocode-rules-specifications/src/main/rules/{EC1337/EC1337.json => EC89/EC89.json} (100%) rename ecocode-rules-specifications/src/main/rules/{EC1337/python/EC1337.asciidoc => EC89/python/EC89.asciidoc} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12d8bf76..678b9703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -[Python #26](https://github.com/green-code-initiative/ecoCode-python/issues/26) [EC1337] [Python] Avoid unlimited cache +[Python #26](https://github.com/green-code-initiative/ecoCode-python/issues/26) [EC89] [Python] Avoid unlimited cache ### Changed diff --git a/ecocode-rules-specifications/src/main/rules/EC1337/EC1337.json b/ecocode-rules-specifications/src/main/rules/EC89/EC89.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC1337/EC1337.json rename to ecocode-rules-specifications/src/main/rules/EC89/EC89.json diff --git a/ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc b/ecocode-rules-specifications/src/main/rules/EC89/python/EC89.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC1337/python/EC1337.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC89/python/EC89.asciidoc From 37ccff78093621b246b00eeea0bdd4cfd97ebc25 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 3 Jun 2024 18:50:50 +0200 Subject: [PATCH 20/31] typo on CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d49de33..d432f106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#306](https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 - [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript - [#321](https://github.com/green-code-initiative/ecoCode/pull/321) Add rule EC522 for javascript (avoid brightness override) - [Python #26](https://github.com/green-code-initiative/ecoCode-python/issues/26) [EC89] [Python] Avoid unlimited cache +- [Python #26](https://github.com/green-code-initiative/ecoCode-python/issues/26) [EC89] [Python] Avoid unlimited cache ### Changed From 40733944ef85506320f53829643de9566a0bf386 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 3 Jun 2024 20:23:56 +0200 Subject: [PATCH 21/31] prepare 1.6.0 : update CHANGELOG --- CHANGELOG.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d432f106..4e7dead2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +### Changed + +### Deleted + +## [1.6.0] - 2024-06-03 + +### Added + - [#293](https://github.com/green-code-initiative/ecoCode/issues/293) [EC513] Swift port - [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port - [#306](https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 @@ -310,7 +318,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Comparison List -[unreleased](https://github.com/green-code-initiative/ecoCode/compare/1.5.4...HEAD) +[unreleased](https://github.com/green-code-initiative/ecoCode/compare/1.6.0...HEAD) +[1.6.0](https://github.com/green-code-initiative/ecoCode/compare/1.5.4...1.6.0) [1.5.4](https://github.com/green-code-initiative/ecoCode/compare/1.5.3...1.5.4) [1.5.3](https://github.com/green-code-initiative/ecoCode/compare/1.5.2...1.5.3) [1.5.2](https://github.com/green-code-initiative/ecoCode/compare/1.5.1...1.5.2) From a903a5c065505ceeca3a8922e2cdf2ad1485811e Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 3 Jun 2024 20:25:29 +0200 Subject: [PATCH 22/31] prepare 1.6.0 : update pom.xml --- ecocode-rules-specifications/pom.xml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index e01aef47..85d50850 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -5,7 +5,7 @@ io.ecocode ecocode-parent - 1.5.5-SNAPSHOT + 1.6.0-SNAPSHOT ecocode-rules-specifications diff --git a/pom.xml b/pom.xml index 0570acbd..5d7030be 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ io.ecocode ecocode-parent - 1.5.5-SNAPSHOT + 1.6.0-SNAPSHOT pom ecoCode Sonar Plugins Project From 6da471b46764318a84aeec7263b609cad29271d8 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 3 Jun 2024 20:25:47 +0200 Subject: [PATCH 23/31] [maven-release-plugin] prepare release 1.6.0 --- ecocode-rules-specifications/pom.xml | 2 +- pom.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 85d50850..520feb77 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -5,7 +5,7 @@ io.ecocode ecocode-parent - 1.6.0-SNAPSHOT + 1.6.0 ecocode-rules-specifications diff --git a/pom.xml b/pom.xml index 5d7030be..caf2b5df 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ io.ecocode ecocode-parent - 1.6.0-SNAPSHOT + 1.6.0 pom ecoCode Sonar Plugins Project @@ -86,7 +86,7 @@ scm:git:https://github.com/green-code-initiative/ecocode scm:git:https://github.com/green-code-initiative/ecocode https://github.com/green-code-initiative/ecocode - HEAD + 1.6.0 GitHub From 97ff3d789b48b01a25f95969eee65fa2a4d052fd Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 3 Jun 2024 20:25:48 +0200 Subject: [PATCH 24/31] [maven-release-plugin] prepare for next development iteration --- ecocode-rules-specifications/pom.xml | 2 +- pom.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 520feb77..2dd5f680 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -5,7 +5,7 @@ io.ecocode ecocode-parent - 1.6.0 + 1.6.1-SNAPSHOT ecocode-rules-specifications diff --git a/pom.xml b/pom.xml index caf2b5df..8d4e0441 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ io.ecocode ecocode-parent - 1.6.0 + 1.6.1-SNAPSHOT pom ecoCode Sonar Plugins Project @@ -86,7 +86,7 @@ scm:git:https://github.com/green-code-initiative/ecocode scm:git:https://github.com/green-code-initiative/ecocode https://github.com/green-code-initiative/ecocode - 1.6.0 + HEAD GitHub From 72e4e34f19755dd933d32719ea38c0efe9a07319 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 3 Jun 2024 20:49:59 +0200 Subject: [PATCH 25/31] type in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e7dead2..268a4bc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [#293](https://github.com/green-code-initiative/ecoCode/issues/293) [EC513] Swift port -- [#310] (https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port +- [#310](https://github.com/green-code-initiative/ecoCode/issues/310) EC515 Swift port - [#306](https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 - [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript - [#321](https://github.com/green-code-initiative/ecoCode/pull/321) Add rule EC522 for javascript (avoid brightness override) From d20071d0c7321b2792cf9b4d42eeff3fc15c08d8 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Wed, 5 Jun 2024 23:25:29 +0200 Subject: [PATCH 26/31] [ISSUE 26][PYTHON] update RULES.md : EC89 added for Python --- RULES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RULES.md b/RULES.md index b401c743..16aa9f99 100644 --- a/RULES.md +++ b/RULES.md @@ -65,7 +65,7 @@ Some are applicable for different technologies. | EC86 | GC.Collect should not be called | In most cases, the cost of calling GC.Collect far outweighs the benefits | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | -| EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | 🚧 | ❓ | ❓ | ❓ | +| EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | From 6153f82cf8c335f75dc57404197945a7d2322816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20Lallou=C3=A9?= Date: Fri, 7 Jun 2024 09:35:21 +0200 Subject: [PATCH 27/31] Move OBS naming to Orange Business (#340) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fae772b8..8ef25a0c 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ Here we honor some no-longer-active core team members who have made valuable con They have contributed to the success of ecoCode : - [Davidson Consulting](https://www.davidson.fr/) -- [Orange Business Services](https://www.orange-business.com/) +- [Orange Business](https://www.orange-business.com/) - [Snapp'](https://www.snapp.fr/) - [UniversitΓ© de Pau et des Pays de l'Adour (UPPA)](https://www.univ-pau.fr/) - [Solocal](https://www.solocal.com/) / [PagesJaunes.fr](https://www.pagesjaunes.fr/) From 98aac72b1ffde9d31845e44e0bee5f891701ede7 Mon Sep 17 00:00:00 2001 From: Vianney de Bellabre Date: Sat, 8 Jun 2024 12:57:34 +0200 Subject: [PATCH 28/31] [EC93] [C#] Return Task directly (#339) --- CHANGELOG.md | 2 + RULES.md | 1 + .../src/main/rules/EC93/EC93.json | 15 +++++++ .../src/main/rules/EC93/csharp/EC93.asciidoc | 41 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC93/EC93.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index 268a4bc9..a2635e29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [C# #66](https://github.com/green-code-initiative/ecoCode-csharp/pull/66) [EC93] [C#] Return Task directly + ### Changed ### Deleted diff --git a/RULES.md b/RULES.md index 16aa9f99..84ea2788 100644 --- a/RULES.md +++ b/RULES.md @@ -66,6 +66,7 @@ Some are applicable for different technologies. | EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | +| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | diff --git a/ecocode-rules-specifications/src/main/rules/EC93/EC93.json b/ecocode-rules-specifications/src/main/rules/EC93/EC93.json new file mode 100644 index 00000000..dd3cbc1d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC93/EC93.json @@ -0,0 +1,15 @@ +{ + "title": "Return Task directly", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc b/ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc new file mode 100644 index 00000000..07237306 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC93/csharp/EC93.asciidoc @@ -0,0 +1,41 @@ +:!sectids: + +Return Task directly. + +## Why is this an issue ? + +Asynchronous methods that contain a single awaited statement can be optimized by removing the `async` modifier and returning the awaited `Task` directly. Doing so reduces the overhead of the state machine generated for the asynchronous method, leading to more efficient code and improving the resource efficiency of the application. + +### When can it be ignored ? + +When this rule is applied, exception handling is deferred to the main caller instead, which may or may not be desirable. The decision should be done with consideration to the context in which the method is used. + +## Non-compliant examples + +[source, cs] +---- +public static async Task Test1() +{ + await Task.Delay(1000); // Non-compliant, return the Task directly. +} + +public static async Task Test2() +{ + await MyAsyncMethod(); // Non-compliant, exceptions within MyAsyncMethod are handled by the method itself. +} +---- + +## Compliant examples + +[source, cs] +---- +public static Task Test1() +{ + return Task.Delay(1000); // Compliant +} + +public static Task Test2() +{ + return MyAsyncMethod(); // Compliant, exceptions within MyAsyncMethod are handled by the caller of Test2. +} +---- From e734aab04b844c611b5bdb9ffcf02dcf7dbc80a0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-J99B7TH\\Gilbert" Date: Thu, 30 May 2024 14:36:20 +0200 Subject: [PATCH 29/31] Add avoid-keep-awake asciidoc and json --- .../rules/EC505/javascript/EC505.asciidoc | 26 +++++++++++++++++++ .../main/rules/EC505/javascript/EC505.json | 13 ++++++++++ 2 files changed, 39 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.json diff --git a/ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.asciidoc b/ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.asciidoc new file mode 100644 index 00000000..05f67795 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.asciidoc @@ -0,0 +1,26 @@ +:!sectids: + +== Why is this an issue? + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. + +== Example of non compliant code + +```js +export default function KeepAwakeExample() { + useKeepAwake(); // Non compliant + return ( + + This screen will never sleep! + + ); +} +``` + +```js +_activate = () => { + activateKeepAwake(); // Non-compliant + alert('Activated!'); + }; +``` \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.json b/ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.json new file mode 100644 index 00000000..936cf1cc --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC505/javascript/EC505.json @@ -0,0 +1,13 @@ +{ + "tags": [ + "sobriety", + "environment", + "ecocode", + "react-native", + "eco-design" + ], + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] + } \ No newline at end of file From 4d28f85d73256e8335674be31674b5a9c7f62c1c Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 13 Jun 2024 21:53:46 +0200 Subject: [PATCH 30/31] array format correction of RULES.md --- RULES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RULES.md b/RULES.md index e0ebb5c7..5f3b8b1a 100644 --- a/RULES.md +++ b/RULES.md @@ -66,7 +66,7 @@ Some are applicable for different technologies. | EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | -| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | +| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | From 93f94a792552b731e8d1ffc297c94de89c8c29a4 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 13 Jun 2024 21:59:59 +0200 Subject: [PATCH 31/31] [EC94] update rule referentiel with real rule ID + CHANGELOG.md + RULES.md --- CHANGELOG.md | 1 + RULES.md | 2 +- .../src/main/rules/{EC1369/EC1369.json => EC94/EC94.json} | 0 .../{EC1369/java/EC1369.asciidoc => EC94/java/EC94.asciidoc} | 0 4 files changed, 2 insertions(+), 1 deletion(-) rename ecocode-rules-specifications/src/main/rules/{EC1369/EC1369.json => EC94/EC94.json} (100%) rename ecocode-rules-specifications/src/main/rules/{EC1369/java/EC1369.asciidoc => EC94/java/EC94.asciidoc} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2635e29..1198bd0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [C# #66](https://github.com/green-code-initiative/ecoCode-csharp/pull/66) [EC93] [C#] Return Task directly +- [JAVA #323](https://github.com/green-code-initiative/ecoCode/pull/323) [EC94] [Java] Use orElseGet instead of orElse ### Changed diff --git a/RULES.md b/RULES.md index 5f3b8b1a..65a8ebc1 100644 --- a/RULES.md +++ b/RULES.md @@ -67,6 +67,7 @@ Some are applicable for different technologies. | EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | | EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | | ❓ | ❓ | ❓ | βœ… | ❓ | ❓ | ❓ | | EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | βœ… | ❓ | +| EC94 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | | EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | πŸš€ | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | 🚫 | 🚫 | | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | πŸš€ | @@ -76,7 +77,6 @@ Some are applicable for different technologies. | | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | πŸš€ | | | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | | | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC1369 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | ## Rules to be reworked / measured / clarified diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json b/ecocode-rules-specifications/src/main/rules/EC94/EC94.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC1369/EC1369.json rename to ecocode-rules-specifications/src/main/rules/EC94/EC94.json diff --git a/ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc b/ecocode-rules-specifications/src/main/rules/EC94/java/EC94.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC1369/java/EC1369.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC94/java/EC94.asciidoc