I have a method inside a class that is not returning a value.
These are the relevant variables that are being used in the method, they are in the private part of the class
int hieght_of_plane = 0 ;
int position_of_plane = 0 ;
bool landing_gear = true ;
bool is_flying = false ;
bool is_alive = true ;
And here is the method that is not returning true or false, it is in the public section of the class.
bool check_for_alive()
{
if (is_flying == false)
{
return true ;
}
if (is_flying == true)
{
if (hieght_of_plane <= 3)
{
if (landing_gear == false)
{
is_alive = false ;
return false ;
}
else if (landing_gear == true)
{
return true ;
}
}
}
}
The method seems to not do anything and then I get the no return value error
warning: control reaches end of non-void function [-Wreturn-type]
I assume that it should return false in this instance because is_flying = false so the method should return false, but it isn't.
The case that falls through is when flying == true and hieght_of_plane is greater than 3.
if (is_flying == true)
{
if (hieght_of_plane <= 3)
{
The 2nd if statement does not have an else, which is necessary for returning a value.
Edit 1: An else clause
The else clause should be used for the "not" case of the if statement.
For example:
if (is_flying == false)
{
return true;
}
else // This means is_flying == true
{
if (hieght_of_plane <= 3)
{
if (landing_gear == false)
{
is_alive = false ;
return false ;
}
else // implies landing_gear == true
{
return true ;
}
else // implies hieght > 3
{
return ????
}
}
You could also reduce this down to one Boolean expression.
Edit 2: Simplification
There are two functionalities here: Return true/false for a combination of conditions and to set is_alive based on a condition.
Let's say that one condition is true, all others is false.
bool check_for_alive()
{
if (is_flying && (height_of_plane <= 3) && (landing_gear == false))
{
is_alive = false;
}
return is_flying && (height_of_plane <= 3) && (landing_gear == true);
}
Note: The above does not consider the case of `(height_of_plane > 3), since this is not specified in the OP's original code.
the control flow would reach the end of the function when is_flying is true and hieght_of_plane is > 3. you MUST return something there.
and one more thing is the you don't always need to check that if (something == true) instead if (something) would enough, I've pointed some more below the code.
bool check_for_alive() {
if (!is_flying)
return true;
// if you reached here is_flying is true no need to check
if (hieght_of_plane <= 3) {
if (!landing_gear) {
is_alive = false;
return false;
} else {
// if you're here landing_gear is false no need to check
return true;
}
}
// what to return if we reach here? yes the control flow could reach
// here when is_flying is true and hieght_of_plane is > 3
// you must return something here.
}
Related
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;
}
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()
}
After watching carefully the following code I can't see why the compiler is warning me with "warning: control reaches end of non-void function".
bool Foam::solidMagnetostaticModel::read()
{
if (regIOobject::read())
{
if (permeabilityModelPtr_->read(subDict("permeability")) && magnetizationModelPtr_->read(subDict("magnetization")))
{
return true;
}
}
else
{
return false;
}
}
I can't see where is the problem, the else statement should care for returning false in every case which the first if is not true.
Trace the code path when regIOobject::read() is true, but either of permeabilityModelPtr_->read(subDict("permeability")) or magnetizationModelPtr_->read(subDict("magnetization")) is false. In that case, you enter the top if block (excluding the possibility of entering its attached else block), but then fail to enter the nested if block:
bool Foam::solidMagnetostaticModel::read()
{
if (regIOobject::read())
{
// Cool, read() was true, now check next if...
if (permeabilityModelPtr_->read(subDict("permeability")) && magnetizationModelPtr_->read(subDict("magnetization")))
{
return true;
}
// Oh no, it was false, now we're here...
}
else
{
// First if was true, so we don't go here...
return false;
}
// End of function reached, where is the return???
}
The minimalist fix is to just remove the else { } wrapping, so any fallthrough ends up at return false;:
bool Foam::solidMagnetostaticModel::read()
{
if (regIOobject::read())
{
// Cool, read() was true, now check next if...
if (permeabilityModelPtr_->read(subDict("permeability")) && magnetizationModelPtr_->read(subDict("magnetization")))
{
return true;
}
// Oh no, it was false, now we're here...
}
// Oh, but we hit return false; so we're fine
return false;
}
Alternatively, avoid specifically mentioning true or false at all, since your function is logically just a result of anding three conditions together:
bool Foam::solidMagnetostaticModel::read()
{
// No need to use ifs or explicit references to true/false at all
return regIOobject::read() &&
permeabilityModelPtr_->read(subDict("permeability")) &&
magnetizationModelPtr_->read(subDict("magnetization"));
}
The nested if is the problem.
When that branch is not taken, there is no other paths to take
the else statement should care for returning false in every case which the first if is not true.
Correct, but what if the first if condition is true, but the second if condition is not?
That is: What if regIOobject::read() returns true, but permeabilityModelPtr_->read(subDict("permeability")) returns false?
Then the flow of control enters the first if block, does not return, but does not enter the else block (because the first condition was true), so it just falls off the end of the function without hitting a return statement.
If you want the else { return false; } part to apply to either condition, you could just naively copy/paste it:
if (COND1) {
if (COND2) {
return true;
} else {
return false;
}
} else {
return false;
}
But that's quite a bit of code duplication. A better solution is to replace the nested if by a single condition:
if (COND1 && COND2) {
return true;
} else {
return false;
}
There's still some duplication: Both branches consist of a return statement followed by some expression.
We can factor out the common parts (return) and push the condition into the expression:
return COND1 && COND2 ? true : false;
But ? true : false is redundant: If the condition is true, evaluate to true, else evaluate to false? Well, that's just what the condition itself does:
return COND1 && COND2;
Or with your concrete expressions:
return regIOobject::read()
&& permeabilityModelPtr_->read(subDict("permeability"))
&& magnetizationModelPtr_->read(subDict("magnetization"));
I'm sure you've been there. You want to say "if flib do this, if flob do that, if flab do diet, etc" where any number of them can be true, then at the end you want an "if you didn't do ANY of them".
For example (the examples below are in Swift, as I've been playing with it, but I think the situation is the same in most languages):
let thing = 101
var isInteresting = false
if (thing % 3 == 0) {
println("\"\(thing)\" is a multiple of three.")
isInteresting = true
}
if (thing > 100) {
println("\"\(thing)\" is greater than one hundred.")
isInteresting = true
}
if (thing > 1000) {
println("\"\(thing)\" is greater than one thousand.")
isInteresting = true
}
if !isInteresting {
println("\"\(thing)\" is boring.")
}
I find keeping track of a boolean to tell me whether I did anything or not kinda ungainly.
The only other way I came up with was this:
let thing = 101
let isAMultipleOfThree = (thing % 3 == 0)
let isGreaterThan100 = (thing > 100)
let isGreaterThan1000 = (thing > 1000)
if isAMultipleOfThree {
println("\"\(thing)\" is a multiple of three.")
}
if isGreaterThan100 {
println("\"\(thing)\" is greater than one hundred.")
}
if isGreaterThan1000 {
println("\"\(thing)\" is greater than one thousand.")
}
if !(isAMultipleOfThree || isGreaterThan100 || isGreaterThan1000 ) {
println("\"\(thing)\" is boring.")
}
but if anything that's worse (if you add a new clause you need to remember to add it in three places.
So my question is, is there a neat, succinct way of doing this?
I'm dreaming of an imaginary switch-like statement:
switchif { //Would have fallthrough where every case condition is checked
case thing % 3 == 0:
println("\"\(thing)\" is a multiple of three.")
case thing >100 :
println("\"\(thing)\" is greater than one hundred.")
case thing > 1000:
println("\"\(thing)\" is greater than one thousand.")
none: //Unlike 'default' this would only occur if none of the above did
println("\"\(thing)\" is boring.")
}
It's a good question that does not have a perfect answer. However, here's one other idea in addition to those you suggest: Encapsulate the testing machinery in a procedure to allow the calling code at least to be a bit more streamlined.
Specifically, for your example, the calling code can be this:
if (! doInterestingStuff(101)) {
println("\"\(thing)\" is boring.");
}
If testing is encapsulated into a procedure:
public boolean doInterestingStuff(int thing) {
var isInteresting = false
if (thing % 3 == 0) {
println("\"\(thing)\" is a multiple of three.")
isInteresting = true
}
if (thing > 100) {
println("\"\(thing)\" is greater than one hundred.")
isInteresting = true
}
if (thing > 1000) {
println("\"\(thing)\" is greater than one thousand.")
isInteresting = true
}
return isInteresting
}
I'm not sure how you'd do this in Swift, but since you didn't give a language tag I'll answer in C++.
The key to this is that && is short circuiting, and the second part won't be evaluated when the first part is false. It's the same idea as your boolean flag, but it's a little more automated.
struct Tracker
{
Tracker() : any(false) { }
bool operator()() { any = true; return true; }
bool any;
};
int thing = 101;
Tracker tracker;
if (thing % 3 == 0 && tracker()) {
printf("\"%d\" is a multiple of three.\n", thing);
}
if (thing > 100 && tracker()) {
printf("\"%d\" is greater than one hundred.\n", thing);
}
if (thing > 1000 && tracker()) {
printf("\"%d\" is greater than one thousand.\n", thing);
}
if (!tracker.any) {
printf("\"%d\" is boring.\n", thing);
}
See it in action: http://ideone.com/6MQYY2
kjhughes' answer inspired me a little:
Perhaps one could write a global function that accepts an indeterminate number of key-value pairs (or even just two element arrays), where the key is a comparison and the value is the statement to run if it's true. Then return false if none of them were run, otherwise true.
Update:
Tried it, it's horrible!
//Function:
func ifNone(ifNoneFunc:()->Void, tests: Bool...)
{
var oneTestPassed = false
for test in tests
{
oneTestPassed |= test
}
if(!oneTestPassed)
{
ifNoneFunc()
}
}
//Example:
let thisThing = 7
ifNone(
{
println("\(thisThing) is boring")
},
{
if(thisThing % 10 == 0)
{
println("\"\(thisThing)\" is a multiple of 10")
return true
}
else
{
return false
}
}(),
{
if(thisThing % 3 == 0)
{
println("\"\(thisThing)\" is a multiple of 3")
return true
}
else
{
return false
}
}(),
{
if(thisThing > 1_000_000)
{
println("\"\(thisThing)\" is over a million!!")
return true
}
else
{
return false
}
}()
)
I am asked to make a maze game in c++ (using codeblocks). I figured out most of it, but stuck in one method of Maze class. I have this function to say that travel in anyone direction (up, down, left, right) where you dont get the wall.
int Maze::mazeTraversal(int a, int b)
{
// If a,b is outside maze, return false.
if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) return FALSE;
// If a,b is the goal, return true.
if ( maze[b][a] == 'G' ) return TRUE;
// If a,b is not open, return false.
if ( maze[b][a] != '0' && maze[b][a] != 'S' ) return FALSE;
// Mark a,b part of solution path.
maze[b][a] = 'x';
// If find_path North of a,b is true, return true.
if ( mazeTraversal(a, b - 1) == TRUE ) return TRUE;
// If find_path East of a,b is true, return true.
if ( mazeTraversal(a + 1, b) == TRUE ) return TRUE;
// If find_path South of a,b is true, return true.
if ( mazeTraversal(a, b + 1) == TRUE ) return TRUE;
// If find_path West of a,b is true, return true.
if ( mazeTraversal(a - 1, b) == TRUE ) return TRUE;
// Unmark a,b as part of solution path.
maze[b][a] = '0';
return FALSE;
}
I am calling this function as:
Maze mo(maze,12); //creating maze game with 12/12 array
mo. mazeTraversal(0,2) // because the entry point is in 0,2 position of the game.
I just realised that I am asked to have this mazeTraversal as void. No any return. My mind is blowing up. Excepting some creative ideas please.
Use:
void Maze::mazeTraversal(int a, int b, bool& Status)
and then instead of return inside function use :-
Status = false; // or true
or use a data member bool Status inside class and update its value
class Maze{
public :
bool Status;
//..
void Maze::mazeTraversal(int a, int b);
//...
};
If your request states that the signature of the function must not be modified, the signature does not include the return type, therefore, you can change the return type.
If the assignment does not tell anything about the signature there are the solutions already posted upstream:
add a by reference param (not a pointer, that is cumbersome and really not needed)
add an internal state that you maintain and return in some "Status" function.
These are the solution I would investigate.
Either way you'd have to adapt the implementation of the traverse function.
As it seems that your Maze object is stateful (i.e.: does maintain some internal state as to what position it arrived to), the second makes more sense.
Here is another solution that does not change your function signature yet it does not return anything at all:
bool g_Status = true;
void Maze::mazeTraversal(int a, int b)
{
if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) // If a,b is outside maze, return false.
g_Status=false;
else if ( maze[b][a] == 'G' ) // If a,b is the goal, return true.
g_Status=true;
else if ( maze[b][a] != '0' && maze[b][a] != 'S' ) // If a,b is not open, return false.
g_Status=false;
else{
maze[b][a] = 'x'; // Mark a,b part of solution path.
mazeTraversal(a, b - 1);
if (!g_Status) // If find_path North of a,b is true, return true.
mazeTraversal(a + 1, b); // If find_path East of a,b is true, return true.
if (!g_Status)
mazeTraversal(a, b + 1); // If find_path South of a,b is true, return true.
if (!g_Status)
mazeTraversal(a - 1, b); // If find_path West of a,b is true, return true.
if (!g_Status)
maze[b][a] = '0'; // Unmark a,b as part of solution path.
}
}
You may write your code like this:
void Maze::mazeTraversal(int a, int b,bool* retValue)
{
// If a,b is outside maze, *retValue = false.
if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) {
*retValue= FALSE;
return;
}
// If a,b is the goal, return true.
if ( maze[b][a] == 'G' ) {
*retValue= TRUE;
return;
}
// If a,b is not open, return false.
if ( maze[b][a] != '0' && maze[b][a] != 'S' ) {
*retValue= FALSE;
return;
}
// Mark a,b part of solution path.
maze[b][a] = 'x';
// If find_path North of a,b is true, return true.
bool* ret1;
mazeTraversal(a + 1, b,ret1)
if ( *ret1 == TRUE ) {
*retValue= TRUE;
return;
}
// If find_path East of a,b is true, return true.
bool* ret2;
mazeTraversal(a + 1, b,ret2);
if ( *ret2 == TRUE ) {
*retValue= TRUE;
return;
}
// If find_path South of a,b is true, return true.
bool* ret3;
mazeTraversal(a, b + 1,ret3);
if ( *ret3 == TRUE ) {
*retValue= TRUE;
return;
}
// If find_path West of a,b is true, *retValue= true.
if ( mazeTraversal(a - 1, b) == TRUE ) {
*retValue= TRUE;
return;
}
// Unmark a,b as part of solution path.
maze[b][a] = '0';
*retValue= FALSE;
}
Now create a global variable:
bool* retvalue;
This variable will always hold the return value, you dont need to return from function