Suppose in my one particular piece of code, I am avoiding loops.
ie instead of providing 'for' loop to do the task , I am copying and pasting a part three or four times to make it simple (?)to code.

In this case what are the consequences of my action?

I feel that only problem is a increased code size - which is not an issue when GBs of Hard disk space is available. Is my understanding correct? Any other issues like more RAM consumption or increased execution time? or anything else?

Thanks
Roy Thomas

I suppose the main thing you are giving up is streamlined maintenance and scalability since you will need to manually copy and paste (or perhaps remove) segments of code anytime the size of the container gets changed.

Why are you avoiding loops?

Why are you avoiding loops?

I am not avoiding loops altogether - just to know what is the effect.
For example my payload coming from my USB data logger is 64 bytes in each transfer of which 16 is one record. So for clipping and arranging the data, i have to repeat a the same process 4 times. I can do it by adding a index variable to the array addressing, but I simply clipped the first 16 bytes according to my requirement and copied the same code three more times and just changed the array index values in the copied code to change the index of the source payload byte array.(Probably looping was more easy hi!).
At this time I thought I must know if any problem by doing this. That is all.

Thanks
Roy Thomas

As long as you are reusing the same variables for each block then it is essentially doing the same as a loop. As you said, the extra lines of code will increase your file size slightly.
As DdoubleD pointed out, the problem is more to do with readability and maintainance...if you need to make a change to the way you clip/arrange the data you will have to make that change in four places rather than one.
The real advantage of loops becomes apparent when you are working with a less predictable system. In your case you know that you need to repeat the action four times, but if the data could be anywhere between (for example) 8 and 64 bytes, you would find a loop much more streamlined and simpler.

Thank you friends.
The subject is clear.

Thank you all

Roy Thomas

The compilers optimiser should handle doing that for you actually - though you will never actualy see it unless you compile to MSLI or decompile the EXE.

In code i find there is a kind of 3-way tie off speed (getting from the top to bottom of the code fast), effeciency (adding conditions - which add to CPU time - which stop unneccicary code from running) and resources (say loading 100,000,000 strings into an array then sorting them - you kill your ram but you get the job done fast).

To be fare its the job of the optimiser to work of most of these details, and the example you give of making a static for loop into individual calls is exactly the sort of text-book example you'll find. So basicly, do what reads best in terms of code..... dont copy/paste 32 lines, leave them in a for loop because you know it will itterate 32 times (not 31 if you didnt press ^v enough), but it would just as silly to put a a for loop that itterates 2 times.

commented: Nice explanation:) +5

Thanks for the clarification.

Roy Thomas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.