Java – is it better to use inline local variables or chain methods?
If I have a series of method calls, each value for the next call, I should store them in local variables, as follows:
DynamicForm filledForm = Form.form().bindFromRequest(); String shareIdStr = filledForm.get("data[shareId]"); UUID shareId = UUID.fromString(shareIdStr); Share share = Share.find.byId(shareId);
Or as a single call chain, as follows:
Share share = Share.find.byId(UUID.fromString(Form.form().bindFromRequest().get("data[shareId]")));
In this case, the only value used again is share Maybe the answer is somewhere in between, or something completely different What's your opinion?
Solution
ADV
>Enhance readability. > Provide opportunities for reuse. > Pin abnormality (if any) becomes easier. > Debugging becomes easier, that is, it's easy to set breakpoints on specific calls
DisADV
>Increase the length of the code (I won't say size:). > IDE warning (if any)
ADV
>Reduce the need to create multiple temporary Variable. > Yes, synchronous sugar > reduce the number of rows to write
DisADV
>Reduce code readability. > Commenting becomes difficult, if any, for the particular method being invoked. > Debugging the entire call chain becomes very difficult