Results 1 to 6 of 6

Thread: More object/class based ponderings

  1. #1

    More object/class based ponderings

    After reading a recent post regarding the delights of object/class based development it spurred me to use them in my own projects. I was previously using records and separate functions and procedures. What I'm wondering is how small is too small for an object. Here's my class for my logger that records all goings on within the game (it's very basic but that's half my point):

    Code:
    type
      TLog = Class
        LogFile: TextFile;
        MessageStr: String;//Last message logged
        public
          constructor Create(Filename: String);
          destructor Destroy;Reintroduce;
          procedure AddLog(Msg: String;Fatal: Boolean);//A fatal error will always result in a report
          procedure Report(Msg: String);
      end;
    Is it really worth me creating a class with just 2 fields and 2 procdedures (Not including Create and Destroy of course)? How small is too small?
    Isometric game development blog http://isoenginedev.blogspot.com/

  2. #2

    More object/class based ponderings

    There's no too small A class is always worth writing if it fits.. There could be classes with even few members, for example a TLogger with just a TStringList and a Log() method.
    Designing all in object is a Good Thing (tm) IMHO.
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  3. #3

    More object/class based ponderings

    Well, one advantage of using objects for logging is that it is then very easy to have several logs. Say one for overall application state, one for error and one for timing.

    I agree with {MSX}. No too small. I tend to think in objects when coding.

    /Balaras
    I've been trying for some time to develop a lifestyle that doesn't require my presence. - G.B. Trudeau

  4. #4

    More object/class based ponderings

    lol.. that logger looks extremely suspeciously much like my old

    well i have all my major objects in classes. The only records i have are vector2f, vector3f, vector4f, plane, ray, matrix4, matrix4x4
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5

    More object/class based ponderings

    There is a case where using a class is overkill, and that would be in the case of vectors or matrices. You tend to have thousands of vectors and matrices in a decent game. Using a class for these means having a four byte overhead for each instance for the pointer to the virtual method table. Also, allocating large blocks of instances means you have to override InitInstance and FreeInstance of the base class to change the way the instances are allocated and freed.

    Compare this with using records. Block allocation is easy. The size of each instance is exactly what you specify in the record declaration. No hidden method calls in creation and destruction. A lot more efficient in memory and CPU.

    There is a middle ground where you specify the type as 'object'. This works very similar to records, but you can have methods in an object type.
    Code:
    type
      TVector = object
      public
        x, y, z&#58; Single;
        procedure Dot&#40;const v&#58; TVector&#41;&#58; Single;
      end;
    Of course, Borland are deprecating the use of 'object' so I wouldn't be surprised if it disappeared from the language in a few more releases.

  6. #6
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    More object/class based ponderings

    Quote Originally Posted by Sly
    Of course, Borland are deprecating the use of 'object' so I wouldn't be surprised if it disappeared from the language in a few more releases.
    I think it is more fair to say it will disappear from the Delphi compiler than the Object Pascal language. With all these other compilers now supporting the Borland extended version of Apple's original dialect, they don't have full claim on the language anymore.

    Some compilers that now use the Object Pascal standard in some form are: Chrome, Delphi, Kylix and Free Pascal.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •