Hack is a programming language for HHVM that interoperates seamlessly with PHP. It provides static typing with type inference, generics, nullable types, and other features while maintaining compatibility with existing PHP code.
Hack Anti-Patterns Overview
Ignoring Static Typing
mixed
types excessively or falling back to dynamic type checking with functions like is_array()
. Instead, leverage Hack’s type system to catch errors at compile time rather than runtime.Not Using Collections
Vector
, Map
, Set
, etc.) over PHP arrays. Collections provide type safety, better performance in many cases, and useful higher-order functions for transformations.Excessive Use of Nullable Types
?T
) are useful, excessive use can lead to null-checking chains and defensive programming. Consider using default values, the Option type pattern, or restructuring your code to minimize nullable types.Not Using Shapes and Structural Typing
Ignoring Async
Not Using XHP for HTML Generation
Not Using Type Aliases and Newtype
type
aliases and newtype
to create more specific types for your domain concepts. This improves type safety, documentation, and prevents accidental misuse of values.Not Using Enums
Not Using Generics
Not Using Immutable Data Structures
ImmVector
, ImmMap
, ImmSet
) over mutable ones when appropriate. Immutable data structures prevent unexpected modifications and make your code easier to reason about, especially in concurrent contexts.Not Using Traits for Code Reuse
Not Using Proper Error Handling
Not Using Namespaces
Not Using Dependency Injection
Not Using Interfaces
Not Using Type Refinement
Not Using Attributes
Not Writing Tests