comp.lang.ada
 help / color / mirror / Atom feed
* Naming convention for classes?
@ 2004-02-03 23:52 Peter C. Chapin
  2004-02-04  0:27 ` Jeffrey Carter
  2004-02-04  8:06 ` tmoran
  0 siblings, 2 replies; 16+ messages in thread
From: Peter C. Chapin @ 2004-02-03 23:52 UTC (permalink / raw)



I'm a C++ person learning my way around Ada. Naturally I tend to think 
about things in a C++ way and that leads me to various questions. I 
notice, for example, that there seems to be two (at least) somewhat 
different ways to name classes in Ada. The first way gives the class 
name to a package producing the following effect.

package Date is
  type Object is private;

  procedure Advance(Item : in out Object; Step : in Integer);
  ...
end Date;

The I can create Date objects by first withing Date and then doing

  Today : Date.Object;
  ...
  Date.Advance(Today, 100);

Another approach would be to give the type itself the "nice" name and 
regard the package name as "noise" used to avoid name clashes in large 
programs. For example

package My_Library is
  type Date is private;

  procedure Advance(Item : in out Date; Step : in Integer);
  ...
end My_Library;

Then I might with My_Library and use My_Library and do

  Today : Date;
  ...
  Advance(Today, 100);

The second approach seems natural but I notice that the Charles 
component library uses the first approach and it seems to work well in 
that case. For example, in my program I do

   package Vector is new Charles.Vectors.Unbounded(
     Index_Type => Natural, Element_Type => Storage_Type);

Then I declare things to be Vector.Container_Type and use procedures 
like Vector.Append and Vector.Insert, etc. Now I'm building my own 
abstract type and I find myself waffling... should I use my intended 
name for the package or for a type inside the package? Is there some 
kind of "best practice" for this? Or am I off in the weeds here?

Thanks!

Peter



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-03 23:52 Naming convention for classes? Peter C. Chapin
@ 2004-02-04  0:27 ` Jeffrey Carter
  2004-02-04  2:31   ` Peter C. Chapin
  2004-02-04  8:06 ` tmoran
  1 sibling, 1 reply; 16+ messages in thread
From: Jeffrey Carter @ 2004-02-04  0:27 UTC (permalink / raw)


Peter C. Chapin wrote:

> package Date is
>   type Object is private;
> 
>   procedure Advance(Item : in out Object; Step : in Integer);
>   ...
> end Date;
> 
> The I can create Date objects by first withing Date and then doing
> 
>   Today : Date.Object;
>   ...
>   Date.Advance(Today, 100);
> 
> package My_Library is
>   type Date is private;
> 
>   procedure Advance(Item : in out Date; Step : in Integer);
>   ...
> end My_Library;
> 
> Then I might with My_Library and use My_Library and do
> 
>   Today : Date;
>   ...
>   Advance(Today, 100);

Is this a troll? You'll find ample discussion of this (naming types and 
packages) if you search c.l.a at groups.google.com. Some people are very 
attached to one approach or the other.

The 2 approaches you mention are called "use unfriendly" and "use 
friendly", respectively. There are arguments for and against both 
approaches. Ada's standard packages are use friendly. For example, 
package Ada.Strings.Unbounded declares type Unbounded_String.

If you create use friendly packages, you should not consider the package 
name to be noise. "Ada.Strings.Unbounded" is not just noise.

One approach is to use appropriate suffixes to create useful names:

package Date_Handling is
    type Date_Info is private;

    procedure Advance (Date : in out Date_Info; Num_Days : in Positive);

Note that with this approach the reader can tell that you're advancing a 
date by a number of days without any comments. Note also that negative 
numbers of days are not allowed, as that would not be considered 
advancing a date.

-- 
Jeff Carter
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail
06




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  0:27 ` Jeffrey Carter
@ 2004-02-04  2:31   ` Peter C. Chapin
  2004-02-04  8:57     ` Jean-Pierre Rosen
                       ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Peter C. Chapin @ 2004-02-04  2:31 UTC (permalink / raw)


In article <G%WTb.12124$uM2.2382@newsread1.news.pas.earthlink.net>, 
spam@spam.com says...

> Is this a troll? You'll find ample discussion of this (naming types and 
> packages) if you search c.l.a at groups.google.com. Some people are very 
> attached to one approach or the other.

Okay, so in other words either approach is considered acceptable by the 
community at large. It's a matter of style. At least that's the 
impression I get from your reply. Does that sound like a fair 
assessment?

