Skip to content

Commit

Permalink
Day093
Browse files Browse the repository at this point in the history
  • Loading branch information
hbelmiro committed Aug 20, 2021
1 parent f7dd7eb commit d923bf4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3516,4 +3516,42 @@ public class Day092 {
System.out.println(sum);
}
}
----

== Day 93 - Never return `null` in a method that has a `Collection` as return type.
[source,java]
----
package com.thegreatapi.ahundreddaysofjava.day093;
import java.time.Instant;
import java.time.temporal.ChronoField;
import java.util.List;
public class Day093 {
public static void main(String[] args) {
List<String> myList = getList();
// If myList is null, a NullPointerException will be thrown
for (String s : myList) {
System.out.println(s);
}
}
private static List<String> getList() {
if (someCondition()) {
return List.of("a", "b", "c");
} else {
// Instead of returning null
// return null;
// Return an empty list, so the caller don't need to check if the returned list is not null
return List.of();
}
}
private static boolean someCondition() {
return Instant.now().get(ChronoField.MILLI_OF_SECOND) % 2 == 0;
}
}
----
19 changes: 19 additions & 0 deletions days/day093/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>days</artifactId>
<groupId>com.thegreatapi.100daysofjava</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>day093</artifactId>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.thegreatapi.ahundreddaysofjava.day093;

import java.time.Instant;
import java.time.temporal.ChronoField;
import java.util.List;

public class Day093 {

public static void main(String[] args) {
List<String> myList = getList();

// If myList is null, a NullPointerException will be thrown
for (String s : myList) {
System.out.println(s);
}
}

private static List<String> getList() {
if (someCondition()) {
return List.of("a", "b", "c");
} else {
// Instead of returning null
// return null;

// Return an empty list, so the caller don't need to check if the returned list is not null
return List.of();
}
}

private static boolean someCondition() {
return Instant.now().get(ChronoField.MILLI_OF_SECOND) % 2 == 0;
}
}
1 change: 1 addition & 0 deletions days/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<module>day090</module>
<module>day091</module>
<module>day092</module>
<module>day093</module>
</modules>

</project>

0 comments on commit d923bf4

Please sign in to comment.