Search code examples
javarefactoringthriftcode-readability

Apache Thrift Struct for better readability


I just started working with thrift and created following thrift interface.

map<string, map<string, string>> getInformationByIds(1: set<string> Ids)

As you can see, the return type is a map of maps. I want to know wether I can improve the readability by creating a custom thrift structure. Any direction is appreciated.


Solution

  • Although I have trouble relating this to readability only, I can give a generic advice that might also solve that question for you.

    Use a struct as return value

    Service methods are (currently) designed in a way most programming languages1) use the concept of a callable function: Although it allows for handing over 0-N arguments, you are still limited to a maximum of one return value only 2):

    RETVAL  function( ARG1, ARG2, ..., ARGN) throws (...)
    

    So using as struct as a return value in your service methods turns out to be a quite clever approach. In fact, it solves four problems at once:

    • not limited to 1 return value anymore: add as many fields as you need
    • extendable: you can add more fields in the future without breaking it
    • readability: separating method and retval gives you cleaner code
    • reusability: easily possible to use the same return structure for multiple methods

    How cool is that?


    1) e.g. Golang is a notable exception.

    2) We are not counting exceptions here, only normal data.