0

Since I met the out keyword in C#, I'm trying to find an equivalent approach in JS.

So, please take a look at the code and leave your opinions of existing cases or suggestions of new ways to implement this.

The working version is inside this sandbox:

https://stackblitz.com/edit/js-out

1 null on fail

const number_1 = tryParseNullOnFail(numberAsString);

if (null !== number_1) {
  page.log(`#1 Converted '${numberAsString}' to ${number_1}`);
} else {
  page.log(`Failed to convert '${numberAsString}'`);
}

2 kvp response

const { isNumber: isNumber_2, number: number_2 } = tryParseKvp(numberAsString);

if (isNumber_2) {
  page.log(`#2 Converted '${numberAsString}' to ${number_2}`);
} else {
  page.log(`Failed to convert '${numberAsString}'`);
}

3 by reference

const number_3 = [];

if (tryParseByReference(numberAsString, number_3)) {
  page.log(`#3 Converted '${numberAsString}' to ${number_3[0]}`);
} else {
  page.log(`Failed to convert '${numberAsString}'`);
}

4 condition expression

if (null != (number_4 = tryParseCondExp(numberAsString))) {
  page.log(`#4 Converted '${numberAsString}' to ${number_1}`);
} else {
  page.log(`Failed to convert '${numberAsString}'`);
}

5 IIFE short cercuit

const number_5 =
  tryParseNullOnFail(numberAsString) ??
  (() => {
    page.log(`Failed to convert '${numberAsString}'`);
  })();

page.log(`#5 Converted '${numberAsString}' to ${number_5}`);

6 high order function

function withTryParse(str, onSuccess, onFail) {
  const number = Number(str);
  isNumber(number) ? onSuccess(number) : onFail(str);
}

const onSuccess = (number_6) => {
  page.log(`#6 Converted '${numberAsString}' to ${number_6}`);
};

const onFail = (str) => {
  page.log(`Failed to convert '${str}'`);
};

withTryParse(numberAsString, onSuccess, onFail);

The mad professor

for (
  var done = false, number_ = tryParseMad(numberAsString);
  !done && null !== number_;
  done = true
) {
  page.log(`#* Converted '${numberAsString}' to ${number_}`);
}
10
  • How best to implement out params in JavaScript? Personally, it looks like more trouble than it's worth. Oct 12, 2021 at 14:55
  • @RobertHarvey, that's a good article that I found earlier but it does not try a different approach but by reference. I'm trying to find there may be something that I've missed.
    – user403390
    Oct 12, 2021 at 15:01
  • codereview.stackexchange.com might be a better venue for your question. Oct 12, 2021 at 15:14
  • @RobertHarvey: I'm actually pretty sure this person posted a similar question at code review. Oct 12, 2021 at 15:45
  • 1
    In this particular case, parsing a string to a number, returning NaN is a very natural way to express failure. In the more general case, most JS code either returns null on failure, or what you call a KVP: your options 1 or 2.
    – user949300
    Oct 12, 2021 at 17:02

1 Answer 1

1

out parameters in C# are one way of implementing the "try get pattern", where we may receive either nothing, or a value. So bool TryGetXxx<T>(out T value) is a common way that that pattern is implemented in C#.

But for any language that offers discriminated unions, the Maybe<T> type can be used instead. It either returns none or a value. So if you can use TypeScript rather than JavaScript, then this StackOverflow answer summarises various ways this type can be created and used in TypeScript.

If you can't use TypeScipt, then a common way of implementing a "poor man's maybe type" is via using null to represent none, as per your "null on fail" option. Of course, this only works in situations where null isn't a valid return value, but if it isn't this is a simple and effective solution to your requirement.

1
  • Yes, thanks for your answer. That's why I put nullOnFail first because it's the first thought that you have when getting into a similar case. But that implies you have to do double if. What are you thinking about #2?
    – user403390
    Oct 12, 2021 at 18:49