Classes, Good or bad?
Author |
Message |
mirhagk
|
Posted: Fri Dec 04, 2009 12:09 pm Post subject: Classes, Good or bad? |
|
|
I recently wondered what the point of classes is. To demonstrate my point let's look at using classes vs normal types and procedures. We're going to use a snow flake engine (very simple but it serves the point)
Turing: |
class snowflake
export create, fall, draw
var x, y : real %the position of the snowflake
procedure create %moves the snowflake to the top of the screen
x := Rand.Int (0, maxx)
y := maxy
end create
procedure fall %the snowflake falls randomly
y - = Rand.Real * 3
end fall
procedure draw %draws the snowflake
drawfilloval (round (x ), round (y ), 3, 3, white)
end draw
end snowflake
colourback (black) %makes the background black so you can see it
cls
var snow : array 1 .. 100 of pointer to snowflake %creates 100 snowflakes
for i : 1 .. 100
new snowflake, snow (i ) %actually creates the object
end for
for i : 1 .. 100
snow (i ) -> create %creates all the snowflakes
end for
loop %main loop
for i : 1 .. 100
snow (i ) -> fall %randomly falls a bit
snow (i ) -> draw %draws the snowflake
end for
Time.DelaySinceLast(20)
cls %clears the screen
end loop
|
could be a lot better but let's look at the exact same program using types
Turing: |
module Snow
export snowflake, create, fall, draw
type snowflake :
record
x, y : real
end record
function create : snowflake
var temp : snowflake
temp.x := Rand.Int (0, maxx)
temp.y := maxy
result temp
end create
function fall (flake : snowflake ) : snowflake
var temp := flake
temp.y - = Rand.Real * 3
result temp
end fall
procedure draw (flake : snowflake )
drawfilloval (round (flake.x ), round (flake.y ), 3, 3, white)
end draw
end Snow
colourback (black)
cls
var snow : array 1 .. 100 of Snow.snowflake %creates 100 snowflakes
for i : 1 .. 100
snow (i ) := Snow.create %creates all dem snowflakes
end for
loop %main loop
for i : 1 .. 100
snow (i ) := Snow.fall (snow (i )) %the snow flake falls a bit
Snow.draw (snow (i )) %draws the snowflake
end for
Time.DelaySinceLast (20)
cls
end loop
|
the engine part is even in a module for reuse
Now can someone tell me the advantage of using classes, if there is one.
To my understanding creating two hundred variables and three procedures (with types) is better than two hundred variables and three hundred procedures. Not sure if that actually makes a difference but even if it doesn't, it's still easier to do the second way because you don't need to understand pointers and things.
Please correct me if I'm wrong. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
Posted: Fri Dec 04, 2009 1:28 pm Post subject: RE:Classes, Good or bad? |
|
|
Classes let you structure your code in a better way and gives you encapsulation. |
|
|
|
|
|
DemonWasp
|
Posted: Fri Dec 04, 2009 1:58 pm Post subject: RE:Classes, Good or bad? |
|
|
Creating a new instance of a class shouldn't create a new "instance" of its procedures. Those procedures are compiled to operate on a supplied pointer to the data objects of the class itself in any reasonable implementation (no guarantees that Turing is reasonable).
The big advantage of classes that you're missing out on is polymorphism. Instead of snowflakes, let's assume you're looking at vehicles. Well, some vehicles fly, some drive, and some float on water. Naturally, they'll behave differently and have different information to store. With classes, you would have a base class called Vehicle, then one for FlyingVehicle / DrivingVehicle / etc. With modules, there's no way to do that cleanly. |
|
|
|
|
|
Superskull85
|
Posted: Fri Dec 04, 2009 2:22 pm Post subject: Re: Classes, Good or bad? |
|
|
To expand on what DemonWasp mentioned you could have a pointer to the base class Vehicle, but make an instance for FlyingVehicle/DrivingVehicle/etc. and than pass those pointers into the same method easily. For example an expansion of your class example could be:
Turing: | class particle
export create, fall, draw
var x, y : real %the position of the particle
procedure create %moves the particle to the top of the screen
x := Rand.Int (0, maxx)
y := maxy
end create
deferred procedure fall
deferred procedure draw
end particle
class snowflake
inherit particle
body procedure fall %the snowflake falls randomly
y - = Rand.Real * 3
end fall
body procedure draw %draws the snowflake
drawfilloval (round (x ), round (y ), 3, 3, white)
end draw
end snowflake
class rain
inherit particle
body procedure fall %the rain falls randomly faster
y - = Rand.Real * 5
end fall
body procedure draw %draws the rain smaller than a snowflake
drawfilloval (round (x ), round (y ), 2, 2, white)
end draw
end rain
colourback (black) %makes the background black so you can see it
cls
var particles : array 1 .. 100 of pointer to particle %creates 100 particles
for i : 1 .. 100
if Rand.Int (1, 2) = 1 then
new snowflake, particles (i ) %creates a snowflake particle
else
new rain, particles (i ) %creates a snowflake particle
end if
end for
for i : 1 .. 100
particles (i ) -> create %creates all the particles
end for
loop %main loop
for i : 1 .. 100
particles (i ) -> fall %randomly falls a bit
particles (i ) -> draw %draws the particles
end for
Time.DelaySinceLast(20)
cls %clears the screen
end loop |
How could you easily/cleanly do this with modules and types? What if you wanted rain to have additional methods that a basic particle does not have? |
|
|
|
|
|
|
|