# Constant

> Constants Layer

- Skill: `harshamendu/constant` (Agent Skill, multi-file: 10 files)
- Install (CLI): `npx skillmds@latest add harshamendu/constant`
- Raw SKILL.md: https://api.skillmd.com/api/skills/harshamendu/constant/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: Harshamendu (https://skillmd.com/u/harshamendu)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/harshamendu/constant

---

# Constants Layer

> **📝 Note:** This guide uses generic placeholder names to be reusable across any Spring Boot microservice.
> Replace with your actual implementation:
> - `{YourService}` → Your service name (e.g., `OrderService`, `PaymentService`)
> - `BusinessService` → Your core service (e.g., `OrderService`, `UserService`)
> - `DataService` → Your data processing service (e.g., `PaymentService`, `InventoryService`)
> - `IntegrationService` → Your external integration (e.g., `PaymentGatewayService`)
> - `{RequestType}` → Your request DTO (e.g., `CreateOrderRequest`)
> - `{ResponseType}` → Your response DTO (e.g., `OrderResponse`)


## Purpose

The constants layer provides centralized, immutable constants and type-safe enumerations used throughout the application. It ensures consistency, prevents magic strings/numbers, and provides compile-time validation.

## Core Principles

- **Immutability**: All constants are final and cannot be modified
- **Type Safety**: Enums over strings for finite sets of values
- **Centralization**: Single source of truth for application-wide values
- **Semantic Naming**: Clear, descriptive names following Java conventions
- **Static Import Friendly**: Designed for clean static imports

## Package Structure

```
constant/
├── Constants.java                  # Application-wide constants
└── NewRelicErrorMessages.java      # Expected error messages enum
```

## Documentation

### 📚 Guides

Comprehensive guides covering constant layer development:

- **[Design Patterns](guides/patterns.md)** - Constant Interface, Type-Safe Enum, Static Import patterns
- **[Best Practices](guides/best-practices.md)** - Immutability, naming conventions, domain grouping
- **[Testing Strategies](guides/testing.md)** - Unit tests, immutability tests, enum tests
- **[Anti-Patterns](guides/anti-patterns.md)** - Common mistakes and how to avoid them

### 💡 Examples

Compilable Java examples demonstrating key concepts:

- **[ConstantsExample.java](examples/ConstantsExample.java)** - Immutable constant class pattern
- **[NewRelicErrorMessagesExample.java](examples/NewRelicErrorMessagesExample.java)** - Type-safe enum with values
- **[StaticImportUsageExample.java](examples/StaticImportUsageExample.java)** - Static import best practices
- **[CollectionConstantsExample.java](examples/CollectionConstantsExample.java)** - Immutable collection constants

## Quick Reference

### Key Patterns

- **Constant Class**: Final class with `public static final` fields and private constructor
- **Type-Safe Enum**: Enum with encapsulated values and getter methods
- **Static Import**: Import constants directly for cleaner code
- **Immutable Collections**: Use `List.of()` and `Set.of()` for collection constants

### Naming Conventions

- **Constants**: `UPPER_SNAKE_CASE` (e.g., `BEARER_TOKEN_PREFIX`)
- **Enum Class**: `PascalCase` (e.g., `ExpectedErrorMessages`)
- **Enum Values**: `UPPER_SNAKE_CASE` (e.g., `INCORRECT_EMAIL_OR_PASSWORD`)
- **Enum Methods**: `camelCase` (e.g., `getErrorMessage()`)

### Usage Example

```java
import static com.example.microserviceorch.constant.Constants.*;

public class BusinessService {
    public String processToken(String authorization) {
        if (authorization.startsWith(BEARER_TOKEN_PREFIX)) {
            return authorization.substring(BEARER_TOKEN_PREFIX.length());
        }
        return authorization;
    }
}
```

## Code Formatting

All Java code is formatted using **Spotless** with Google Java Format (AOSP style):

```bash
./gradlew spotlessApply
```

## Related Documentation

- [Architecture Overview](../../ARCHITECTURE.md) - Overall application architecture
- [Model Layer](../model/skill.md) - Data transfer objects and domain models
- [Service Layer](../service/skill.md) - Business logic and service orchestration

---

For detailed guidance, refer to the guides above. For working examples, see the examples directory.

