Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it bad to call a function in the condition of a for loop?

I recently asked an Application Architect at work to review a php script I had written to automate some tasks I do weekly in our Help Desk department.

In his review he stated

## Loops

Your loops are good, you didn't do anything bad like calling functions in the condition

for ($i=0; $i < count($array); $i++); is BAD

Honestly, I've never been tempted to do that in my code before but it made me wonder why it would be bad.

I assume it's because, the result of a function might be any value at all and it seems like a perfect way to create an infinite loop and just in general cause unexpected behaviour.

I tried googling but couldn't find any relevant results so I ask:

Why is it bad to call a function in the condition of a for loop?

Note the count($array) in the comment itself is, to me, a gimme. Of course, you'd want to just cache that. More specifically, I mean in the context of using other more complex functions.

For those that will surely wonder "Why not just ask the guy who wrote it", he's super busy and already took the time to help me, I don't want to push that too far with "Now, can you please explain all of your comments to me?"

like image 375
Wesley Smith Avatar asked Feb 15 '16 07:02

Wesley Smith


People also ask

Can you call a function within a for loop?

"function" as a keyword is only used for defining functions, and cannot be used inside a loop (or inside an "if" or "switch" or other control statement.) The only kinds of functions that can be defined within loops are anonymous functions.

Can we call function inside for loop in C?

You should not define a function inside a loop because you'll encounter errors unless you add a check before the function declaration to see if the function already exists. if (menuSelect == 2); --> ; is not required in your case.

Can you put a function inside a for loop Javascript?

A function is just a set of instructions, so you could, theoretically, take any function's instructions and put them directly inside the loop, and you have essentially the same thing.

When should you use a function instead of a loop?

Just as a loop is an embodiment of a piece of code we wish to have repeated, a function is an embodiment of a piece of code that we can run anytime just by calling it into action. A given loop construct, for instance could only be run once in its present location in the source code.


2 Answers

for ($i=0; $i < count($array); $i++); //is not efficient

Yes, this is not efficient to use because in every iteration function being called which is not good. You need to execute this function one time.

$count = count($array);
for ($i= 0; $i < $count; $i++); //is much efficient

Because in this code count function will execute one time. In your previous code count function execute multiple times.

like image 145
Md. Sahadat Hossain Avatar answered Nov 04 '22 15:11

Md. Sahadat Hossain


There are a couple of reasons I can think of:

  • The return value of a function can vary, so you cannot be certain your loop will remain finite
  • The return type is not guarenteed in some languages, so you have another opportunity for undefined behaviour
  • It is usually less performant (especially in the count() example)
  • It reduces readability, because a developer now needs to review and understand the function just to know how many times the loop should run
  • I cannot think of ANY legitimate examples of why you would actually need the return value of a function, every time the condition is evaluated
  • The number of iterations cannot be (easily) determined at run-time, because the function must be called again at the end of each iteration
like image 4
deed02392 Avatar answered Nov 04 '22 15:11

deed02392