0

I have a function and I'm testing 4 variables, I want to optimize the structure of my if statement test, can some one help ? :

$scope.filterActivated = ->
    if $scope.postParams.options.scopes.report.from || $scope.postParams.options.scopes.report.to || $scope.postParams.options.template_id || $scope.displayOptions.query.length > 0
      return true
    else
      return false
  • what exactly you want to optimize? Looks not bad at all. – Void Spirit Oct 04 '18 at 16:27
  • 4
    `return ($scope.postParams.options.scopes.report.from || $scope.postParams.options.scopes.report.to || $scope.postParams.options.template_id || $scope.displayOptions.query.length > 0)` – N'Bayramberdiyev Oct 04 '18 at 16:27
  • hi @Timggwp and thanks for your comment , it look good this structure ? is there any other syntax that look much better than that ? –  Oct 04 '18 at 16:28
  • are you looking for destructing ?? – Learner Oct 04 '18 at 16:28
  • 1
    Correct me if I am wrong, but this isn't valid syntax for JS `$scope.filterActivated = ->`. If this is an arrow function there has to be `= () =>` for no parameters – Max Baldwin Oct 04 '18 at 16:29
  • @MaxBaldwin it seems CoffeeScript – jolvera Oct 04 '18 at 16:30
  • Also forgot to mention that you are comparing only the last field. It will always return true if `$scope.postParams.options.scopes.report.from` is not undefined – Void Spirit Oct 04 '18 at 16:30
  • @MaxBaldwin it's CoffeeScript syntax –  Oct 04 '18 at 16:31
  • thanks guys for your answers :) –  Oct 04 '18 at 16:33
  • If you are using coffeeScript consider using the existential operator. for example: `$scope.displayOptions.query.length` will blow up if scope.displayOptions.query is undefined. you could use the existential operator to guard against it blowing up by doing `scope.displayOptions.query?length` to keep from blowing up. – ruby_newbie Oct 04 '18 at 16:41

2 Answers2

1

you can remove true/false and optimize it a little bit like this:

$scope.filterActivated = ->
  options = $scope.postParams.options
  options.scopes.report.from or options.scopes.report.to or options.template_id or $scope.displayOptions.query.length > 0

Edit: JS for you:

$scope.filterActivated = () => {
  let options = $scope.postParams.options;
  return options.scopes.report.from || options.scopes.report.to || options.template_id || $scope.displayOptions.query.length > 0;
};
BraveVN
  • 411
  • 5
  • 22
0

Not sure what you mean by optimizing, but a shorthand of that could be:

$scope.filterActivated = ->
    $scope.postParams.options.scopes.report.from
    || $scope.postParams.options.scopes.report.to
    || $scope.postParams.options.template_id
    || $scope.displayOptions.query.length;

Edit:

Initially, I used the ternary syntax, but that's not supported by CoffeeScript. For reference Ternary operation in CoffeeScript

Edit 2:

Reduced it a bit more, @user633183 suggested using Boolean but I think this gives the same result.

jolvera
  • 2,369
  • 1
  • 15
  • 15
  • 1
    remove `? true : false` and it does the same thing – better `-> Boolean (a || b || c || d)` – Mulan Oct 04 '18 at 16:35
  • `... then true else false` is the same anti-pattern. just remove it. – Mulan Oct 04 '18 at 16:37
  • 1
    If you want to force it to return a boolean, you can wrap the whole expression in `!!(...)`. – Barmar Oct 04 '18 at 16:37
  • @user633183 can you point a reference where it says is an anti-pattern, I'm curious. – jolvera Oct 04 '18 at 16:37
  • @thinkxl `if (condition) then return true else false` is the same thing as `return Boolean (condition)` – or `return !!(condition)` as Barmar suggests – Mulan Oct 04 '18 at 16:38
  • @user633183 that's true, but I see it as different syntax, I was asking about a reference where it says `then/else` is an anti-pattern. – jolvera Oct 04 '18 at 16:39