Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Question for OOP disciples

Author
Message
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Oct 2003 21:59
Ok, fair enough

The place that I use classes mostly is in a pattern called RAII (Resource Allocation Is Initialisation). Basically, where-ever I have to get a resource of any kind I wrap a small class around it. That way when the class instance goes out of scope for any reason the resource gets released automatically.

The types of resource I'm talking about here are anything from allocated memory (the obvious one) to sockets, database connections, timers, windows, loaded dlls ... basically anything that is allocated and needs a shutdown/destruction process.

Another place I tend to use a class is for the singleton pattern. This is where you must have just a single instance of a class that is shared program wide, but it needs a shutdown/destruction process - The pointer received from the Direct3DCreate9 function is a good example of this. You only need one occurance of the return value in your program, and you need to ensure it is destroyed when your app terminates.

The third place I use classes is when I have to deal with data in an unknown sequence - this is where I use what is known as a class factory.

The traditional way to deal with this sort of thing is to have a way to determine the type of the data and put 'case' type structures everywhere that you need to deal with the data. This is error prone - miss one point when adding a new type and your code won't work correctly.

If you instead have a shared base class and a class for each data type, you can use your factory to create the correct class for the data in the first place and use dynamic casting to ensure that the actions that you want to take on that data are valid for that class.

That last one may need a bit (lot!) more explanation - if so, just ask
kingius
22
Years of Service
User Offline
Joined: 16th Oct 2002
Location:
Posted: 11th Oct 2003 19:06 Edited at: 11th Oct 2003 19:09
Its interesting reading the points of view of some of the inexperienced programmers on the forum about programming in general. There seems to be an opinion that the lower level the programming the better it is - in particular Im referring to this opinion that assembly is "better" than BASIC, and similarly that procedural code is "better" than OOP. Those of you that believe this have got it all the wrong way round.

Assembly is only good for one thing. Speed. Its a pain to develop anything in it because it takes so long to and is difficult to debug. Thats why compilers (and high level languages) were developed, so that we didnt waste weeks banging our heads against walls with low level bugs and could actually get on with making the program actually do something, which was the point in the first place.

Getting back on topic, OOP is far superior to simple procedural code. By considering the problem you are trying to solve in the design phase it is pretty easy to conceptualise the objects required in the solution that you will develop. These objects have a definite relationship with what you are trying to achieve, all of a sudden you are thinking out of the box - no longer in terms of the programming language or the underlying hardware.

DrakeX :
Quote: "i'd probably opt for three individual names, like PrintToScreen, PrintToDDS, and PrintToPrinter."


You would also end up with a program that is much harder to follow the more you want to do with those three example devices. Imagine if every device had 20 functions. I wonder if you see how it makes sense for those to belong to the objects and not the main program? What if some of those functions are called from other functions, and you do not want them to be executable from the main program? In OOP, you make them private to do this. Private functions and variables do not exist outside of the object itself and cannot be accessed outside of this scope. Free code security!
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 12th Oct 2003 06:42
@ DrakeX:

Quote: "as opposed to a program-in-a-class where you have "global" member variables and nothing but functions?"


forgive me but what are you saying?

-RUST-
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 16th Oct 2003 18:39
Ian:

Sorry i've been meaning to follow up, but time is rapidly becoming limited.

I'm certainly interested in hearing a bit more about how a class factory actually works. So if you have a little free time or some decent links, i'd appreciate it.

Kevin Picone
Play Basic - Visible Worlds - Kyruss II
[url]www.underwaredesign.com[/url]
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 16th Oct 2003 19:51
Kingius:


Quote: " Assembly is only good for one thing. Speed. Its a pain to develop anything in it because it takes so long to and is difficult to debug. Thats why compilers (and high level languages) were developed, so that we didnt waste weeks banging our heads against walls with low level bugs and could actually get on with making the program actually do something, which was the point in the first place."


Personally, one of the biggest myths perpetuated about assembly, is that it's difficult to debug. It's presented far too often as either a reason not use it, or that it's somehow inferior form of programming. This opinion seems to be created from a gross miss understand of the tools available, or the impression that coders write code in a 'line by line' fashion. They don't.



Quote: "You would also end up with a program that is much harder to follow the more you want to do with those three example devices. Imagine if every device had 20 functions."



Well that's semantics really. In both cases you end up with the example of 20 commands / functions.

i.e

PrintToScreen, PrintToDDS, and PrintToPrinter

Print.Screen, Print.DDS, Print.Printer

Both convey to the programmer their object/action. What can not be overstated though is the place of modern IDE's level of integration with a high level oo based languages.

Now, Imagine writing VB code with a text editor.


Quote: " I wonder if you see how it makes sense for those to belong to the objects and not the main program? What if some of those functions are called from other functions, and you do not want them to be executable from the main program? In OOP, you make them private to do this. Private functions and variables do not exist outside of the object itself and cannot be accessed outside of this scope. Free code security!"


Well true, protection is a nice benefit in the modularity of code/data. Although it really doesn't take a lot of effort to write equally modular code in procedural basic. It's just missing some creature comforts.

