Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
181: Scaladoc Best Practices
I was reviewing some PRs for a teammate recently, and I noticed a pattern that drives me crazy. They were writing Scaladocs that basically just repeated the method signature in English. You know the type: def calculateTotal(amount: Double): Double documented as "Calculates the total of the amount". It's noise. It adds lines to the file without adding a single bit of actual information to the developer using the API.
Let's look at a real piece of code I'm working on for a payment gateway integration and walk through how I actually approach writing documentation that's worth reading.
The "It's Obvious" Fallacy
Here is my starting point. I've got a trait for a PaymentProcessor. At first glance, I might think the logic is so simple it doesn't need much explanation.
trait PaymentProcessor {
def processPayment(amount: BigDecimal, currency: String, providerId: String): Either[PaymentError, TransactionId]
}
If I just run the Scaladoc tool on this, the generated HTML is empty. If a new dev joins the team, they have to guess: Does currency need to be ISO 4217? What happens if the providerId is invalid? Is this method idempotent? The code tells them what it takes, but not how to use it correctly. Let's try to fix it, but I'll start with the "bad" way first, just to show you what to avoid.
/**
* Processes a payment.
*
* @param amount The amount to process.
* @param currency The currency to use.
* @param providerId The ID of the provider.
* @return An Either containing the error or the transaction ID.
*/
def processPayment(amount: BigDecimal, currency: String, providerId: String): Either[PaymentError, TransactionId]
I'm cringing just looking at that. I've added ten lines of text and told the reader absolutely nothing they couldn't have inferred from the types. This is "checkbox documentation"—done just to satisfy a linter or a manager.
Fighting the Urge to Parrot the Code
The trick is to document the contract, not the signature. I want to tell the caller about the constraints and the expectations. Let's adjust the documentation to actually provide value. I'll ask myself: "What would I need to know to call this without crashing the system?"
/**
* Initiates a financial transfer via the specified provider.
*
* This operation is synchronous and will block until the provider responds
* or the internal timeout of 30 seconds is reached.
*
* @param amount Must be positive. Amounts less than or equal to zero will
* trigger a [[PaymentError.InvalidAmount]].
* @param currency A three-letter ISO 4217 currency code (e.g., "USD", "EUR").
* @param providerId The unique identifier for the gateway; must be registered
* in the system configuration.
* @return A [[TransactionId]] if successful, or a [[PaymentError]] explaining
* why the payment failed.
*/
def processPayment(amount: BigDecimal, currency: String, providerId: String): Either[PaymentError, TransactionId]
Now we're talking. I've explained the timeout behavior, the specific format required for the currency, and the precondition for the amount. I also used [[...]]. That's a crucial Scaladoc feature—it creates a clickable link to the mentioned class or object in the generated docs, making it much easier for the user to navigate the API.
Handling Complexity and Edge Cases
But wait, there's more. What if this method has weird edge cases? For example, what happens if the network cuts out halfway through? Just putting that in a paragraph can get messy. This is where I use Markdown lists to make the documentation scannable.
I'll add a section explaining the failure modes. I prefer to keep the main description high-level and use a list for the specifics.
/**
* Initiates a financial transfer via the specified provider.
*
* This operation is synchronous and blocks until completion. Note that
* network failures during the request may leave the transaction in an
* "Unknown" state on the provider's end.
*
* Possible outcomes:
* - Success: Returns [[TransactionId]].
* - Validation Failure: Returns [[PaymentError.InvalidAmount]] or [[PaymentError.InvalidCurrency]].
* - Provider Error: Returns [[PaymentError.GatewayTimeout]] if the provider is unreachable.
*
* @param amount Must be positive.
* @param currency A three-letter ISO 4217 currency code.
* @param providerId The unique identifier for the gateway.
* @return A result indicating success or a specific [[PaymentError]].
*/
def processPayment(amount: BigDecimal, currency: String, providerId: String): Either[PaymentError, TransactionId]
By structuring it this way, I'm not just describing the method; I'm providing a manual. When you're the one maintaining a library used by fifty other developers, this is the difference between a quiet Friday afternoon and a Friday afternoon spent answering the same five questions on Slack.
The Final Polish
One last tip: avoid using words like "This method..." or "This function...". The reader knows they are looking at a method. Start with a strong verb like "Initiates", "Calculates", or "Retrieves". It keeps the documentation punchy and professional.
📋 Practical Task
Refactoring the Inventory Management API
You have been handed a WarehouseManager trait that is technically functional but has terrible documentation. The current Scaladocs are redundant and provide no actual guidance on how to use the API safely.
Your Task: Rewrite the Scaladoc for the adjustStock method. You must:
- Remove redundant "parrot" descriptions (e.g., "The quantity to adjust").
- Document the specific constraint:
quantitycannot be zero. - Document the behavior: If the resulting stock would be negative, the method should return a
StockError.InsufficientFunds. - Use
[[...]]links to reference theStockErrorandInventoryItemtypes. - Use a Markdown list to describe the possible return outcomes.
trait WarehouseManager {
/**
* Adjusts the stock.
*
* @param itemId The ID of the item.
* @param quantity The quantity to adjust.
* @return An Either with an error or the updated item.
*/
def adjustStock(itemId: String, quantity: Int): Either[StockError, InventoryItem]
}
sealed trait StockError
case object InsufficientFunds extends StockError
case object ItemNotFound extends StockError
case class InventoryItem(id: String, currentStock: Int)
There are no comments for now.