Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Green Java

Sandrine Banas
October 12, 2023
230

Green Java

Sandrine Banas

October 12, 2023
Tweet

Transcript

  1. String var1 = getValue(); return var1; return getValue(); Do not

    unnecessarily assign values to variables. It increases the use of RAM memory. A noter que la JVM optimise de toute façon ce code lors de la compilation de la version sale à la version green.
  2. libérer les ressources => try with ressources primitive vs Object

    => ex Long vs long A venir: les value objects
  3. public String concatenateStrings(String[] strings) { String result = ""; for

    (String string : strings) { result += string; } return result; }
  4. public String concatenateStrings(String[] strings) { String result = ""; for

    (String string : strings) { result += string; } return result; } public String concatenateStrings(String[] strings) { StringBuilder result = new StringBuilder(); for (String string : strings) { result.append(string); } return result.toString(); } Don't concatenate Strings in loop. Use StringBuilder instead. Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU.
  5. public voidfoo() { for (int i = 0; i <

    getMyValue(); i++) { System.out.println(i); } }
  6. public voidfoo() { for (int i = 0; i <

    getMyValue(); i++) { System.out.println(i); } } public voidfoo() { int myValue =getMyValue(); for (int i = 0; i < myValue; i++) { System.out.println(i); } } Do not call a function when declaring a for-type loop in order to avoid function calls each iterations. It saves CPU cycles.
  7. Eviter d’utiliser des Wrappers sur les primitifs Utiliser la bonne

    Collection pour son besoin JDBC < Spring Data JPA native queries < JPQL* Switch plutôt que if/else Threads virtuels *Tests faits sur une base oracle