Notes on How to Name Things in Scala (and Java)
(, en)
Some notes on how to name things in Scala and Java
Variable and Function Naming
There’s a great language-agnostic naming guide I often reference.
Some things I see in the wild:
No active verb in the function name
Examples:
def matchingRateCard(should bematchRateCard)def calculationPrice(should becalculatePrice)
Verbs as Nouns
initialCardCreates <- determineInitialCustomerCardCreates(customer): CardCreate
// should be
cardCreateCommands <- determineInitialCustomerCardCreateCommands(customer): CardCreateCommand
Leaking Implementation Details
I see AccountDto in the API spec. Use Account instead.
package org.bargsten.banking.transaction.swift.api.dto
// Don't name it AccountDto. You are already in the dto package AND
// having the suffix "Dto" in your (REST) API looks somewhat unprofessional.
case class Account(
id: String,
name: String,
balance: BigDecimal,
)
If you have to map from core/domain to dto, you can import the packages only, but not the models.
import org.bargsten.banking.transaction.swift.api.dto
import org.bargsten.banking.transaction.swift.core
Use dto.Account and core.Account to distinguish.
Using request/response naming, e.g. AccountRequest and AccountResponse is also a
common pattern.
