Ideally the "shape" of your return values shouldn't change based on
arguments. I think the application of this return shape principle is pretty
clearly pointing at using 3 different functions, but it's possible the real
code is more nuanced than that.

~Ryan

On Tue Feb 17 2015 at 11:54:22 AM Mad Frode <[email protected]> wrote:

> Hi!
>
> I have a module, a simple function that takes two parameters. If only one
> parameter is given, it returns  an array as the result and all is fine.
>
> If two paramenters is given an array is also the main result. But for some
> edge cases, it returns another array with some additional data.
>
> If it was not for the edge (rare) cases that requires an additional array
> of data, I would have just returned an array, so the user would use my
> module like this:
>
> var coolModule = require('cool-module');
> var resultArray1 = coolModule('parameter1');
> var resultArray2 = coolModule('parameter1', 'parameter2');
>
> But since I have some edge cases that requires an additional array, how
> should I best return data? I see the following alternatives:
>
> // Returned data is object:
> var resultArray1 = coolModule('parameter1').resultArray;
> var resultArray2 = coolModule('parameter1', 'parameter2').resultArray;
> var resultArray3 = coolModule('parameter1',
> 'parameter2').resultEdgeCasesArray;
>
> // Return data as array when only one paramenter, return data as object if
> two properties:
> var resultArray1 = coolModule('parameter1');
> var resultArray2 = coolModule('parameter1', 'parameter2').resultArray;
> var resultArray3 = coolModule('parameter1',
> 'parameter2').resultEdgeCasesArray;
>
> // Use 3 different methods:
> var resultArray1 = coolModule.oneParameterMethod('parameter1');
> var resultArray2 = coolModule.twoParameterMethod('parameter1',
> 'parameter2');
> var resultArray3 = coolModule.edgeCasesMethod('parameter1', 'parameter2');
>
> // Use an array, and for the edgecase have a special method. But then I
> would need to create an additional method name for the standard cases:
> var resultArray1 = coolModule.standardMethod('parameter1');
> var resultArray2 = coolModule.standardMethod('parameter1', 'parameter2');
> var resultArray3 = coolModule.edgeCasesMethod('parameter1', 'parameter2');
>
> // Use array in all cases, but for the second add the edge cases result as
> a property on the array:
> var resultArray1 = coolModule('parameter1');
> var resultArray2 = coolModule('parameter1', 'parameter2');
> var resultArray3 = coolModule('parameter1',
> 'parameter2').resultEdgeCasesArray;
>
> I would have preferred the last one, since in 99% of the cases the users
> of the module would just be interested in one array. But since this would
> limit the users way of working with the array (the user could not use
> for...in to traverse the array, but would use a normal for-loop), then I
> guess this is a bad option.
>
> Which would you choose, or maybe there is a better (simplest/most
> logical/most used) approach?
>
> Thanks you in advance for any thoughs!
>
> Frode
>
>  --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/677843b4-66a8-40ca-8e78-e34d31ec9db0%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/677843b4-66a8-40ca-8e78-e34d31ec9db0%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAGjmZGwVUjmCioWcYSB%2B6xRYHdGuPSKzgrbqnQMSVuz9F5RLuQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to