Kevin Picone
Play Basic - Visible Worlds - Kyruss II
[url]www.underwaredesign.com[/url]
Pricey
21
Years of Service
User Offline
Joined: 22nd Feb 2003
Location:
Posted: 16th Oct 2003 20:17


Nice Gun... AHH! He's Gotta Gun!

MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 16th Oct 2003 21:06
Who cares about C++?

*ducks and runs*

Just kidding, Object Pascal is OOP, and as easy to learn as Basic. I'd recommend it to programmers that don't want to grapple with the intricasies of C++.

-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 17th Oct 2003 00:39
1..DG has valid points..and he does have a published game in a procedural language , more than I have in C++ (or even anything that could be even slightly thought of as a game in C++).

2..some language researchers are now saying that OOP is the current fashion, and they claim that some of the features are hindering solid program development or limiting flexibility (some coders just code whole swathes that are just library calls and basic variable handling)

3..give it thirty/forty years and whatever we will be coding in will look nothing like C++, C# or anything else currently out there and probably not have even the faintest simularity to the current languages

4..for a games language I would still like to see a cross between Darkbasic and FastTracks other language Div, Div has some powerful features that most people seem to ignore, imagine if you wrote a snippet of code to create and run an enemy and every time you wanted another enemy you just wrote

Make_Enemy(x,y,power)

and that enemy just ran itself until it was killed, want 500 enemys? just

for i=1 to 500
Make_Enemy(x,y,power)
next i

would do the trick, no indexes to keep track of, just make em and off they go until they get killed, all handled automaticaly, alas Div has serious limitations (256 colours, no true 3d, DOS mode only etc)

5..even the most devout OOP coder has to admit two things, the base compiler was written in procedural assembler, and the compiler will optimise the code and remove a lot (if not all) of the "nice" OOP structure, to create a fast chunk of procedural assembler, so thats what you end up with anyway.

Just my two bobs worth

Mentor.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Oct 2003 00:39
Kevin,

Here's a good explanation of factories, although the code is Java : http://gsraj.tripod.com/design/creational/factory/factory.html

And here are various implementations that qualify as factories in good-old C++ : http://www.codeproject.com/cpp/all_kinds_of_factories.asp

The only bit that isn't really covered is the v-table which these implementations rely on behind the scenes ... this is covered in a very basic way here : http://members.tripod.com/~s_mathur/Virt.html

This is all reasonably easy to implement in any language that has functions pointers, especially if they have macro languages to help ... this includes assembly
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 17th Oct 2003 02:06
Mentor,

Your #4 above is an OOP Class, and when the class is instantiated (how ever many times 1, 500, 1000) those become independent objects. Need to change code, change class code and all object recieve the benefit. This is just one of the many reasons why I wish DBP was an OOPL.


-RUST-
heartbone
22
Years of Service
User Offline
Joined: 9th Nov 2002
Location:
Posted: 17th Oct 2003 08:28
CattleRustler, this may sound a bit harsh but I believe that if DBP was an OOPL then you would be b*tching about why you couldn't do your job because of something else lacking.

DBP is wonderful just as it is now structured. It has everything necessary for development. All that is missing is a good creative developer.

If you can't deal with it, that's really too bad for you. You are missing out. DBP is all that you need to create excellent software. Either learn how to deal with it, or for your own sake go back to your object oriented programming. Your desire to corrupt it ain't gonna happen. Peace.

The more you see, the more you know.
The more you know, the more you see.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 17th Oct 2003 08:48
@Heartbone,

I never said I didn't like dbp, there are plenty of my posts floating around singing the praises of dbp. I am entitled to MY HUMBLE OPINIONS and I am entitled to post them here without worrying what you think!

I am hardly bitching, as you put it, by pointing out how much I like OOP and wanted to see some more oop-like stuff incorporated into the language.

Stop being a jackass and get a grip
Peace

-RUST-
kingius
22
Years of Service
User Offline
Joined: 16th Oct 2002
Location:
Posted: 17th Oct 2003 14:14
There seems to be a lot of insecurity about OOP and DBP. There shouldnt be. DBP is doing OOP in the background for you by interfacing with DirectX, its just wrapping it into functions so you dont have to worry about it. This means that while DBP isnt letting you use OOP principles in the programming, its built on OOP under the hood.

OOP is everywhere, part of every application you use and the OS they run on, you have nothing to fear.
heartbone
22
Years of Service
User Offline
Joined: 9th Nov 2002
Location:
Posted: 17th Oct 2003 21:51 Edited at: 17th Oct 2003 21:53
CattleRustler you don't seem very humble to me. But as you say you are entitled to your opinion.

So am I.

You get a grip on reality.

DarkBASIC and objects, ha ha ha.

The more you see, the more you know.
The more you know, the more you see.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 17th Oct 2003 23:26
Everyone is entitled to their opinion - Good Luck to all

-RUST-

Login to post a reply

Server time is: 2024-11-24 03:14:35
Your offset time is: 2024-11-24 03:14:35