473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

should i use a loop or write each repetition

guillermobytes
hi, i have a code that is repeated 3 times and i wonder if i better use a for loop or just leave it explicitly coded. here is the code:

Expand|Select|Wrap|Line Numbers
  1.     $partsCopy = $this->_parts;
  2.     $pos = array_search($newOrder[0], $this->_order);
  3.     $this->_parts[0] = $partsCopy[$pos];
  4.     $pos = array_search($newOrder[1], $this->_order);
  5.     $this->_parts[1] = $partsCopy[$pos];
  6.     $pos = array_search($newOrder[2], $this->_order);
  7.     $this->_parts[2] = $partsCopy[$pos];
or i should do this :

Expand|Select|Wrap|Line Numbers
  1. $partsCopy = $this->_parts;
  2. for ($i = 0; $i < 3; $i++) {
  3.     $pos = array_search($newOrder[$i], $this->_order);
  4.     $this->_parts[$i] = $partsCopy[$pos];
  5. }
i don't know which is the best approach since the count is so little and the code inside the loop too.
if you have any suggestions let me know
Mar 23 '10 #1
3 1246
Atli
5,058 Expert 4TB
Hey.

Ideally you would want to avoid hard-coding the number of repetitions needed. It allows you to dynamically change the output without having to change the code.

In your code, does the $newOrder have more than 3 elements? If the number of elements in $newOrder changed, would the code have to reflect that?
If so, the you might want to do something more like:
Expand|Select|Wrap|Line Numbers
  1. $partsCopy = $this->_parts;
  2. $count = count($newOrder);
  3. for ($i = 0; $i < $count; $i++) { 
  4.     $pos = array_search($newOrder[$i], $this->_order); 
  5.     $this->_parts[$i] = $partsCopy[$pos]; 
  6. }
Now the code would work no matter how many items $newOrder has.
Mar 24 '10 #2
Thanks Atli,
no the number of repetitions will never change.
the parts here are those of a Date (day, month, year) and the program does not care about time in minutes or anything like that, so i'm sure 100% that the parts will always be 3.

so given that the parts will always be 3 and that the repeated code is very short, is it better to use a for loop or just hard code the 3 repetitions?
Mar 24 '10 #3
Markus
6,050 Expert 4TB
You could, of course, skip the need to count the elements by using a foreach loop.

Expand|Select|Wrap|Line Numbers
  1. foreach ($this->_parts as $key => $data) {
  2.     // ...
  3. }
  4.  
Mar 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Wolfgang Buechel | last post by:
Hi, I want to iterate over all 2x2 matrices with elements in range 0..25 (crypto-stuff). To produce them, first I wrote a fourfold nested for loop: M=26 for a in range(M): for b in range...
0
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes...
9
by: Itay | last post by:
Hi all , Suppose i have structurs defines as follows : struct S1 { int n1 ; int n2 ; int n3 ; };
1
by: Chris Gamache | last post by:
For my particular case, word repetition shouldn't be relevant in determining the rank of a document. If I strip() the vector, I loose what relevance proximity and weight add to the rank. It seems...
17
by: caijunfu | last post by:
Is there a way to create a new array, and initialise every of them to 0, but maintain the order to this process to O(1)? (usually, we do initialisation by using for loop. that is of O(n).)
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
5
by: 2nervous | last post by:
I created the following function to change the visibility of nine different table cells. It appears to only execute the first instead of looping. How can I get it to loop through? function...
12
by: ryann18 | last post by:
1. Write a while loop that verifies that teh user enters a positive integer value. 2. Write a do loop that verifies that teh user enters an even integer value. 3. Write a for loop to print...
3
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
0
by: hennas | last post by:
Basically i want to design a membership Name and Telephone List form using the following command buttons. Edit Add New; Update; Delete; Cancel; Save; Clear, and Exit
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.