1

I am following a javascript course and they did something like this:

function removeTransition(e) {
  if (e.propertyName !== 'transform') return
  // code here
}

However, to me, I think it would be better this way:

function removeTransition(e) {
  if (e.propertyName === 'transform') {
    // code here
  }
}

Is there a reason to prefer one way over the other or it is just a style decision?

kevin parra
  • 336
  • 4
  • 11

2 Answers2

1

It is your decision to make the call since it's sounds like a code convention issue.

I suggest you apply the same code styling conventions in your project to keep consistency. you can use eslint for that.

https://eslint.org/

Ran Turner
  • 14,906
  • 5
  • 47
  • 53
0

I don't have a definite answer, but personally I prefer the first variant because it doesn't have the main logic code inside an if block. It checks for situations where you want to bail out (the single if line with return in your example) , and if it passes those checks it proceeds with the actual work in the outermost indent level of the function. Of course, this main part can have any number of if checks and indent levels in itself. I think this makes for a clearer structure. But there may be situations where this is not the best solution.

Btw, coldgas' reply misses the point completely. Edit: Rukshan also misses the point, it seems. The question is not about using curly braces or not in an if statement.

tonebender
  • 69
  • 4