3

I have some incoming request - it's an instance of class generated from api specification - POJO with public getters/setters. I would like to normalize some values. For example dimensions (to use metric system).

I have a service class which has method processing incoming requests. First I would like to normalize the request.

So I define the method like:

private void normalizeDimensions(Request request);

and inside I normalize request's fields related to dimensions.

But from the readability perspective I think it's better to return the modified object so it's more explicit that something was changed and ideally the caller should use the returned object:

private Request normalizeDimensions(Request request);

What do you think is better from readability perspective or generally?

Shaolin
  • 33
  • 5
  • Does this mean that all Request properties are public? – Christophe Jun 09 '23 at 19:14
  • @Christophe yes, I modified the question. – Shaolin Jun 09 '23 at 20:16
  • Here is pretty the same question [Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?](https://softwareengineering.stackexchange.com/questions/262221/coding-style-issue-should-we-have-functions-which-take-a-parameter-modify-it?rq=1). But in my case the object to modify is a generated POJO. – Shaolin Jun 09 '23 at 20:21
  • @Shaolin: note my answer to that other question which I posted a link above - even when you here use a non-member function, my arguments against this style stay exactly the same - you create the opposite effect of what you are trying to accomplish. And Philip Kendall's answer is telling you almost the same. – Doc Brown Jun 09 '23 at 20:28
  • @DocBrown I find your answer helpful and I used it to mark my question as a duplicate. But I think that this question should be duplicate of [Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?](https://softwareengineering.stackexchange.com/questions/262221/coding-style-issue-should-we-have-functions-which-take-a-parameter-modify-it?rq=1). My opinion is that it answers my question slightly better. – Shaolin Jun 09 '23 at 21:01

2 Answers2

4

I think you have this backwards. The only thing that

private void normalizeDimensions(Request request);

can do is to modify the object (or have other side effects), so it's clear that's what this function must do.

private Request normalizeDimensions(Request request);

implies to me that it returns a new Request object and does not modify the original object.

But then I come from a functional world where objects are immutable so the idea of modifying an object doesn't exist.

Philip Kendall
  • 22,899
  • 9
  • 58
  • 61
  • sorry, but there is no implication that the object won't be changed in the second approach. Method chaining and fluent interfaces are still popular. A prefix convention would be advised (such as `with` to tell that the original value doesn't change: https://blog.joda.org/2011/08/common-java-method-names.html?m=1 ) – Christophe Jun 09 '23 at 19:13
2

In the functional world Philip Kendall is entirely correct. And in the OOP world we occasionally write functional code as well. So if you're going to make Request immutable then:

private Request normalizeDimensions(Request request);

This makes me expect a modified defensive copy of the immutable request. Even in the OOP world.

But if your Request is mutable then in the OOP world it would look like this:

public void normalizeDimensions();

and you'd call it like this:

request.normalizeDimensions();

Because in the OOP world, objects suck up the methods that act on them.


Now if Request is big and ugly and you'd like it to be immutable anyway another OOP trick (at least in the Java world) is the Joshua Bloch Builder:

public RequestBuilder normalizeDimensions();

Which would be used like this:

Request request = new Request
    .Builder("Stuff")
    .otherStuff("stuffy")
    .normalizeDimensions()
    .build()
;

Done this way you get one chance to normalize those dimensions on the builder before it spits out the immutable request.

This is mostly a Java thing because those lucky C# coders have named arguments which is what Joshua is helping us simulate here.

candied_orange
  • 102,279
  • 24
  • 197
  • 315