1
\$\begingroup\$

I need to fetch data from an server that isn't always reliable and unfortunately fixing that is out of my hands. My team determined we would attempt the request up to 3 times.

So I thought of using a try-catch inside the while loop but didn't like the extra indentation for no good reason and thought to remove the braces from the while loop.

async function doAjax() {
  const data = { /* stuff */ }
  const retryLimit = 2;
  let retryCount = 0;
  let found = false;

  while (!found && retryCount <= retryLimit) try {
    let serverResponse = await $.get("url", data);
    // Do stuff
  } catch (err) {
    retryCount++;
  }

  // Do more stuff
}

I know my team won't be against this, but I still got curious if there's a consensus or some reason in favor/against writing code this way. Thoughts?

\$\endgroup\$

closed as off-topic by Sᴀᴍ Onᴇᴌᴀ, vnp, 200_success, yuri, t3chb0t Jun 15 at 8:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – vnp, 200_success, t3chb0t
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. \$\endgroup\$ – dfhwze Jun 14 at 22:21
3
\$\begingroup\$

I would not do this. It breaks too far from any typical convention for very little gain.

I'm personally against omitting braces at all; except in very narrow circumstances. I've seen many questions on stack overflow that stem from people omitting braces and not being able to figure out why "only certain lines are running".

This also appears to look like a new language construct and feels a little jolting at first. It takes an extra second to realize what's going on.

Considering how short the lines are, indentation shouldn't be a problem; and even if it is, there are other ways of addressing that problem. I would write this in the standard way

async function doAjax() {
  const data = { /* stuff */ }
  const retryLimit = 2;
  let retryCount = 0;
  let found = false;

  while (!found && retryCount <= retryLimit) {
    try {
      let serverResponse = await $.get("url", data);
      // Do stuff

    } catch (err) {
      retryCount++;
    }
  }

  // Do more stuff
}

If you like playing around with making code read interestingly and creating language constructs, I recommend playing around with a language with Clojure (or another lisp). Doing experimental things with the syntax is a little more expected in a language like that.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your honesty! I understand it was an opinion based question, and I would have asked on the javascript chat to get opinions but I don't have enough reputation 😅 \$\endgroup\$ – neerengi Jun 14 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.