Code Review Rejected a PR Over a 9-Parameter Constructor. The First Fix Was Worse
DEV Community

Code Review Rejected a PR Over a 9-Parameter Constructor. The First Fix Was Worse

The pull request added a loyalty discount to order placement, and it was rejected for a reason that had nothing to do with loyalty discounts. To make the feature work, the author had added a ninth dependency to OrderService, and the reviewer's comment was one line: nine constructor parameters means this class is doing too much, can we look at that first?

The author's first response was the one most of us would reach for. Nine parameters are hard to read, so wrap them. The second version of the pull request introduced an OrderServiceDependencies record holding all nine collaborators, and the constructor went down to a single argument.

It looked tidier but the reviewer rejected it again, and this time the comment was longer, because the second version was worse than the first.

The Constructor Was Never the Problem

The constructor had never been the problem. Nine parameters meant nine collaborators, and a class that needs nine collaborators to do its job is a class with several jobs. Wrapping them in a record did not reduce that number. It hid it, so the next dependency could be added to the record without anyone noticing the count, and every test still had to provide all nine.

So we did what the reviewer had asked for and looked at what the class actually did. Reading through placeOrder and the methods it called, the nine dependencies grouped themselves into four jobs. Validating the order and reserving stock used three of them. Calculating the price, including all the discount rules, used three more. Sending the confirmation email and push notification used two. Writing an audit record used the last one.

Placing an order genuinely needs the first two jobs to happen in the same transaction. The other two do not. They need to happen after an order has been placed, and they do not need to be done by the class that placed it.

The Refactor

The refactor split it accordingly. OrderPlacement now validates, reserves stock and persists, with four dependencies including a new OrderPricing that owns the three pricing collaborators and every discount rule, the new loyalty one among them. Placement publishes an OrderPlaced event when it commits, and two small listeners handle notifications and auditing on their own. No class in the flow has more than four constructor parameters.

The change was most visible in the tests. Every test of the old OrderService, including the ones that only cared about pricing, had to construct or mock nine collaborators. A test for a loyalty discount now builds OrderPricing with three, and none of them is the email client. Pricing tests became pricing tests, which made them shorter, faster to read and much harder to break by accident when someone changed the notification template.

Constructor Size as a Measurement

The useful habit I took from that review is to treat constructor size as a measurement, not a style issue. Constructor injection makes the number of collaborators impossible to ignore, which is exactly why it is worth having. When the number feels uncomfortable, the fix is almost never to make it look smaller. It is to find out which jobs the class is doing, and give some of them away.

More like this? I write short production war stories like this one, plus a deep-dive architecture series. Subscribe here if you want both in your inbox.

Recommended Resources for Java & Spring Boot Engineers

If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot & Architecture skills, check out these highly-rated resources from the Javarevisited publication (Use promo code friends20 for an exclusive 20% discount automatically applied at checkout):

  • Grokking the Java Interview

    Prepare for core Java, concurrency, JVM internals, and design pattern questions. Get Full Book (Paid) | Download Free Sample Copy

  • Grokking the Spring Boot Interview

    Master Spring Core, Auto-configuration, Spring Data JPA, Security, and Microservices. Get Full Book (Paid) | Download Free Sample Copy

  • Grokking the SQL Interview

    Deep-dive into query optimization, indexing, joins, and complex SQL window functions. Get Full Book (Paid) | Download Free Sample Copy

  • The Complete Java + Spring + SQL Interview Bundle

    Get all three interview guides in a single heavily discounted package. Get the Ultimate Interview Bundle

  • Spring Professional Certification Practice Questions (250+ Questions)

    Validating your skills? Practice with real exam-style questions before taking the Spring Professional certification. Get the Spring Professional Questions | Download Free Sample Copy

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.