Is it ok to use composite returns?

Hello, I am beginner and I’d like to know if it’s ok to have returns(or any expression or statement) like this:
return adjust(Clock{c.hour, c.minute + minutes})

Would it be better, if I just adjust the Clock and then return it?

 return c```

Or even:
```c := Clock{c.hour, c.minute + minutes})
c = adjust(c Clock)
return c```

I assume it's up to me. This example is easy to read, but sometimes it could get really messy. So, is there any recommendation about how much it should be shrank? Or what is your opinion about how much is too much. Thanks.

It is indeed up to you. Most Go code is however written to favour explicitness and clarity rather than brevity. In your example, I’d probably have used the first and most brief way as it’s still entirely clear. The equation changes if the struct is larger or the function call important to point out or expensive.

1 Like

Why not put an adjust method on Clock that returns an adjusted Clock?

@calmh: Thank you very much.
@dfc: I am not sure if I understand you. Do you think to make something like return c.Adjust()?


Thank you very much, good reason to make a new iteration in Exercism. But it was just (maybe not good) example of writing this composite statements in general.

A good example of a composite literal should include the names of the field.

c := Clock{
Seconds: s,
Nanoseconds: s,

This ensures that

  1. You can avoid explicitly assigning the zero value to fields you don’t care about
  2. Avoid breakage in the future if a structure acquires new fields

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.