Hacklang at Slack: A Better PHP

Slack launched in 2014 with a PHP 5 backend. Along with several other companies, we switched to HHVM in 2016 because it ran our PHP code faster. We stayed with HHVM because it offers an entirely new language: Hack (searchable as Hacklang). Hack makes our developers faster by improving productivity through better tooling. Hack began as a superset of PHP, retaining its best…

Overview

The article discusses Slack's migration from PHP to Hack, a language designed to enhance PHP's capabilities. It outlines the benefits of Hack, including static type checking, gradual typing, and improved productivity for developers.

What You'll Learn

1

How to leverage static type checking to catch bugs early

2

Why gradual typing can facilitate migration from PHP to Hack

3

How to use shapes to represent complex data structures effectively

4

When to implement async/await for better concurrency management

Prerequisites & Requirements

  • Understanding of PHP and its limitations
  • Familiarity with Hack and HHVM(optional)

Key Questions Answered

What are the benefits of migrating from PHP to Hack?
Migrating from PHP to Hack provides significant benefits such as improved productivity through better tooling, static type checking that catches bugs early, and features like shapes for complex data structures. These enhancements allow developers to code with more confidence and efficiency.
How does Hack's static type checking improve development speed?
Hack's static type checking allows developers to identify type-related errors as they type, rather than at runtime. This immediate feedback loop drastically reduces the time spent debugging and testing, enabling quicker iterations and more reliable code.
What is the role of shapes in Hack?
Shapes in Hack allow developers to define complex data structures with known keys and specific types, improving type safety and reducing bugs. They serve as a form of inline documentation, making code easier to read and understand.
How does async/await simplify concurrency in Hack?
Async/await in Hack allows developers to write code that can pause while waiting for I/O operations, enabling concurrent execution without the complexity of traditional multithreading. This leads to cleaner, more maintainable code while improving application responsiveness.

Technologies & Tools

Programming Language
Hack
Used as a superset of PHP to enhance type safety and developer productivity.
Runtime
Hhvm
Runs Hack code and provides performance improvements over traditional PHP.

Key Actionable Insights

1
Implement static type checking in your codebase to catch errors early.
By using Hack's static type checker, you can identify type-related bugs before running your code, which saves time and resources during development.
2
Adopt gradual typing to facilitate the migration process.
Starting with partial mode in Hack allows you to incrementally add types to your existing PHP code, making the transition smoother and less disruptive.
3
Utilize shapes for defining complex data structures.
Shapes provide a clear contract for data structures, improving code readability and reducing the likelihood of errors due to typos or incorrect types.
4
Leverage async/await for better performance in I/O operations.
Using async/await can significantly enhance the responsiveness of your applications by allowing concurrent operations without complicating the code structure.

Common Pitfalls

1
Failing to fully embrace Hack's type system can lead to missed opportunities for bug prevention.
Without leveraging the full capabilities of Hack's static type checker and type system, developers may continue to encounter runtime errors that could have been caught during development.
2
Neglecting to refactor code for type safety can complicate the migration process.
If developers do not take the time to refactor and properly type their code, they may face challenges in maintaining a clean and efficient codebase, leading to increased technical debt.

Related Concepts

Static Typing
Type Safety
Concurrency Management
Data Structures