What is appropriate formatting style when multiple methods are invoked from a method

StackOverflow https://stackoverflow.com/questions/19604116

  •  01-07-2022
  •  | 
  •  

Вопрос

So I have three possibilities, and I present real code since dry example can lead to misunderstanding of my problem

1 Big big hose approach - all in one line

return ItemDtoMapper.generateItemDtoFromItem(itemDao.saveItemAndReturn(ItemDtoMapper.generateItemFromItemDto(itemDto, UserDtoMapper.generateUserFromDto(userService.findByUserName(UserNameExtractor.getLoggedUser())))));

2 Hose formatted - breaking by parenthesis

 return ItemDtoMapper.generateItemDtoFromItem(
     itemDao.saveItemAndReturn(
          ItemDtoMapper.generateItemFromItemDto(itemDto, UserDtoMapper.generateUserFromDto(
              userService.findByUserName(
                 UserNameExtractor.getLoggedUser())))));

3. Insane object creation - splitting calls for explicite objects

UserDto userDto = userService.findByUserName(UserNameExtractor.getLoggedUser());
User user = UserDtoMapper.generateUserFromDto(userDto);
Item itemForSave = ItemDtoMapper.generateItemFromItemDto(itemDto, user);
Item itemForDao = itemDao.saveItemAndReturn(itemForSave);
return ItemDtoMapper.generateItemDtoFromItem(itemForDao);

I'm aware that my software architecture is not perfect, but this question is just for getting best answer about professional formatting in such situation.

Это было полезно?

Решение

I vote for the second one. To improve readability I would add static imports (Alt+Enter on the method in Intellij Idea) and add an alias to a repeatedly used method ItemDtoMapper.generateItemDtoFromItem like ItemDtoMapper.newItemDto. The less symbols get into way the better.

static import ItemDtoMapper.generateItemDtoFromItem;
static import UserNameExtractor.getLoggedUser;

...

return newItemDto(
        itemDao.saveItemAndReturn(
            newItemDto(
               itemDto, 
               newUser(userService.findByUserName(getLoggedUser())
            ))));

Or may be extracting the nested call makes it more readable:

ItemDao daoToSave = newItemDto(
    newItemDto,
    newUser(userService.findByUserName(getLoggedUser())
));

return newItemDto(itemDao.saveItemAndReturn(daoToSave));

Другие советы

tl;dr Favor readability over brevity.

Writing one-liners may be cool in a project in which you are the only developer and which you won't have to maintain in the future, but since your are asking 'professional'...

As you call it, 'insane object creation' is the way to go. Why?

  1. You get to name all subsequent parts of the computation, so the code is that much easier to read;

  2. The code should be in some small scope (e.g. a dedicated function) so those additional variables will be gone as soon as the scope ends anyway;

  3. Those objects are created anyway, just without variables referencing them, so performance impact is practically non-existent.

Also, you should not choose your own formatting depending on the code you have just written. Use some standard formatter throughout your entire company and get your IDE to always automatically format your code in a coherent way.

I can think of an objective answer:

  • If you can think of good variable names, then go for the second one because it is clearer

  • If you can't think of good variable names, then go for the first one, because it documents the fact that the calls are unordered with respect to each other.
    (The latter suggests they must happen in a certain order, which may confuse the reader into wondering why this is so.)

We use second one.

1. pros: 
  - No additional objects creates, 
  - wellformatted easy reading.
2. contras: 
  - If editing, need to look after dots and brackets 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top