-5

I'm trying to code something who could be say by "If X or if Y AND Z" but it looks like I made a mistake

if((machine.isUsed == false) || (if(this.whoUseIt.includes(Session.get("loggedUser")))&&(machine.isUsed))){

I need to do this test to avoid a code duplication. If you have a better idea I'm still opened to it.

Jerome
  • 1,162
  • 2
  • 16
  • 32
  • 3
    In almost every language it will take this form `if (x || (y && z)) {` I don't use javascript but I'd assume it does too. Try removing the extra `if` in the middle – Albert Renshaw Apr 05 '17 at 06:03
  • @AlbertRenshaw you are right, you can put an answer you were the first one. Then why I got so much down votes ? – Jerome Apr 05 '17 at 06:08
  • 1
    @Jerome Ignore the "downvote"s. What does "downvote" add to the inquiry without relevant and meaningful description accompanying? The important part is getting answer to your Question – guest271314 Apr 05 '17 at 06:10
  • @guest271314 yes you are right but it still a bit annoying, anw I learned a lot of things with all the answers :) – Jerome Apr 05 '17 at 06:13

5 Answers5

4

Using ! rather than == false and removing the extra parentheses:

if (!machine.isUsed || (this.whoUseIt.includes(Session.get("loggedUser")) && machine.isUsed)) {

But not A or (B and A) can be simplified to not A or B:

if (!machine.isUsed || this.whoUseIt.includes(Session.get("loggedUser"))) {

It reads as: if the machine is not used or this is the user that is using it.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Hi, no I can't simplify because to test the `this.whoUseIt.includes(Session.get("loggedUser"))` the `machine.isUsed` must be true – Jerome Apr 05 '17 at 06:09
  • 3
    If `machine.isUsed` is true then `!machine.isUsed` is false and the value of `this.whoUseIt.includes(Session.get("loggedUser"))` is then required. Testing if `machine.isUsed` is true is redundant if we have already tested that it is not false. – Dan D. Apr 05 '17 at 06:13
  • @Jerome [Short-circuit evaluation](http://stackoverflow.com/a/9344369/1270789) also guarantees that the second expression will never be called if `isUsed` is false. – Ken Y-N Apr 05 '17 at 06:14
  • Ok, thank you for the input and now I'll review my code because I'm sure I have a lot of redundant test – Jerome Apr 05 '17 at 06:17
0

Remove if() within if condition

if (machine.isUsed == false 
  || this.whoUseIt.includes(Session.get("loggedUser")) 
  && machine.isUsed) {}
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    Please bracket (or indent as a bare minimum) as I for one have to stop and think which of `||` and `&&` has the higher precedence. Also, `!machine.isUsed` would be better, IMO. – Ken Y-N Apr 05 '17 at 06:05
  • @KenY-N Brackets are not necessary. Not sure what you mean by "better"? – guest271314 Apr 05 '17 at 06:07
  • @KenY-N, why do you have to think which of `||` or `&&` has precedence? It just as logical as multiplication having precedence over addition. – trincot Apr 05 '17 at 06:07
  • 1
    @trincot I know, and that's how I remember it, but I always do have to consciously think which it should be! Perhaps it is just me, but I always apply the "principle of least astonishment" to `||` and `&&`, although never to `+` and `*`... – Ken Y-N Apr 05 '17 at 06:11
  • I have more trouble with understanding code that is full of parentheses. – trincot Apr 05 '17 at 06:33
0
if(x || (Y && Z)){...}

Should be fine.

You don't need to add another if inside if.

Prabhu Vinod
  • 328
  • 2
  • 10
0

If X or if Y AND Z => If (X || (Y && Z))

 if(
   (machine.isUsed == false) || 
   (this.whoUseIt.includes(Session.get("loggedUser")) && (machine.isUsed))
 )
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
0
if(X || (Y && Z)){
    /* your logic
}
Gohel Dhaval
  • 820
  • 1
  • 8
  • 12
  • Although this code might solve the problem, one should always add a explanation to it explaining why/how the problem is solved. – BDL Apr 05 '17 at 11:11