Skip to main content
GameDev.net gamedev.net
🔒 Locked

OOP and double loops

Started by Lode Feb 17, 2005 at 3:40 PM 56 replies 3.7k views
Original Post
Lode
Lode
So I have this course "Information and Program Structures" and it's basicly another one of those courses involving Java and OOP, and with one of those Extremely Hardcore Object Oriented Programming professors. So he said: "A double loop is UNREADABLE! you make a seperate class for the outside and inside loop". In other words, he finds this unreadable:

for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
{
    drawPixel(x, y, image[x][y]);
}
and wants instead to see two classes: one for the first loop, and one for the second loop!!! WTF is up with that? Has anyone ever heard that a double loop is unacceptable in OOP and should be split into two different classes? Can you imagine how FREAKING SLOW this would be for the inner loop? That would make OOP programs 100 times slower than non OOP ones, and that certainly isn't the case! OOP professers FRUSTRATE THE HELL OUT OF ME.
Samith
Samith
I'm not even sure how you would make a whole new class just for a loop. I don't know Java either, though, so that might be why I have no idea what your professor is talking about.

I don't see anything wrong with a double loop though, I use them all the time in C++.
smart_idiot
smart_idiot
Your teacher is an idiot, but you'd better do as he says anyway so you don't fail.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
owl
owl
make the two damn classes for him, tell him how great he is, and never ever do that again.
[size="2"]I like the Walrus best.
Trap
Trap
In theory there doesn't have to be any performance difference between using procedural code and using classes. In practice it depends on the programming language and it's implementation.

In general it is a good idea to look at double loops to find stuff that should be classes, but in this case it is horrible overengineering.
smart_idiot
smart_idiot
for(int x = 0, y = 0; y < height; ++x == width ? ++y, x = 0 : 0)
drawPixel(x, y, image[x][y]);


Well, it works in C++. There we go, one simple readable line of code. Sort of. Well not at all, but it's only one loop so why should he complain?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Raduprv
Raduprv
LOL
That teacher IS an idiot. I usually try to not have more than 2 nested loops, but sometiems I have 3, maybe even nested with IFs. If the code is alligned properly, and commented, then I don't think anyone would have problems reading it.
nprz
nprz
Quote:
Original post by Lode
So I have this course "Information and Program Structures" and it's basicly another one of those courses involving Java and OOP, and with one of those Extremely Hardcore Object Oriented Programming professors.

So he said: "A double loop is UNREADABLE! you make a seperate class for the outside and inside loop".

In other words, he finds this unreadable:

for(int x = 0; x < width; x++)for(int y = 0; y < height; y++){    drawPixel(x, y, image[x][y]);}


and wants instead to see two classes: one for the first loop, and one for the second loop!!!

WTF is up with that? Has anyone ever heard that a double loop is unacceptable in OOP and should be split into two different classes? Can you imagine how FREAKING SLOW this would be for the inner loop? That would make OOP programs 100 times slower than non OOP ones, and that certainly isn't the case!

OOP professers FRUSTRATE THE HELL OUT OF ME.


You should make it more confusing and get rid of the inner loop and just have one big loop.
like
for (int i + 0; i < width * height; ++i)     drawPixel(i % width, (i * width) + height, image[i % width][i * width + height]);


There might be a bad calculation, but basically you can make it the same iterations as two loops but write it correctly with one.

-edit- jesus smart_idiot, that code is hard to read :)
Evil Steve
Evil Steve
I think this is *slightly* unreadable:
for(int x = 0; x < width; x++)for(int y = 0; y < height; y++){    drawPixel(x, y, image[x][y]);}

But this is fine:
for(int x = 0; x < width; x++)   for(int y = 0; y < height; y++)   {       drawPixel(x, y, image[x][y]);   }

Or, better, this:
for(int x = 0; x < width; x++){   for(int y = 0; y < height; y++)   {       drawPixel(x, y, image[x][y]);   }}



But there's no freaking way I'd ever use a class for the inner loop, unless the loop was more than 3 levels deep, or it made sense to do it that way anyway.
Coward
Coward
Quote:
Original post by smart_idiot
for(int x = 0, y = 0; y < height; ++x == width ? ++y, x = 0 : 0)
drawPixel(x, y, image[x][y]);

I'm seeing and I'm liking..
Don't Temp Fate..
smart_idiot
smart_idiot
I think you want i%width for x and i/width for y, nprz.

Now that I think of it, I think I could have used ++x == width && (++y, x = 0) and short circuit evaluation would have made it to the Right Thing™.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Nemesis2k2
Nemesis2k2
Quote:
I'm seing and I'm liking..

Really? When I look at that code, it makes me want to retreat into a small dark corner and sob. Maybe that's just a flashback to my childhood.
Trap
Trap
Whats so bad about image.draw() calling line.draw() calling pixel.draw()?

As stupid as your prof may explain it to you, there might be a reason behind doing so.
Evil Steve
Evil Steve
Quote:
Original post by Trap
Whats so bad about image.draw() calling line.draw() calling pixel.draw()?

As stupid as your prof may explain it to you, there might be a reason behind doing so.
I also like my programs to run as slow as possible :P. Actually, I don't have anything constructive to add.
Raduprv
Raduprv
Quote:
Original post by Trap
Whats so bad about image.draw() calling line.draw() calling pixel.draw()?

As stupid as your prof may explain it to you, there might be a reason behind doing so.


Because then you'd have 10 billion classes in your program, and it would take 2 days to compile.
Coward
Coward
Quote:
Original post by Trap
Whats so bad about image.draw() calling line.draw() calling pixel.draw()?

It just seems ilogical to me..

When I think "image" I think a 2 dimensionel array of pixels. Not an array of lines.
Don't Temp Fate..
smart_idiot
smart_idiot
for(int y = 0; y < height; y++)
for(int x = 0; x < width; x++)


might be easier on the cache than

for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Raduprv
Raduprv
Quote:
Original post by smart_idiot
for(int y = 0; y < height; y++)
for(int x = 0; x < width; x++)


might be easier on the cache than

for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)


Indeed. It can be orders of magnitude faster to do it in the proper way, else you get huge cache penalties if the width is big.
darookie
darookie
Quote:
Original post by Coward
Quote:
Original post by Trap
Whats so bad about image.draw() calling line.draw() calling pixel.draw()?

It just seems ilogical to me..

When I think "image" I think a 2 dimensionel array of pixels. Not an array of lines.

Nah! That's not an argument at all - image → array of scanlines, scanline → array of pixels; at least that's how my monitor 'sees' an image (ok, actually not, but sort of [smile]).
Trap
Trap
Quote:
Original post by Coward
When I think "image" I think a 2 dimensionel array of pixels. Not an array of lines.

Valid point in this case.

Still you have a lot of assumptions and unnecessary overspecification in this code. 1. You assume the image is loaded into a 2d-array, 2. You assume it to be rectangular, 3. You specify an order in which the pixels are drawn which is completely unnecessary.

image.draw() is way better. All this bad stuff is then contained into a single place where you can easily change it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.