Recommendation on what to return from module

44 views
Skip to first unread message

Mad Frode

unread,
Feb 17, 2015, 3:30:27 AM2/17/15
to nod...@googlegroups.com
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

Ryan Graham

unread,
Feb 17, 2015, 4:55:52 PM2/17/15
to nod...@googlegroups.com
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

--
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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/677843b4-66a8-40ca-8e78-e34d31ec9db0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mad Frode

unread,
Feb 18, 2015, 2:28:38 AM2/18/15
to nod...@googlegroups.com
That makes sense.

Thanks for the reply, Ryan!

Frode
Reply all
Reply to author
Forward
0 new messages