Not sure how better to word this (and, consequently, couldn't find previous answers, though I expect this has been answered before), but I'm interested in whether there's a way to turn code like this:
if ( this.props.mailboxFilter == 'sent' ) {
return this.currentUser.canActOnBehalfOf( m.senderID );
} else {
return !this.currentUser.canActOnBehalfOf( m.senderID );
}
To something like the below (not sure how better to express it):
var bangOrNot = this.props.mailboxFilter == 'sent ? '!' : '';
bangOrNot( this.currentUser.canActOnBehalfOf( m.senderID ) );
As in, is there a way to avoid the extended if/else syntax and all the repetition by choosing whether or not to call the return
line with a bang?
Not sure how better to word this (and, consequently, couldn't find previous answers, though I expect this has been answered before), but I'm interested in whether there's a way to turn code like this:
if ( this.props.mailboxFilter == 'sent' ) {
return this.currentUser.canActOnBehalfOf( m.senderID );
} else {
return !this.currentUser.canActOnBehalfOf( m.senderID );
}
To something like the below (not sure how better to express it):
var bangOrNot = this.props.mailboxFilter == 'sent ? '!' : '';
bangOrNot( this.currentUser.canActOnBehalfOf( m.senderID ) );
As in, is there a way to avoid the extended if/else syntax and all the repetition by choosing whether or not to call the return
line with a bang?
- 1 The solution might be elsewhere in your logic. Do you really want to send the inverse of the permissions? That seems weird. – isherwood Commented Dec 15, 2015 at 22:15
5 Answers
Reset to default 9You could simplify it in other ways:
var response = this.currentUser.canActOnBehalfOf(m.senderID);
return this.props.mailboxFilter == 'sent' ? response : !response;
In general, you would want to avoid this method if your function changes any state. However, since in this case you are calling it regardless, there's no harm in caching its value first.
If this.currentUser.canActOnBehalfOf( m.senderID )
allways returns a boolean value, you could do XOR.
return (this.props.mailboxFilter != 'sent') ^ this.currentUser.canActOnBehalfOf(m.senderID);
XOR a boolean value with true
makes a NOT. And XOR with false
makes a buffer which maintains the same value.
Note that I changed the ==
to !=
.
And, just make sure you put ments on this part of the code if you are going to use it actually. It is not that easy to read this code after a while.
Update
As Bergi suggested, it is better to use Boolean XOR !=
to return a boolean value.
return (this.props.mailboxFilter != 'sent') != this.currentUser.canActOnBehalfOf(m.senderID);
And of course if it can be used with boolean XNOR as well, just replace both !=
s with ==
in the code above.
You want to use the boolean XNOR operator here - also known as equivalence:
return this.currentUser.canActOnBehalfOf(m.senderID) == (this.props.mailboxFilter == 'sent');
There are many good ments regarding why or why NOT to do such thing. I'm assuming the OP has good reason to want to apply the bang (!) in front of the result of the function call. My answer is more of a technical solution to the OP's question.
function bang(r) {
return !r;
}
function notBang(r) {
return r;
}
function canActOnBehalf() {
return true;
}
var filter = 'notsent';
var f = (filter == 'sent' ? bang : notBang);
f(canActOnBehalf('whatever'));
Assuming you're using javascript, you can use the eval
method to evaluate a string of javascript code. You can conditionally apply the bang to the front of the string and then return the result of eval
.