Peter



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-03 23:52 Naming convention for classes? Peter C. Chapin
  2004-02-04  0:27 ` Jeffrey Carter
@ 2004-02-04  8:06 ` tmoran
  2004-02-04 11:49   ` Peter C. Chapin
  1 sibling, 1 reply; 16+ messages in thread
From: tmoran @ 2004-02-04  8:06 UTC (permalink / raw)


>  Today : Date.Object;
>  Today : Date;
>The second approach seems natural ...
  Agreed.
Remember that a package can contain more than one type definition, and
in general a package is a higher level of abstraction than any one of
its contents.  So you might do
  package Timestamps is
    type Date is ...
or
  package Historical_Records is
    type Date is ...
But as noted, it's often not obvious and it comes down to a personal
stylistic preference - presumably a reasonably consistent one.
Have you looked at "Ada Quality and Style"?



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  2:31   ` Peter C. Chapin
@ 2004-02-04  8:57     ` Jean-Pierre Rosen
  2004-02-04 11:52       ` Peter C. Chapin
  2004-02-04  9:13     ` Preben Randhol
                       ` (2 subsequent siblings)
  3 siblings, 1 reply; 16+ messages in thread
From: Jean-Pierre Rosen @ 2004-02-04  8:57 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1942 bytes --]


"Peter C. Chapin" <pchapin@sover.net> a �crit dans le message de > Okay, so in other words either approach is considered acceptable
by the
> community at large. It's a matter of style. At least that's the
> impression I get from your reply. Does that sound like a fair
> assessment?
>
Since you said you were comming from a C++ background, let me explain some things you must understand first.

Ada has a so-called "building blocks" approach. Each feature provides a well-defined functionality, and the user builds the features
he needs by assembling those building blocks. For example, packages provide encapsulation. Derived types provide support for
inheritance. Tagged types provide support for dynamic dispatching.

Ada has no built-in concept of "Class" in the usual sense. If you consider that a class is an encapsulation with dynamic binding,
then a class in Ada is a design pattern where you just declare one tagged type inside a package. *For this design pattern*, it makes
a lot of sense to declare the package with the class name. A full discussion of this has been published in Ada Letters (Vol. XV,
n�2): "A naming convention for classes in Ada 9X". The paper can be downloaded from http://www.adalog.fr/publica2.htm. Note that
this convention works very well with "facets" generics, i.e. generics used to add properties to tagged types.

Now, this is not the only possible design pattern. For example, you can have the equivalent of "friends" in C++ by declaring two
tagged types in the same package. The previous notation is no more applicable to this pattern.

For more sophisticated use of building blocks/design pattern approach, look at "Ada, Interfaces and the Listener Paradigm", a paper
presented at Ada-Europe 2002 which is available from the same web page.

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  2:31   ` Peter C. Chapin
  2004-02-04  8:57     ` Jean-Pierre Rosen
@ 2004-02-04  9:13     ` Preben Randhol
  2004-02-04 14:57     ` Georg Bauhaus
  2004-02-04 19:01     ` Jeffrey Carter
  3 siblings, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2004-02-04  9:13 UTC (permalink / raw)


On 2004-02-04, Peter C Chapin <pchapin@sover.net> wrote:
>
> Okay, so in other words either approach is considered acceptable by the 
> community at large. It's a matter of style. At least that's the 
> impression I get from your reply. Does that sound like a fair 
> assessment?

My advice is to use senisble names for the types in the packages and try
to avoid using the "use" statement for the packages you built yourself.

-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  8:06 ` tmoran
@ 2004-02-04 11:49   ` Peter C. Chapin
  2004-02-04 12:36     ` Preben Randhol
  0 siblings, 1 reply; 16+ messages in thread
From: Peter C. Chapin @ 2004-02-04 11:49 UTC (permalink / raw)


In article <fK1Ub.87451$U%5.467557@attbi_s03>, tmoran@acm.org says...

> Remember that a package can contain more than one type definition, and
> in general a package is a higher level of abstraction than any one of
> its contents.

Yes, I understand... although in the case where one is trying to build a 
"class" in the sense meant by other object oriented languages, using a 
package to wrap up a single type and its operations also seems to be 
sensible as well. I can see that this is a matter of debate.

> Have you looked at "Ada Quality and Style"?

No, but I will look for it. Thanks!

Peter



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  8:57     ` Jean-Pierre Rosen
@ 2004-02-04 11:52       ` Peter C. Chapin
  2004-02-04 14:02         ` Jean-Pierre Rosen
  2004-02-04 14:13         ` Martin Krischik
  0 siblings, 2 replies; 16+ messages in thread
From: Peter C. Chapin @ 2004-02-04 11:52 UTC (permalink / raw)


In article <ngcqvb.2ln.ln@skymaster>, rosen@adalog.fr says...

