CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    35

    What's your preference on writing functions: ByRef manipulation or returning values?

    Hi guys,

    I was wondering about this and thought I'd ask this here.

    Personally I find that code readability is really important, if not the most important part of software development.

    So given the following examples, which one do you think is more readable?

    Code:
    // example 1 (return result)
    public function doSomething1(String someString):String{
     /* some manipulation on someString */
     return someString;
    }
    public function doSomething2(String someString):String{
     /* some manipulation on someString */
     return someString;
    }
    
    
    //example 2 (ByRef)
    public function doEverything(String someString1, String someString2):void{
        someString1 = doSomething1(someString1);
        someString2 = doSomething2(someString2);
    }
    Which function would you call if you needed to apply the actions doSomething1 and doSomething2 to strings? Does it make sense to group the actions into a wrapper which manipulates the strings accordingly? Is there a magic number at which you would start wrapping your functions?
    Does the Single-Responsability-Pattern still apply assuming that the actions doSomething1 and doSomething2 are strongly related?


    Discuss

    Laurent

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: What's your preference on writing functions: ByRef manipulation or returning valu

    I prefer to pass in, out, in/out variables as arguments, and return an error code, which would be 0 if everything went well, or some value if something went wrong.

    Code:
    //example 3 (ByRef in doEverything and also in doSomething, and return code)
    public function doEverything(String someString1, String someString2):int
    {
       int errorCode = doSomething1(someString1);
       if (errorCode != 0) return(errorCode);
    
       errorCode = doSomething2(someString2);
       return(errorCode);
    }
    Last edited by olivthill2; August 30th, 2011 at 03:27 AM.

  3. #3
    Join Date
    Jun 2011
    Posts
    35

    Re: What's your preference on writing functions: ByRef manipulation or returning valu

    Hi olivthill2,

    I like your view on this as it cover both business and error cases. However I think that for error management we should re-use an existing mechanism, such as exceptions? It would probably make the code more read-able and create an alternative stream in your code for error handling.

    In your case, for instance, you'd error check by comparing the output, such as:
    Code:
        int manipulationOutcome = doEverything(someString1, someString2);
        if (manipulationOutcome != 0) {
            System.err.println("something went terribly wrong");
        }
    In comparison the exception-based one

    Code:
    try{
    doEverything(someString1, someString2);
    }catch (Exception e){
            System.err.println("something went terribly wrong:" + e.getStackTrace());
    }
    I know that error handling was not the main purpose of this thread but I thought I should still point it out.


    What do you think about returning an array of results from a function? I know that it would probably overcomplicate the problem but would it make it more readable?


    I always try to avoid ByRef so as to make functions behave as clearly as possible. Sometimes I'd even make a function explicitly return a value eventhough it wouldn't be necessary, like so:
    Code:
    public function doSomething1(String someString):String{
     someString = "." + someString + ".....";
     return someString;
    }
    This doesn't cost anything performance-wise yet makes the code more readable IMO..

    Laurent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured