Is it poor technique to include inside the main loop?

Hello all,

I was wondering if it is bad to have an include in the main loop. I have several blocks of code that are a couple hundred lines each. I only need to call them once, so using a function would be unneeded IMO.

Here is an example of what I am talking about.

foo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() {

        while (true) {

                #include "bar.cpp"

        }

        return 0;

}


bar.cpp
 
std::cout << "foobar\n";


This code works, but I am not sure if it is good technique..

Thanks

EDIT:

Some compilers may need an 'ifndef once' block
Last edited on
It's not so much poor technique as a clumsy way of implementing pseudo-functions. Why not just have functions? The extra overhead really isn't going to kill you, especially if you only call them once. This is what functions are for.

You could use inlining if you want to give the compiler the option of this.
Last edited on
Yeah, I was thinking about using functions.. but I was reading an article that was basically saying if you only need to use a code block once, then it is better to just have the code directly in the place that it should be.

Seeing as I have never seen someone use an include in this way, I won't. But I will leave this thread open for a little longer just to see others' opinions.

but I was reading an article that was basically saying if you only need to use a code block once, then it is better to just have the code directly in the place that it should be.


Please post a link and let me know who publishes such nonsense.... o_O
+1 Moschops, rapidcoder.

Functions are good.

There is nothing wrong with having a function for a block of code that you call only once, and in fact a lot of times it can be very benefitial.
its not a back technique but its best if you do grouping in your program becos it gives clarity and understanding if any programmer caries the codes to read.
@rapidcoder -- sorry, can't find it...

Thanks for the input everyone.
If you only intend to use the code once then why not just put the code there rather than an #include?

This is exactly what functions are for.
@Galik what I was wondering is if that is bad technique or not, for every other program I've created I just used functions.. but some people say it is bad technique to use a function if it is only going to be used once. But my question was answered by other posters.
Topic archived. No new replies allowed.