> Now, this is not the only possible design pattern. For example, you can have the equivalent of "friends" in C++ by declaring two
> tagged types in the same package. The previous notation is no more applicable to this pattern.

Yes, I see that. Coupling two classes by defining them in the same 
package provides some interesting options.

BTW, the web page you mentioned in your post, 
http://www.adalog.fr/publica2.htm, doesn't work for me. I get an error 
about loading the Java applet.

Peter



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04 11:49   ` Peter C. Chapin
@ 2004-02-04 12:36     ` Preben Randhol
  2004-02-04 12:41       ` Preben Randhol
  2004-02-04 14:09       ` Martin Krischik
  0 siblings, 2 replies; 16+ messages in thread
From: Preben Randhol @ 2004-02-04 12:36 UTC (permalink / raw)


On 2004-02-04, Peter C Chapin <pchapin@sover.net> wrote:
> In article <fK1Ub.87451$U%5.467557@attbi_s03>, tmoran@acm.org says...
>
>> Remember that a package can contain more than one type definition, and
>> in general a package is a higher level of abstraction than any one of
>> its contents.
>
> Yes, I understand... although in the case where one is trying to build a 
> "class" in the sense meant by other object oriented languages, using a 
> package to wrap up a single type and its operations also seems to be 
> sensible as well. I can see that this is a matter of debate.

Why should one limit a package to contain a single type?

-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04 12:36     ` Preben Randhol
@ 2004-02-04 12:41       ` Preben Randhol
  2004-02-04 14:09       ` Martin Krischik
  1 sibling, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2004-02-04 12:41 UTC (permalink / raw)


On 2004-02-04, Preben Randhol <randhol+valid_for_reply_from_news@pvv.org> wrote:
> On 2004-02-04, Peter C Chapin <pchapin@sover.net> wrote:
>> In article <fK1Ub.87451$U%5.467557@attbi_s03>, tmoran@acm.org says...
>>
>>> Remember that a package can contain more than one type definition, and
>>> in general a package is a higher level of abstraction than any one of
>>> its contents.
>>
>> Yes, I understand... although in the case where one is trying to build a 
>> "class" in the sense meant by other object oriented languages, using a 
>> package to wrap up a single type and its operations also seems to be 
>> sensible as well. I can see that this is a matter of debate.
>
> Why should one limit a package to contain a single type?

Oops hit send a bit fast. Here are two pages worth reading to understand
"classes" in Ada95.

http://www.adapower.com/articles/class.html


http://www.adapower.com/lang/whatclass.html


-- 
"Saving keystrokes is the job of the text editor, not the programming
 language."



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04 11:52       ` Peter C. Chapin
@ 2004-02-04 14:02         ` Jean-Pierre Rosen
  2004-02-05 12:18           ` Stuart Palin
  2004-02-04 14:13         ` Martin Krischik
  1 sibling, 1 reply; 16+ messages in thread
From: Jean-Pierre Rosen @ 2004-02-04 14:02 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 889 bytes --]


"Peter C. Chapin" <pchapin@sover.net> a �crit dans le message de news:MPG.1a8aa72f52f26da1989691@news.sover.net...
> BTW, the web page you mentioned in your post,
> http://www.adalog.fr/publica2.htm, doesn't work for me. I get an error
> about loading the Java applet.
>
Sigh... Try a different browser.
It does work with some Java machines, not with others. I think I'll have to give up with these Adaplets (yes, they were written in
Ada). So much for Java portability...

Talking about Java:
I recently found an article in a French computer journal that roughly said:

"If portability of Java code is a reality [...] it is better that each targeted platform uses an identical JDK (version and
vendor)."

Nice view of portability!
-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04 12:36     ` Preben Randhol
  2004-02-04 12:41       ` Preben Randhol
@ 2004-02-04 14:09       ` Martin Krischik
  1 sibling, 0 replies; 16+ messages in thread
From: Martin Krischik @ 2004-02-04 14:09 UTC (permalink / raw)


Preben Randhol wrote:

> On 2004-02-04, Peter C Chapin <pchapin@sover.net> wrote:
>> In article <fK1Ub.87451$U%5.467557@attbi_s03>, tmoran@acm.org says...
>>
>>> Remember that a package can contain more than one type definition, and
>>> in general a package is a higher level of abstraction than any one of
>>> its contents.
>>
>> Yes, I understand... although in the case where one is trying to build a
>> "class" in the sense meant by other object oriented languages, using a
>> package to wrap up a single type and its operations also seems to be
>> sensible as well. I can see that this is a matter of debate.
> 
> Why should one limit a package to contain a single type?

