Get rid of useless return statement - loops

I am trying to refactor some code and make it easier to read. I noticed that I have some unnecessary return statements at the end of some functions. Here a conceptual example:
func someFunction(a []arr) int {
for _,v := range a {
if v == something {
// will defenitly get here at some point!
return somethingElse
}
}
return -1 // never ever happens!
}
In my opinion the return statement at the end of the function is misleading, because it suggests, that it may be reached at some point. How do I prevent it?
Please note, that I do error handling at some other point, which is why I can be sure, that someFunction will always return somethingElse.

Panic instead of returning fake value at the end of a function:
func someFunction(a []arr) int {
for _,v := range a {
if v == something {
// will defenitly get here at some point!
return somethingElse
}
}
panic("unreachable")
}
This is a common pattern in standard library.

Related

Would like to stop execution of code when a match is met

I created this service method in Angular JS which checks if an array of potential statuses(pendingApplications) match any of an array of set statuses(applicableStatuses). For this to work it meetsStatusCondition should return true after the first match occurs. Only 1 of the numbers in pendingApplications array needs to match and I'd like to end the execution of this function. Currently it's looping through every item in pendingApplications array
`containsApplicableStatus: function(pendingApplications, applicableStatuses) {
pendingApplications.forEach(function(status) {
if (applicableStatuses.includes(status)) {
return pendingApplications.meetsStatusCondition = true;
}
});
}`
This is a limitation with .forEach, you can't break out if it like you can with a for loop
Just a regular for loop will work
for (const status of applicableStatuses){
if (applicableStatuses.includes(status)) {
pendingApplications.meetsStatusCondition = true;
break //or return if you want to exit out of the enclosing function instead of just the loop
}
}
Often when you want to short-circuit a forEach like this, what you're really looking for is another method like find() or some().
containsApplicableStatus: function(pendingApplications, applicableStatuses) {
pendingApplications.meetsStatusCondition = pendingApplications.some(function(status) {
return applicableStatuses.includes(status)
});
}
There is no point in using forEach (which doesn't have a breaking option) if you could just use a regular for ... of loop instead:
containsApplicableStatus: function(pendingApplications, applicableStatuses) {
for (const status of pendingApplications) {
if (applicableStatuses.includes(status)) {
pendingApplications.meetsStatusCondition = true;
break;
}
}
}
However, even this seems a bit too complicated, you could just set meetsStatusCondition to the result of some:
containsApplicableStatus: function(pendingApplications, applicableStatuses) {
pendingApplications.meetsStatusCondition =
pendingApplications.some(status => applicableStatues.includes(status));
}
I do wonder if it makes sense to set a non-index property on your array though, maybe rethink that. This works but it's usually not something you'd expect on an array, and it will be lost if you convert that array to JSON for instance.

What is an idiomatic way of looping with a match when I only care about one variant and not even the value in the variant?

loop {
match stream.write(&buffer) {
Ok(_) => break,
_ => {}
}
}
Is there a way to write it on a more readable/idiomatic way? I don't need to do anything inside the match, I don't need any actions after the Ok or after the Err, because the buffer is updated as a reference whenever it returns Ok.
Something like:
while match some_fn() != Ok() {
...
}
The general case is covered by ForceBru's answer, but since you have a Result and don't care about the value inside Ok, you can use Result::is_ok (or Result::is_err):
while stream.write(&buffer).is_err() {}
As pointed out by rodrigo in the comments:
Worth noting that Rust 1.42 adds the matches! macro, so you can write while matches!(stream.write(&buffer), Ok(_)) {}, useful for other enums that lack the is_* functions.
You can use while let:
fn some_fn(x: u32) -> Option<u32> {
if x > 30 {
Some(x)
} else {
None
}
}
fn main() {
let mut x = 0;
while let None = some_fn(x) {
x += 1;
}
dbg!(x);
}
Playground
Same with the Result type: check if some_fn returned Err.

Sum in recursive function

I have a recursive function which I call acc. If a specific condition is fulfilled I call the function again. If not, I want do add a number to the variable a.
In my opinion it does not what it should. Can someone have a look on this:
double acc(v)
{
double a = 0;
for(int q=0; q<v; q++)
{
if(bf(q) < 1)
{
if(ef() == 0)
{
a += cf();
}
else
{
a += df();
}
}
else
{
return a += acc(v);
}
}
return a;
}
I tried to simplify it as good as I can. vis a variable. bf(), cf(), ef() and df() are functions which return an integer value. Now I want that a gets incremented every time a specific condition is fulfilled during the whole recursive process. Does my code what I want? I don't see it at the moment.
Your problem is that a is defined inside the recursive function. If you want to count events inside the recursion, declare a outside of acc().

Kotlin: For-loop must have an iterator method - is this a bug?

I have the following code:
public fun findSomeLikeThis(): ArrayList<T>? {
val result = Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>
if (result == null) return null
return ArrayList(result)
}
If I call this like:
var list : ArrayList<Person>? = p1.findSomeLikeThis()
for (p2 in list) {
p2.delete()
p2.commit()
}
It would give me the error:
For-loop range must have an 'iterator()' method
Am I missing something here?
Your ArrayList is of nullable type. So, you have to resolve this. There are several options:
for (p2 in list.orEmpty()) { ... }
or
list?.let {
for (p2 in it) {
}
}
or you can just return an empty list
public fun findSomeLikeThis(): List<T> //Do you need mutable ArrayList here?
= (Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>)?.toList().orEmpty()
try
for(p2 in 0 until list.count()) {
...
...
}
I also face this problem when I loop on some thing it is not an array.
Example
fun maximum(prices: Array<Int>){
val sortedPrices = prices.sort()
for(price in sortedPrices){ // it will display for-loop range must have iterator here (because `prices.sort` don't return Unit not Array)
}
}
This is different case to this question but hope it help
This can also happen in Android when you read from shared preferences and are getting a (potentially) nullable iterable object back like StringSet. Even when you provide a default, the compiler is not able to determine that the returned value will never actually be null. The only way I've found around this is by asserting that the returned expression is not null using !! operator, like this:
val prefs = PreferenceManager.getDefaultSharedPreferences(appContext)
val searches = prefs.getStringSet("saved_searches", setOf())!!
for (search in searches){
...
}

Angular promises run conditionally

I'd like to perform an asynch function conditionally, but I think I'm missing the correct syntax to say what I want.
this.doUpToThreeThings = function(skipTheMiddleStep) {
return doFirstThing().then(function (result) {
if (skipTheMiddleStep) {
return doThirdThing();
} else {
return doSecondThing();
}
}).then(function (result) {
if (skipTheMiddleStep) {
return "ok"; // return what?
} else {
return doThirdThing();
}
});
}
By the time we get to the second then, I don't know if the first block did the middle step, so I'm forced into repeating the condition. And the second block reads weird: it should say, if skip the middle step, then do the third thing, but since it we know that the previous block must have done the third thing, it just returns. So I have to repeat the condition and write pretty wrong-looking code in the second then.
I realize I can write a function called doSecondAndThirdThings, and just call that from the condition in the first block, but that's not really DRY, it's just hiding the non-DRYness. (or maybe I'm wrong about that?)
Also, I'm still a little confused about returning a completed promise on that "ok" branch. Is that right how it is, or should I say something like resolve? -- Thanks
The deferred in thefourtheye's answer is pointless and is considered an anti pattern with promises.
Here is how I would do it:
this.doUpToThreeThings = function(skipTheMiddleStep) {
return doFirstThing().then(function (result) {
return (skipTheMiddleStep) ? doThirdThing() : doSecondThing().then(doThirdThing);
});
}

Resources