Review this EK9 class. Does it follow EK9 best practices?
← Code Quality · Ref: Q992
This class follows several EK9 best practices:
GOOD:
- Fields are private (class default) with accessor methods
- Constructor uses -> for parameters and assigns with :
- 'default operator' generates standard operators from fields
- The describe() method returns a value rather than printing (pure)
- Named accessor methods match field names (accountHolder, currentBalance)
IMPROVEMENT OPPORTUNITIES:
- The deposit and withdraw methods could validate inputs (e.g., negative amounts)
- The class could use 'as pure' on accessor methods to declare they have no side effects
- Consider using :=? guarded assignment in withdraw to prevent negative balance
- If this class is not meant to be extended, the default closed status is correct
OVERALL: Good basic design. The separation of data (private fields) from behaviour (methods) is correct EK9 style.
Example
defines module qa.codequality.reviewclass defines class BankAccount accountHolder as String: String() currentBalance as Float: 0.0 BankAccount() -> accountHolder as String currentBalance as Float this.accountHolder: accountHolder this.currentBalance: currentBalance accountHolder() as pure <- rtn as String: accountHolder currentBalance() as pure <- rtn as Float: currentBalance deposit() -> depositAmount as Float currentBalance := currentBalance + depositAmount withdraw() -> withdrawalAmount as Float currentBalance := currentBalance - withdrawalAmount describe() as pure <- rtn as String: `${accountHolder}: ${currentBalance}` default operator defines program ReviewClassDemo() stdout <- Stdout() account <- BankAccount("Alice Smith", 1000.0) stdout.println(account.describe()) account.deposit(500.0) stdout.println(account.describe()) account.withdraw(200.0) stdout.println(account.describe())
Common mistakes
E06180 — Class fields are private in EK9. Access them through methods: account.describe() or account.accountHolder(). Direct field access like account.accountHolder only works on records.
Incorrect:
stdout.println(account.accountHolder)
Correct:
stdout.println(account.describe())
Other ways to ask this
- Assess the quality of this EK9 class design
- What could be improved about this class?
- Is this class well-designed by EK9 standards?
Coming from another language?
EK9 classes follow encapsulation by default — fields are private, methods provide access. Use 'default operator' to auto-generate standard operators.
Keywords: quality, best practice, class, review, assess, design