Unless you want the classes to be "friend"s  (in a C++ sence) you must put
the in different packages.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04 11:52       ` Peter C. Chapin
  2004-02-04 14:02         ` Jean-Pierre Rosen
@ 2004-02-04 14:13         ` Martin Krischik
  1 sibling, 0 replies; 16+ messages in thread
From: Martin Krischik @ 2004-02-04 14:13 UTC (permalink / raw)


Peter C. Chapin wrote:

> In article <ngcqvb.2ln.ln@skymaster>, rosen@adalog.fr says...
> 
>> Now, this is not the only possible design pattern. For example, you can
>> have the equivalent of "friends" in C++ by declaring two tagged types in
>> the same package. The previous notation is no more applicable to this
>> pattern.
 
> Yes, I see that. Coupling two classes by defining them in the same
> package provides some interesting options.

You should also remember that in Ada the concept of "protected" is
implemented by child packages not child classes.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  2:31   ` Peter C. Chapin
  2004-02-04  8:57     ` Jean-Pierre Rosen
  2004-02-04  9:13     ` Preben Randhol
@ 2004-02-04 14:57     ` Georg Bauhaus
  2004-02-04 19:01     ` Jeffrey Carter
  3 siblings, 0 replies; 16+ messages in thread
From: Georg Bauhaus @ 2004-02-04 14:57 UTC (permalink / raw)


Peter C. Chapin <pchapin@sover.net> wrote:
: In article <G%WTb.12124$uM2.2382@newsread1.news.pas.earthlink.net>, 
: spam@spam.com says...
: 
:> Is this a troll? You'll find ample discussion of this (naming types and 
:> packages) if you search c.l.a at groups.google.com. Some people are very 
:> attached to one approach or the other.
: 
: Okay, so in other words either approach is considered acceptable by the 
: community at large.

Unless I have missed it there is another style which uses plural
for packages and singular for types. Thus you can have

package Dates is

  type Date is ...;

and then

  Dates.operation(today, ...);


-- Georg



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04  2:31   ` Peter C. Chapin
                       ` (2 preceding siblings ...)
  2004-02-04 14:57     ` Georg Bauhaus
@ 2004-02-04 19:01     ` Jeffrey Carter
  3 siblings, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2004-02-04 19:01 UTC (permalink / raw)


Peter C. Chapin wrote:
> 
> Okay, so in other words either approach is considered acceptable by the 
> community at large. It's a matter of style. At least that's the 
> impression I get from your reply. Does that sound like a fair 
> assessment?

To a large degree, yes, but as other replies have shown, there are cases 
where the use-unfriendly approach does not work.

-- 
Jeff Carter
"Perfidious English mouse-dropping hoarders."
Monty Python & the Holy Grail
10




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Naming convention for classes?
  2004-02-04 14:02         ` Jean-Pierre Rosen
@ 2004-02-05 12:18           ` Stuart Palin
  0 siblings, 0 replies; 16+ messages in thread
From: Stuart Palin @ 2004-02-05 12:18 UTC (permalink / raw)


Jean-Pierre Rosen wrote:
> 
> "Peter C. Chapin" <pchapin@sover.net> a �crit dans le message
> de news:MPG.1a8aa72f52f26da1989691@news.sover.net...
> > BTW, the web page you mentioned in your post,
> > http://www.adalog.fr/publica2.htm, doesn't work for me. I get an error
> > about loading the Java applet.
> >
> Sigh... Try a different browser.
> It does work with some Java machines, not with others. I think I'll
> have to give up with these Adaplets (yes, they were written in
> Ada). So much for Java portability...

Please at least provide an alternative link to the
information: unfortunately your 'active content' does not
make it through the company firewall here!

--
Stuart Palin



^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2004-02-05 12:18 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-03 23:52 Naming convention for classes? Peter C. Chapin
2004-02-04  0:27 ` Jeffrey Carter
2004-02-04  2:31   ` Peter C. Chapin
2004-02-04  8:57     ` Jean-Pierre Rosen
2004-02-04 11:52       ` Peter C. Chapin
2004-02-04 14:02         ` Jean-Pierre Rosen
2004-02-05 12:18           ` Stuart Palin
2004-02-04 14:13         ` Martin Krischik
2004-02-04  9:13     ` Preben Randhol
2004-02-04 14:57     ` Georg Bauhaus
2004-02-04 19:01     ` Jeffrey Carter
2004-02-04  8:06 ` tmoran
2004-02-04 11:49   ` Peter C. Chapin
2004-02-04 12:36     ` Preben Randhol
2004-02-04 12:41       ` Preben Randhol
2004-02-04 14:09       ` Martin Krischik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox