what is the opposite of this logical statement - if-statement

I would like to check for the else condition only. Is there a way that I can simplify the code below?
if (_endCurrentGoal && _startCurrentGoal && _startCurrentGoal === _startEffectiveDate && _endCurrentGoal === _endEffectiveDate) { //no statement );
}
else {
console.log("check for this code only");
}
Is below code correct? is there a way to simplify it?
if(!(_endCurrentGoal && _startCurrentGoal && _startCurrentGoal === _startEffectiveDate && _endCurrentGoal === _endEffectiveDate) {
console.log("check for this code only");
}

You can use De Morgan's Law to distribute the ! through the &&s.
That will look like this:
if(!_endCurrentGoal || !_startCurrentGoal || _startCurrentGoal !== _startEffectiveDate || _endCurrentGoal !== _endEffectiveDate) {
console.log("check for this code only");
}

Related

writing a junit test(s) for a method that returns boolean and accepts two params of non-primitive type

I am fairly new to writing Junits in such detail.
The method
returns boolean true/false
accepts two params of same non-primitive type.
This method does a whole lot of validations by comparing the two instances of the similar type using if else conditions.
And few more validations while iterating through the collection based fields of the params .
I tried writing the base scenario, but unclear as to how to do it for the underlying if_conditons. especially with regard to mocking the data.
What I tried and aware: There are lots of methods under AssertClass which I can use to validate, however for this method that return boolean based on quite a bunch of conditions, how do I write? do I have to write individual asserts for each condition under the #Test annotated method?
below is my code :
public boolean checkEligblity(A newversion, A currentversion) {
if (currentversion.getPurchaseOrders().isEmpty())
return true;
if (currentversion.getReceiptNotice() != newversion.getReceiptNotice()
|| currentversion.getSizeType() != newversion.getSizeType()
|| currentversion.getNumInPrices() != newversion.getNumInPrices())
return true;
boolean storeM = newversion.getReceiptNotice() == PASSED;
if (currentversion.getOrderInfo() != newversion.getOrderInfo()) {
if (storeM || containsCustomerString(currentversion.getOrderInfo())
|| containsCustomerString(newversion.getOrderInfo()))
return true;
}
List<PurchaseOrders> newVersionPurchaseOrders = newversion.getPurchaseOrders();
List<PurchaseOrders> currentVersionPurchaseOrders = currentversion.getPurchaseOrders();
for (Iterator<PurchaseOrders> newPOIterator = newVersionPurchaseOrders.iterator(); newPOIterator.hasNext();) {
PurchaseOrders newPO = newPOIterator.next();
for (Iterator<PurchaseOrders> currentPOIterator = currentVersionPurchaseOrders.iterator(); currentPOIterator
.hasNext();) {
PurchaseOrders currentPO = currentPOIterator.next();
if (newPO.getSku() == currentPO.getSku()
&& newPO.getSkuId() == currentPO.getSkuId()
&& newPO.getOrgin() == currentPO.getOrgin()
&& newPO.getTarget() == currentPO.getTarget()
&& newPO.getShippingDate()== currentPO.getDepratureDate()) {
if (currentPO.getStatusCode().equals(newPO.getStatusCode())
&& currentPO.getItemsInQty().equals(newPO.getItemsInQty())
&& currentPO.getVendorId().equals(newPO.getVendorId())
&& currentPO.getCodeInd().equals(newPO.getCodeInd())
&& currentPO.getPriceType().charAt(0) == newPO.getPriceType().charAt(0)) {
boolean matchingFound = true;
if (storeM && currentPO.getInbound() != newPO.getInbound()) {
matchingFound = false;
}
if (matchingFound) {
newPOIterator.remove();
currentPOIterator.remove();
break;
}
}
}
}
}
if (!newVersionPurchaseOrders.isEmpty() || !currentVersionPurchaseOrders.isEmpty()) {
return true;
}
return false;
}
private boolean containsCustomerString(String ticketingInfo) {
if (orderInfo == null)
return false;
if (orderInfo.startsWith(PASS))
return true;
return false;
}

Hello everyone, please help me check this IF statement in Google app script

I want to make a code to assign logic input for my sheet. I use IF to make it. My code ran successfully but the logic didn't work. I have checked it many times, but I couldn't find something wrong. Can you help me with this? I'm stuck. Please review my example sheet and my script for more information. Thank you! https://docs.google.com/spreadsheets/d/1eV2SZ45Gs6jISgh_p6RIx-rfOGlHUM6vF114Mgf6c58/edit#gid=0
function logic(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeCell = ss.getActiveCell();
if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "Yes") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue("1");
} if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "Hafl") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue("1/2");
} if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "No") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue(0);
}
}
You can simplify your code this way.
(Note that I use the const variable declaration instead of var (ES6 - V8 engine))
function logic() {
const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const activeCell = ss.getActiveCell();
const activeCellValue = activeCell.getValue();
if (activeCell.getColumn() === 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama") {
switch(activeCellValue) {
case 'Yes':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('1');
break;
case 'Half':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('1/2');
break;
case 'No':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('0');
break;
}
}
}
This way you only have to test the common conditions once.
Using the Switch function clearly shows the behavior of the script depending on the input value 'ActiveCellValue'.
If you need that only one action resolve per run, you need to use else if to chain the statements:
if(statement){
Action
}else if (statement2){
Action2
}else if...

Redundant 'if' statement less

My code is as below
private fun validateInput(): Boolean {
if (etReportRow1.text.toString() == ""
|| etReportRow2.text.toString() == ""
|| etReportRow3.text.toString() == "")
return false
else
return true
}
The compiler tell me
Redundant 'if' statement less... (Ctrl+F1) This inspection reports if
statements which can be simplified to single statements. For example:
if (foo()) { return true } else { return false } can be
simplified to return foo().
Won't the suggested code go into loop?
All statments in the form:
if(condition){
return false
} else {
return true
}
can be simplified into:
return !condition
So in your case it would lead to:
return !(etReportRow1.text.toString() == "" || etReportRow2.text.toString() == "" || etReportRow3.text.toString() == "")
Or:
return
etReportRow1.text.toString().isNotEmpty() &&
etReportRow2.text.toString().isNotEmpty() &&
etReportRow3.text.toString().isNotEmpty()
Note: the isNotEmpty() is an extension method:
public inline fun CharSequence.isNotEmpty(): Boolean = length > 0
To avoid duplicate code you could also use a Sequence:
public fun validateInput() = sequenceOf(etReportRow1, etReportRow2, etReportRow3)
.map { it.text.toString() }
.all { it.isNotEmpty() }
Because a boolean expression evaluates to a boolean value, you can simply return the result of the expression itself, without having to explicitly return true or false.
You can further simplify things using the following single-expression function:
private fun validateInput() = etReportRow1.text.toString() != "" &&
etReportRow2.text.toString() != "" &&
etReportRow3.text.toString() != ""
try
return !(etReportRow1.text.toString() == "" || etReportRow2.text.toString() == "" || etReportRow3.text.toString() == "")
I think this should work:
private fun validateInput() = !(etReportRow1.text.toString.isEmpty()
|| etReportRow2.text.toString().isEmpty()
|| etReportRow3.text.toString().isEmpty() )
Even more concise:
public fun validateInput() = setOf(
etReportRow1, etReportRow2, etReportRow3
).none {
"${it.text}".isEmpty()
}

UnityScript If(var="this" OR "that"){//do something}

I'm not sure how to make the following code work:
if(response!==("usernamewrong" OR "passwordwrong")){
print("login Wrong");
} else {
//if anything else other than the two shows up into the response goes here
}
You have to be explicit when doing multiple checks in a conditional:
if (response == "usernamewrong" || response == "passwordwrong")

How can I get which part of an if expression is true?

Assume I have code like:
if(condition1 || condition2 || condition 3 || condition4)
{
// this inner part will be executed if one of the conditions is true.
// Now I want to know by which condition this part is executed.
}
I'm sure there are better ways to do this, here's one:
int i = 0;
auto check = [&i](bool b)->bool
{
if (!b) ++i;
return b;
};
if (check(false) || // 0
check(false) || // 1
check(true) || // 2
check(false)) // 3
{
std::cout << i; // prints 2
}
|| is short circuit evaluation, so you can have code like this :
if(condition1 || condition2 || condition 3 || condition4)
{
if (condition1 )
{
//it must be condition1 which make the overall result true
}
else if (condition2)
{
//it must be condition2 which make the overall result true
}
else if (condition3)
{
//it must be condition3 which make the overall result true
}
else
{
//it must be condition4 which make the overall result true
}
// this inner part will executed if one of the condition true. Now I want to know by which condition this part is executed.
}
else
{
}
If the conditions are independent of each other, you need to check them separately, or, if they belong to one variable, you can use a switch statement
bool c1;
bool c2
if ( c1 || c2 )
{
// these need to be checked separately
}
int i; // i should be checked for multiple conditions. Here switch is most appropriate
switch (i)
{
case 0: // stuff
break;
case 1: // other stuff
break;
default: // default stuff if none of the conditions above is true
}
Without a switch you can use only or and if statements:
if(condition1 || condition2 || condition 3 || condition4) {
// this inner part will executed if one of the condition true.
//Now I want to know by which condition this part is executed.
if ( condition1 || condition2 ) {
if ( condition1 )
printf("Loop caused by 1");
else
printf("Loop caused by 2");
else
if ( condition3)
printf("Loop caused by 3");
else
printf("Loop caused by 4");
}
I'm not sure that this is the most efficient thing you've ever seen, but it will identify which of the four conditions caused entry into the if ... block.
If you need to know for programmatic reasons, i.e. run different code depending on which condition is true, you could do something like this
if (condition1)
{
...
}
else if (condition2)
{
...
}
else if (condition3)
{
...
}
else if (condition4)
{
...
}
else
{
...
}
If you only want to know for debugging reasons, just do a printout.
What about the comma operator?
By using that logical operators follow the short circuit evaluation method, the following works fine:
int w = 0; /* w <= 0 will mean "no one is true" */
if ( (w++, cond1) || (w++, cond2) || ... || (w++, condN) )
printf("The first condition that was true has number: %d.\n", w);