Detect and report errors as early as possible with detailed context to prevent silent failures and aid debugging. Validate inputs, states, and implementation completeness with appropriate exceptions that include relevant information.
Detect and report errors as early as possible with detailed context to prevent silent failures and aid debugging. Validate inputs, states, and implementation completeness with appropriate exceptions that include relevant information.
Key practices:
// AVOID: Silently ignoring invalid parameters
if (gamma > 0)
this.gamma = gamma;
// Negative gamma silently ignored
// PREFER: Explicit validation with informative errors
Preconditions.checkArgument(gamma > 0,
"Gamma must be positive, got %s", gamma);
this.gamma = gamma;
// AVOID: Generic errors without context
if (shape[dimension] != 1) {
throw new ND4JIllegalStateException("Can squeeze only dimensions of size 1.");
}
// PREFER: Detailed exceptions with context
Preconditions.checkState(shape[dimension] == 1,
"Can squeeze only dimensions of size 1. Dimension %s has size %s. Shape: %s",
dimension, shape[dimension], Arrays.toString(shape));
// AVOID: Always constructing error messages
Preconditions.checkState(input.shape().length == 3,
"3D input expected to RNN layer expected, got " + input.shape().length);
// PREFER: Lazy message construction with format strings
Preconditions.checkState(input.rank() == 3,
"3D input expected to RNN layer, got %s", input.rank());
// AVOID: Unimplemented code with TODOs
if (modelParamsSupplier != null) {
val params = modelParamsSupplier.get();
if (params != null) {
// TODO: We should propagate params across the workers
}
}
// PREFER: Throw exception for unimplemented functionality
if (modelParamsSupplier != null) {
val params = modelParamsSupplier.get();
if (params != null) {
throw new UnsupportedOperationException(
"Parameter propagation to workers not yet implemented");
}
}
Enter the URL of a public GitHub repository