Types

The goal of typed languages is to pull issues from run time to compile time.

Rules

  • Use types

  • Do not use String for non-textual values

  • Do not use Strings for dates - it is always formatted!

  • Avoid using boolean parameters - hard to read, "magic numbers"

  • Create exception types instead of error codes

Enums

  • Enums are static constants

  • You can static import enums to shorten the code

  • Enums are created threadsafe

  • Always use enums for a finit set of constants

  • Use enum to map information

  • Prefer enums to maps

  • Prefer enums to switches

  • Enums cannot have a parent class but can implement interfaces

Example: Bad: Implementation with collections and procedures

Example: Good: Information and mapping as enum

Type Smells

  • Do not pass parameters in a map

Example: Bad: Parameter map

Everything should be made as simple as possible, but not simpler. (Albert Einstein)

Last updated

Was this helpful?