You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"title": "Optional.orElseThrow() without supplier",
5
+
"category": "errors",
6
+
"difficulty": "beginner",
7
+
"jdkVersion": "10",
8
+
"oldLabel": "Java 8",
9
+
"modernLabel": "Java 10+",
10
+
"oldApproach": "get() or orElseThrow(supplier)",
11
+
"modernApproach": "orElseThrow()",
12
+
"oldCode": "// Risky: get() throws if empty, no clear intent\nString value = optional.get();\n\n// Verbose: supplier just for NoSuchElementException\nString value = optional\n .orElseThrow(NoSuchElementException::new);",
13
+
"modernCode": "// Clear intent: throws NoSuchElementException if empty\nString value = optional.orElseThrow();",
14
+
"summary": "Use Optional.orElseThrow() as a clearer, intent-revealing alternative to get().",
15
+
"explanation": "Optional.get() is widely considered a code smell because it hides the possibility of failure. The no-arg orElseThrow(), added in Java 10, does exactly the same thing but makes the intent explicit: the developer expects a value and wants an exception if absent.",
16
+
"whyModernWins": [
17
+
{ "icon": "📖", "title": "Self-documenting", "desc": "orElseThrow() clearly signals that absence is unexpected." },
18
+
{ "icon": "🔒", "title": "Avoids get()", "desc": "Static analysis tools flag get() as risky; orElseThrow() is idiomatic." },
19
+
{ "icon": "⚡", "title": "Less boilerplate", "desc": "No need to pass a supplier for the default NoSuchElementException." }
20
+
],
21
+
"support": {
22
+
"state": "available",
23
+
"description": "Available since JDK 10 (March 2018)."
"oldApproach": "new Random() / ThreadLocalRandom",
11
+
"modernApproach": "RandomGenerator factory",
12
+
"oldCode": "// Hard-coded to one algorithm\nRandom rng = new Random();\nint value = rng.nextInt(100);\n\n// Or thread-local, but still locked in\nint value = ThreadLocalRandom.current()\n .nextInt(100);",
13
+
"modernCode": "// Algorithm-agnostic via factory\nvar rng = RandomGenerator.of(\"L64X128MixRandom\");\nint value = rng.nextInt(100);\n\n// Or get a splittable generator\nvar rng = RandomGeneratorFactory\n .of(\"L64X128MixRandom\").create();",
14
+
"summary": "Use the RandomGenerator interface to choose random number algorithms by name without coupling to a specific class.",
15
+
"explanation": "JDK 17 introduced RandomGenerator as a common interface for all RNG implementations. Instead of hard-coding new Random() or ThreadLocalRandom, you can select algorithms by name via a factory, making it easy to swap between algorithms optimized for different use cases (speed, statistical quality, splittability).",
16
+
"whyModernWins": [
17
+
{ "icon": "🔧", "title": "Algorithm-agnostic", "desc": "Choose the best RNG algorithm by name without changing code structure." },
18
+
{ "icon": "⚡", "title": "Better algorithms", "desc": "Access to modern LXM generators with superior statistical properties." },
"summary": "Use Predicate.not() to negate method references cleanly instead of writing lambda wrappers.",
15
+
"explanation": "Before Java 11, negating a method reference required wrapping it in a lambda. Predicate.not() lets you negate any predicate directly, keeping the code readable and consistent with method reference style throughout the stream pipeline.",
16
+
"whyModernWins": [
17
+
{ "icon": "👁", "title": "Cleaner negation", "desc": "No need to wrap method references in lambdas just to negate them." },
18
+
{ "icon": "🔗", "title": "Composable", "desc": "Works with any Predicate, enabling clean predicate chains." },
"oldCode": "String text = \"one\\ntwo\\nthree\";\nString[] lines = text.split(\"\\n\");\nfor (String line : lines) {\n System.out.println(line);\n}",
13
+
"modernCode": "String text = \"one\\ntwo\\nthree\";\ntext.lines().forEach(System.out::println);",
14
+
"summary": "Use String.lines() to split text into a stream of lines without regex overhead.",
15
+
"explanation": "String.lines() returns a Stream<String> of lines split by \\n, \\r, or \\r\\n. It is lazier and more efficient than split(), avoids regex compilation, and integrates naturally with the Stream API for further processing.",
16
+
"whyModernWins": [
17
+
{ "icon": "⚡", "title": "Lazy streaming", "desc": "Lines are produced on demand, not all at once like split()." },
18
+
{ "icon": "🔧", "title": "Universal line endings", "desc": "Handles \\n, \\r, and \\r\\n automatically without regex." },
19
+
{ "icon": "🔗", "title": "Stream integration", "desc": "Returns a Stream for direct use with filter, map, collect." }
20
+
],
21
+
"support": {
22
+
"state": "available",
23
+
"description": "Available since JDK 11 (September 2018)."
0 commit comments