Hidden Features and
Lesser-Known Tricks in Popular Languages
Every language contains features that most developers never discover until they read someone else's code and think: "I didn't know it could do that." Here are the best of them — in Python, JavaScript, Java, and C#.
The gap between a developer who knows a language's basics and one who knows it deeply is often not about algorithms or data structures — it is about the features built into the language that eliminate entire categories of boilerplate, express intent more precisely, or solve common problems in ways that beginners reach for a dozen lines to accomplish. These features are documented; they are just rarely taught in introductory materials.
Python
The walrus operator (:=)
Introduced in Python 3.8 (PEP 572), the walrus operator assigns a value to a variable as part of an expression — allowing assignment and use in the same statement. Its most useful application is avoiding repeated function calls in conditions:[1]
# Without walrus: calls len() twice
if len(data) > 10:
print(f"Long list: {len(data)} items")
# With walrus: calls len() once
if (n := len(data)) > 10:
print(f"Long list: {n} items")
Particularly useful in while loops that read chunks from a stream:
while chunk := file.read(8192):
process(chunk)
Dictionary merge operators (| and |=)
Since Python 3.9, dictionaries support merge (|) and update-in-place (|=) operators — cleaner than {**a, **b} unpacking:
defaults = {"timeout": 30, "retries": 3}
overrides = {"retries": 5, "verbose": True}
config = defaults | overrides
# {"timeout": 30, "retries": 5, "verbose": True}
functools.cache — one-line memoisation
@functools.cache (Python 3.9+) memoises a function's return values automatically, caching results keyed on arguments. For recursive functions that recompute the same values repeatedly (Fibonacci, dynamic programming), it converts exponential time complexity to linear with a single decorator:
from functools import cache
@cache
def fib(n):
return n if n < 2 else fib(n-1) + fib(n-2)
Structural pattern matching (match/case)
Python 3.10 added structural pattern matching — significantly more powerful than a switch statement. It matches on value, type, structure, and destructures simultaneously:
match command:
case {"action": "move", "direction": d}:
move(d)
case {"action": "attack", "weapon": w}:
attack(w)
case _:
raise ValueError(f"Unknown: {command}")
JavaScript
The Proxy object
JavaScript's Proxy wraps an object and intercepts fundamental operations — property access, assignment, function calls — enabling reactive programming patterns, validation, and logging without modifying the target object:[2]
const handler = {
set(target, key, value) {
if (typeof value !== 'number') throw new TypeError('Numbers only');
target[key] = value;
return true;
}
};
const stats = new Proxy({}, handler);
stats.score = 42; // OK
stats.name = "Ali"; // Throws TypeError
Nullish coalescing and optional chaining
The ?? operator (nullish coalescing) returns the right side only when the left is null or undefined — unlike || which also triggers on 0, "", and false. Combined with optional chaining (?.), it allows deeply nested property access without defensive null checks:
const city = user?.address?.city ?? "Unknown";
// Safely reads nested props; returns "Unknown" only if null/undefined
Labeled statements for breaking nested loops
JavaScript allows labeling loops, enabling break or continue to target an outer loop — a feature almost never taught but genuinely useful:
outer: for (const row of matrix) {
for (const cell of row) {
if (cell === target) break outer; // exits both loops
}
}
Java
Sealed classes (Java 17+)
Sealed classes restrict which classes can extend them, enabling exhaustive pattern matching. Combined with records and pattern matching in switch expressions, they enable algebraic data types similar to Rust's enums or Haskell's sum types:[3]
sealed interface Shape permits Circle, Rectangle, Triangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double w, double h) implements Shape {}
double area(Shape s) {
return switch (s) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.w() * r.h();
case Triangle t -> /* ... */;
}; // compiler enforces all cases are covered
}
Virtual threads (Java 21)
Project Loom's virtual threads, released in Java 21, allow millions of concurrent threads without the overhead of OS threads. Code written in blocking style (no callbacks, no reactive frameworks) runs at async-level concurrency. Existing blocking I/O code benefits automatically when run on virtual threads.
C#
LINQ deferred execution and lazy evaluation
LINQ queries in C# are lazily evaluated — the query is not executed until it is iterated. This allows building complex query pipelines that execute only once, against only the elements needed:[4]
var expensive = data
.Where(x => x.IsActive)
.OrderBy(x => x.Name)
.Take(10);
// Nothing executed yet — query is a description, not a result
foreach (var item in expensive) { /* executes here */ }
Pattern matching with is and switch expressions
C# 8+ pattern matching allows concise type-testing and destructuring in conditions and switch expressions, eliminating verbose as/null check patterns:
string Describe(object obj) => obj switch {
int n when n < 0 => "negative integer",
int n => $"positive integer {n}",
string s => $"string of length {s.Length}",
null => "null",
_ => "something else"
};
"A language that doesn't affect the way you think about programming is not worth knowing. The hidden features are where the thinking happens."
— Alan J. Perlis, Epigrams on Programming, ACM SIGPLAN Notices, 1982
References
- van Rossum, G., et al. (2018). PEP 572 — Assignment Expressions. Python Software Foundation. peps.python.org ↩
- MDN Web Docs. (2024). Proxy — JavaScript. Mozilla. developer.mozilla.org ↩
- Oracle. (2023). JEP 409: Sealed Classes. OpenJDK. openjdk.org ↩
- Microsoft. (2024). Language Integrated Query (LINQ). Microsoft Docs. learn.microsoft.com ↩