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

[CHALLENGE24][Samarium] add specifications for rules EC82, EC205, EC1245 #334

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 50 additions & 49 deletions RULES.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.ecocode</groupId>
<artifactId>ecocode-parent</artifactId>
<version>1.5.5-SNAPSHOT</version>
<version>1.5.7-SNAPSHOT</version>
</parent>

<artifactId>ecocode-rules-specifications</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC1245/EC1245.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Avoid Energy Consuming Methods",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "15min"
},
"tags": [
"eco-design",
"ecocode"
],
"defaultSeverity": "Major"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
= Avoid Energy Consuming Methods

This rule aims to identify and flag methods in Java code that have high energy consumption due to the use of computationally expensive operations. The goal is to encourage developers to write more energy-efficient code by limiting the use of costly operations within a single method.

The rule works by assigning a "cost" to specific expensive operations commonly recognized for their high computational demands. These operations include:

- Reflection operations (e.g., `Class.forName()`, `Method.invoke()`)
- Synchronization operations (e.g., `synchronized` blocks or methods)
- I/O operations (e.g., `FileInputStream`, `FileOutputStream`, `BufferedReader`, etc.)
- SQL operations (e.g., `Statement.execute()`, `ResultSet.next()`)
- String operations (e.g., `String.replaceAll()`, `String.split()`, etc.)
- Thread operations (e.g., `Thread.sleep()`, `Thread.join()`, etc.)
- XML parsing (e.g., `DocumentBuilder.parse()`)

For each occurrence of these operations within a method, a predefined number of points is assigned. If the total points for a method exceed a certain threshold (e.g., 5 points), the method is considered to be energy-inefficient. A warning is then issued to indicate that the method may have high energy consumption and suggest the developer to refactor or optimize the method.

== Noncompliant Code Example

```java
public class Example {
public void exampleMethod() {
String data = "example";
data = data.replaceAll("e", "a"); // 1 point
FileInputStream fis = new FileInputStream("file.txt"); // 1 point
FileInputStream fis2 = new FileInputStream("file2.txt"); // 1 point
FileInputStream fis3 = new FileInputStream("file3.txt"); // 1 point
FileInputStream fis4 = new FileInputStream("file4.txt"); // 1 point
}
}
```

== Compliant Code Example

```java
public class Example {
public void exampleMethod() { // Method complexity and energy consumption reduced
String data = "example";
data = data.replaceAll("e", "a"); // 1 point
FileInputStream fis = new FileInputStream("file.txt"); // 1 point
}
}
```

In the compliant solution, expensive operations are minimized or replaced with more efficient alternatives, ensuring the method remains within an acceptable energy consumption range.

== Benefits

- Improved Code Efficiency: Identifying and reducing the use of expensive operations leads to more efficient code execution.
- Enhanced Performance: Lowering the computational demands of methods improves overall application performance.
- Reduced Energy Consumption: Efficient methods consume less energy, contributing to greener and more sustainable software.
- Better Resource Utilization: Optimized methods make better use of system resources, improving responsiveness and scalability.

This rule helps developers identify potential performance bottlenecks and encourages the adoption of more efficient coding practices to reduce the energy footprint of their applications.
14 changes: 14 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC205/EC205.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Force usage of FetchType LAZY for collections on JPA entities",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"ecocode"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
= Force Usage of FetchType LAZY for Collections on JPA Entities

This rule enforces the use of `FetchType.LAZY` for collections in JPA entities to minimize energy consumption and improve application performance. The rule targets the following JPA annotations:
- `@OneToMany`
- `@ManyToMany`
- `@OneToOne`
- `@ManyToOne`

JPA provides two types of fetch strategies for associations: `FetchType.EAGER` and `FetchType.LAZY`.
- `FetchType.EAGER` loads the related entities immediately, which can result in significant performance overhead and increased energy consumption, especially when loading large collections or deeply nested entity graphs.
- `FetchType.LAZY` defers the loading of the related entities until they are accessed, reducing the initial load time, memory usage, and energy consumption.

== Noncompliant Code Example

```java
@Entity
public class Order {
@OneToMany(fetch = FetchType.EAGER) // Noncompliant
private List<OrderItem> orderItems;

@ManyToOne // Noncompliant, should specify fetch = FetchType.LAZY
private Customer customer;
}
```

== Compliant Code Example

```java
@Entity
public class Order {
@OneToMany(fetch = FetchType.LAZY) // Compliant
private List<OrderItem> orderItems;

@ManyToOne(fetch = FetchType.LAZY) // Compliant
private Customer customer;
}
```

In the compliant solution, the fetch attribute is explicitly set to FetchType.LAZY for both the @OneToMany and @ManyToOne associations. This ensures that related entities are loaded only when accessed, improving performance and reducing resource consumption.

== Benefits

- Reduced Initial Data Load: Decreasing memory usage and startup time by loading data only when needed.
- Optimized CPU and IO Usage: Preventing unnecessary data retrieval, thereby lowering CPU and IO usage.
- Improved Application Performance: Enhancing overall application performance and responsiveness.
- Minimized Energy Consumption: Avoiding the overhead of eagerly loading large amounts of data, thereby reducing energy consumption.

By ensuring that JPA associations use FetchType.LAZY, developers can create more efficient and scalable applications that are better suited for high-performance environments with lower energy footprints.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
= Make Non-Reassigned Variables Constant

Declaring variables as constants whenever possible can lead to several benefits, including improved code readability, better performance, and fewer bugs. Constants, being immutable, make it clear that the value will not change once initialized, which can help developers understand and reason about the code more easily.

Non-reassigned variables, if not declared as constants, can lead to misunderstandings about the potential mutability of the variable. By explicitly marking such variables with the `final` keyword, we indicate that their values are fixed after initialization, enabling potential compiler optimizations and making the code more robust.

== Noncompliant Code Example

```java
public class Example {
public void calculate() {
int value = 42; // Noncompliant
// ... some operations
System.out.println(value);
}
}
```

== Compliant Code Example

```java
public class Example {
public void calculate() {
final int value = 42; // Compliant
// ... some operations
System.out.println(value);
}
}
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.ecocode</groupId>
<artifactId>ecocode-parent</artifactId>
<version>1.5.5-SNAPSHOT</version>
<version>1.5.7-SNAPSHOT</version>
<packaging>pom</packaging>

<name>ecoCode Sonar Plugins Project</name>
Expand Down