Review this class. Are the names meaningful and consistent?
← Getting Started · Ref: Q1026
The first draft of this class had two naming issues, both fixed in the code shown here:
1. The discount method was called 'proc' - too abbreviated. It is now 'applyDiscount'. Method names should describe the action clearly.
2. The local for the discount amount was called 'x' - meaningless. It is now 'discountAmount' (and 'saving' in the program). Local variables should describe what the value represents.
The rest of the naming was already fine and is unchanged:
- 'Order' is a clear class name
- 'totalPrice' and 'customerName' are descriptive fields
- 'applyDiscount' clearly states the action
- 'discountRate' describes the parameter's role
Good naming makes code self-documenting. A reader should understand the purpose of each variable without reading the implementation.
Example
defines module qa.gettingstarted.reviewernaming defines class Order totalPrice as Float? customerName as String? default private Order() Order() -> name as String price as Float this.customerName: name this.totalPrice: price applyDiscount() -> discountRate as Float <- discountAmount as Float: Float() if discountRate? discountAmount: totalPrice * discountRate override operator ? as pure <- rtn as Boolean: customerName? and totalPrice? operator $ as pure <- rtn as String: `Order for ${customerName}` defines program NamingReviewDemo() stdout <- Stdout() order <- Order("Alice", 100.0) saving <- order.applyDiscount(0.15) stdout.println(`${order} saving: ${saving}`)
Common mistakes
E11031 — The name 'val' is one of the banned non-descriptive variable names, so 'val <- ...' triggers E11031 — use a descriptive name like saving. See ek9 -h E11031 for details.
Incorrect:
val <- order.applyDiscount(0.15) stdout.println(`${order} saving: ${val}`)
Correct:
saving <- order.applyDiscount(0.15) stdout.println(`${order} saving: ${saving}`)
Other ways to ask this
- Is the naming in this code good enough?
- Check if my variable and method names are clear
- Review my naming choices in this EK9 class
Coming from another language?
EK9 naming follows the same clean-code principles as any language. Use descriptive names that state purpose.
Keywords: review, quality, readable, clean, naming