Always validate bounds, nil pointers, and other potentially unsafe conditions before accessing values to prevent runtime panics and undefined behavior. This includes checking array/slice bounds, validating pointer references, and ensuring data structures are in expected states before use.

Key validation patterns:

Example from bounds checking:

size := ts.Size()
for i := start; i <= stop && i >= 0 && i < size; i++ {
    if tok := ts.Get(i); tok != nil && tok.GetTokenType() != antlr.TokenEOF {
        b.WriteString(tok.GetText())
    }
}

Example from pointer validation:

func ValidatePointer(dest any, caller string) error {
    rv := reflect.ValueOf(dest)
    if rv.Kind() != reflect.Pointer || rv.IsNil() {
        return WrapCacheableErrors(reflect.TypeOf(dest), caller)
    }
    return nil
}

This proactive validation approach prevents common sources of runtime errors and makes code more robust by catching potential issues before they cause panics or undefined behavior.