Board index » delphi » OOP isn't so hard

OOP isn't so hard

Quote
----- Original Message -----
From: "piddy" <piddy...@veggienetcom.net>
Newsgroups: borland.public.delphi.non-technical
Sent: Wednesday, March 12, 2003 6:48 PM
Subject: OOP isn't so hard

You got some of the initialisation wrong:

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>     PiddyJobs := TPiddyJob.Create;
>     with PiddyJobs do begin
>       Troll := False;

// Should be...
    Troll := True ;

>       Programmer := True;
>       BurgerFlipper := false;
>       AddOccupations(Troll,Programmer,BurgerFlipper);
>       label1.Caption := NumberOfJobs(JobCount);
>       PiddyJobs.Free;
>     end;
> end;

This assignment is probably best moved into the TPiddyJob constructor, as
it's very much the default state and there's no real need to externally set
it after each object creation. :-)

Ian

 

Re:OOP isn't so hard


Quote
Kristofer Skaug wrote:
> "Erwien Saputra" wrote

>> Everything for just a nanosecond, isn't it?  :)  Like swapping two
>> integer values using three xor?

>> procedure Swap (var a, b : integer);
>> begin
>>    a := a xor b;
>>    b := a xor b;
>>    a := a xor b;
>> end;

> I profiled it using Zprof and found it, on the average, twice as slow
> (on my P4/1700) as:

> procedure Swap2(var a, b : integer);
> var c:integer;
> begin
>    c := a;
>    a := b;
>    b := c;
> end;

> Hmmm. So that's a lot of binary mumbo jumbo *and* slower code... ??

> Kristofer

But 4 bytes fewer memory :))))

--
Bojidar Alexanrov
Kodar Ltd.
http://www.Kodar.net
------
How To Ask Questions The Smart Way
http://www.linuxsilo.net/docs/smart-questions-en.html

Re:OOP isn't so hard


Quote
> If I went the route I might do something like include

> TJobClass = class of TJob;

...

Yep, that's definitely a better approach if you don't want to create the job
object beforehand, and set any properties of individual job instances.

Quote
> Thanks for this part.  For some reason I've always jumped through hoops
> NOT to add the comma on the last item.  I've done that way so many times
it
> never occurred to me to add the comma and just delete the last one
> afterwards.

Actually I think I borrowed it from TStringList.GetCommaText (been so long
don't really remember) - even that can be optimized by using a faster
version of StringReplace. I came to the conclusion using if's inside the
loop for just the last element was unjustified - would rather use a delete
after (which works even if the string length is zero).

You probably can optimize this further - I do Result := Result + ',' which
is probably using too much of the memory manager, so you could probably use
a "SetLength(Result, FJobList.Count*7)" first, assuming average stringlength
is 6 and leave an extra space for the comma. Not sure this is a good
approach though.

--
Deepak Shenoy (TeamB)
Agni Software
http://www.agnisoft.com

Re:OOP isn't so hard


Quote
> don't even know why I used GetJobName() instead of GetJobName; :-)

You didn't...I did :)

I use them sometimes...something I do everytime I code in C++ or C# and then
get back to Delphi :)

--
Deepak Shenoy (TeamB)
Agni Software
http://www.agnisoft.com

Re:OOP isn't so hard


"Bojidar Alexandrov" wrote

Quote

> But 4 bytes fewer memory :))))

This makes me shiver with optimised e{*word*277}ment!!!! <g>

Kristofer

Re:OOP isn't so hard


Quote
> > function TPiddyJob.GetJobNames : string;
> > var i : integer;
> > begin
> >    Result := '';
> >    for i := 0 to FJobList.Count-1 do
> >         Result := Result + TJob(FJobList[i]).GetJobName() + ',';
> >    Delete(Result, Length(Result)-1,1);
> > end;

Oh Btw, that should be
        Delete(Result, Length(Result),1);

I need a lint for email.

--
Deepak Shenoy (TeamB)
Agni Software
http://www.agnisoft.com

Re:OOP isn't so hard


In article <3e6f8...@newsgroups.borland.com>, piddy says...

Quote
> It's unreadable. If you want unreadable use C++.

First, C++ is definitely not unreadable.

Second, the code above is a lot better than your version.
--
Rudy Velthuis (TeamB)

"Put your hand on a hot stove for a minute, and it seems like an hour.
 Sit with a pretty girl for an hour, and it seems like a minute.
 THAT'S relativity." - Albert Einstein

Re:OOP isn't so hard


Quote
In article <3e705...@newsgroups.borland.com>, Kristofer Skaug wrote:
> > But 4 bytes fewer memory :))))

> This makes me shiver with optimised e{*word*277}ment!!!! <g>

Well, I remember the days when saving 4 bytes was important <g>

Mike Orriss (TeamB and DevExpress)

Re:OOP isn't so hard


Quote
Alessandro Federici wrote:
> "Alessandro Federici" <alef@[nospam]remobjects.com> wrote in message
> news:3e6faefa$1@newsgroups.borland.com...
> And buggy as well on second thought.

I was waiting someone will point this out...
I realized a little after I posted it. People at this group DOES read
the code ;-)

I just arranged the original code, and as it didn't checked for empty
lists (it didn't need to, because delete won't complain if you try to
delete an inexistent pos of a string), my arranged code didn't checked
either.

But the idea is still valid. Even when you have one if, it is not inside
the loop... And  in others languages not delphi, delete() might crash on
an invalid string position,so you might need an if on the original
version too.

for the record, the final code would be:

    if FJobList.Count>0 then Result := TJob(FJobList[0]).GetJobName()
else Result='';
    for i := 1 to FJobList.Count-1 do
         Result := Result + ',' + FJobList[i].GetJobName;

Re:OOP isn't so hard


Quote
On Thu, 13 Mar 2003 09:47:28 -0300, Adrian Gallero wrote:
> for the record, the final code would be:

>     if FJobList.Count>0 then Result := TJob(FJobList[0]).GetJobName()
> else Result='';
>     for i := 1 to FJobList.Count-1 do
>          Result := Result + ',' + FJobList[i].GetJobName;

I'd move the loop into the then-clause, so there's a comparison less ;)

if FJobList.Count = 0 then begin
  Result := '';
end else begin
  Result := TJob(FJobList[0]).GetJobName();
  for i := 1 to FJobList.Count-1 do
    Result := Result + ',' + FJobList[i].GetJobName;
end;

johannes

Go to page: [1] [2] [3] [4] [5] [6] [7] [8]

Other Threads