comp.lang.ada
 help / color / mirror / Atom feed
* Ada Recursion with strings
@ 2008-09-30 20:30 Joe
  2008-09-30 22:24 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Joe @ 2008-09-30 20:30 UTC (permalink / raw)


Hey all,
I'm trying to write a function that converts a 2**32 mod type into an
string formatted like an IP address (i.e. 192.168.2.1).

Here's what I wrote

with Ada.Text_IO;
use  Ada.Text_IO;

procedure Ip_packet is
   type Word_Type is mod 2**32;

   function IP_String (Word : Word_Type;
                       IP_String : String := "") return String
is
      Sub_String : String := Word_Type'Image(Word mod 2**8);
   begin
      if Word <= 2**8 then
         return Word_Type'Image(Word) & IP_String;
      else
         return IP_String( Word / 2**8,
                           "." & Sub_String & IP_String);
      end if;
   end IP_String;

begin  --  Ip_packet
   Put(IP_String(123456));
end Ip_packet;

There are 2 errors with this that I don't understand.  First, Ada
expects an integer for (word / 2**8).  Is this because word_type is
outside the standard integer range?  How do I get a division operator
that works on word_type?

If I patch the integer error by type casting word to an integer I get
another error.  On the second return statement, Ada complains that
there are "too many subscripts in array reference".  Is there a
limitation on the string I can pass to a recursive function call?

Thanks,
Joe



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

* Re: Ada Recursion with strings
  2008-09-30 20:30 Ada Recursion with strings Joe
@ 2008-09-30 22:24 ` Adam Beneschan
  2008-09-30 22:24 ` Jeffrey R. Carter
  2008-10-01  3:01 ` anon
  2 siblings, 0 replies; 19+ messages in thread
From: Adam Beneschan @ 2008-09-30 22:24 UTC (permalink / raw)


On Sep 30, 1:30 pm, Joe <joesmo...@gmail.com> wrote:
> Hey all,
> I'm trying to write a function that converts a 2**32 mod type into an
> string formatted like an IP address (i.e. 192.168.2.1).
>
> Here's what I wrote
>
> with Ada.Text_IO;
> use  Ada.Text_IO;
>
> procedure Ip_packet is
>    type Word_Type is mod 2**32;
>
>    function IP_String (Word : Word_Type;
>                        IP_String : String := "") return String
> is
>       Sub_String : String := Word_Type'Image(Word mod 2**8);
>    begin
>       if Word <= 2**8 then
>          return Word_Type'Image(Word) & IP_String;
>       else
>          return IP_String( Word / 2**8,
>                            "." & Sub_String & IP_String);
>       end if;
>    end IP_String;
>
> begin  --  Ip_packet
>    Put(IP_String(123456));
> end Ip_packet;
>
> There are 2 errors with this that I don't understand.  First, Ada
> expects an integer for (word / 2**8).  Is this because word_type is
> outside the standard integer range?  How do I get a division operator
> that works on word_type?
>
> If I patch the integer error by type casting word to an integer I get
> another error.  On the second return statement, Ada complains that
> there are "too many subscripts in array reference".  Is there a
> limitation on the string I can pass to a recursive function call?

When you say this:

        return IP_String( Word / 2**8,
                          "." & Sub_String & IP_String);

what is the compiler going to think you mean by the first IP_String in
this expression?  What about the second one?

There are other errors in your code; you'll probably figure several of
them out once you get your program to run, and you'll probably figure
another out if you change 123456 in your test to 65536.

                              -- Adam





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

* Re: Ada Recursion with strings
  2008-09-30 20:30 Ada Recursion with strings Joe
  2008-09-30 22:24 ` Adam Beneschan
@ 2008-09-30 22:24 ` Jeffrey R. Carter
  2008-10-01  0:13   ` Joe
  2008-10-01  3:01 ` anon
  2 siblings, 1 reply; 19+ messages in thread
From: Jeffrey R. Carter @ 2008-09-30 22:24 UTC (permalink / raw)


Joe wrote:
> 
>    function IP_String (Word : Word_Type;
>                        IP_String : String := "") return String
> is

>          return IP_String( Word / 2**8,
>                            "." & Sub_String & IP_String);

> There are 2 errors with this that I don't understand.  First, Ada
> expects an integer for (word / 2**8).  Is this because word_type is
> outside the standard integer range?  How do I get a division operator
> that works on word_type?

This is a misleading error msg.

Your problem is that your function IP_String has a parameter of the same name. 
Within the function, IP_String refers to the parameter. Thus,

return IP_String (...);

is interpreted as indexing into the String. Since "Word / 2 ** 8" is of type 
Word_Type and String is indexed by Integer, you get the error msg about 
expecting Integer.

Though you can refer to the function IP_String within itself as 
Ip_Packet.IP_String, it would be better to change the name of the parameter to 
something other than the name of the function, like Current_Result.

How many times does the magic number 2 ** 8 appear in this short example?

-- 
Jeff Carter
"I unclog my nose towards you."
Monty Python & the Holy Grail
11



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

* Re: Ada Recursion with strings
  2008-09-30 22:24 ` Jeffrey R. Carter
@ 2008-10-01  0:13   ` Joe
  2008-10-01  0:27     ` Jeffrey R. Carter
  2008-10-01  0:28     ` Adam Beneschan
  0 siblings, 2 replies; 19+ messages in thread
From: Joe @ 2008-10-01  0:13 UTC (permalink / raw)


On Sep 30, 6:24 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:

> Though you can refer to the function IP_String within itself as
> Ip_Packet.IP_String, it would be better to change the name of the parameter to
> something other than the name of the function, like Current_Result.

doh, I didn't even see that.  Changing the parameter name solved all
my problems.

> How many times does the magic number 2 ** 8 appear in this short example?

Thrice, I'm not sure I know what you're getting at.

> There are other errors in your code; you'll probably figure several of
> them out once you get your program to run, and you'll probably figure
> another out if you change 123456 in your test to 65536.

Thank you Adam for that little hint.  The 2**8n numbers now work as
expected.

One other thing.  The image attribute returns a string with a space in
front.  Is there any way to get rid of the space?



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

* Re: Ada Recursion with strings
  2008-10-01  0:13   ` Joe
@ 2008-10-01  0:27     ` Jeffrey R. Carter
  2008-10-01  1:15       ` Robert A Duff
  2008-10-01  0:28     ` Adam Beneschan
  1 sibling, 1 reply; 19+ messages in thread
From: Jeffrey R. Carter @ 2008-10-01  0:27 UTC (permalink / raw)


Joe wrote:
> 
> Thrice, I'm not sure I know what you're getting at.

It's good practice to make such values into named numbers.

> One other thing.  The image attribute returns a string with a space in
> front.  Is there any way to get rid of the space?

Technically, 'Image for numeric types begins with the sign, which is ' ' for 
non-negative numbers and '-' for negative values. That doesn't make it any more 
user friendly.

There are a number of ways to "get rid" of the space. Probably the easiest is to 
slice the image to exclude the 1st Character. See ARM 4.1.2.

-- 
Jeff Carter
"I unclog my nose towards you."
Monty Python & the Holy Grail
11



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

* Re: Ada Recursion with strings
  2008-10-01  0:13   ` Joe
  2008-10-01  0:27     ` Jeffrey R. Carter
@ 2008-10-01  0:28     ` Adam Beneschan
  2008-10-01  9:48       ` Georg Bauhaus
  1 sibling, 1 reply; 19+ messages in thread
From: Adam Beneschan @ 2008-10-01  0:28 UTC (permalink / raw)


On Sep 30, 5:13 pm, Joe <joesmo...@gmail.com> wrote:
> On Sep 30, 6:24 pm, "Jeffrey R. Carter"
>
> <spam.jrcarter....@spam.acm.org> wrote:
> > Though you can refer to the function IP_String within itself as
> > Ip_Packet.IP_String, it would be better to change the name of the parameter to
> > something other than the name of the function, like Current_Result.
>
> doh, I didn't even see that.  Changing the parameter name solved all
> my problems.
>
> > How many times does the magic number 2 ** 8 appear in this short example?
>
> Thrice, I'm not sure I know what you're getting at.
>
> > There are other errors in your code; you'll probably figure several of
> > them out once you get your program to run, and you'll probably figure
> > another out if you change 123456 in your test to 65536.
>
> Thank you Adam for that little hint.  The 2**8n numbers now work as
> expected.
>
> One other thing.  The image attribute returns a string with a space in
> front.  Is there any way to get rid of the space?

The function

Ada.Strings.Fixed.Trim (S, Ada.Strings.Both)

will get rid of all leading and trailing spaces from S.

I'd much prefer to have an 'Image attribute that didn't add that blank
(it only adds it for nonnegative numbers).  I'm not sure why the
decision was made (back in Ada 83) to add the blank; having a
formatted number with either a blank or a minus sign in front can help
get numbers lined up in a report, but 'Image returns a variable-length
string anyway so I don't think it could be used effectively to line
anything up.  This is one of the most annoying features of Ada, and
I'm betting that I'm not alone in thinking that.

                                -- Adam




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

* Re: Ada Recursion with strings
  2008-10-01  0:27     ` Jeffrey R. Carter
@ 2008-10-01  1:15       ` Robert A Duff
  2008-10-01 11:34         ` Jeffrey R. Carter
  0 siblings, 1 reply; 19+ messages in thread
From: Robert A Duff @ 2008-10-01  1:15 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:

> Joe wrote:
>> Thrice, I'm not sure I know what you're getting at.
>
> It's good practice to make such values into named numbers.

Do you really think that's useful in this case, where 2**8
appears several times close together?  What would you call
that named number -- Two_To_The_Eighth?

>> One other thing.  The image attribute returns a string with a space in
>> front.  Is there any way to get rid of the space?
>
> Technically, 'Image for numeric types begins with the sign, which is ' '
> for non-negative numbers and '-' for negative values. That doesn't make
> it any more user friendly.
>
> There are a number of ways to "get rid" of the space. Probably the
> easiest is to slice the image to exclude the 1st Character. See ARM
> 4.1.2.

Right, the extra blank is very annoying, but not so hard to get rid of.

- Bob



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

* Re: Ada Recursion with strings
  2008-09-30 20:30 Ada Recursion with strings Joe
  2008-09-30 22:24 ` Adam Beneschan
  2008-09-30 22:24 ` Jeffrey R. Carter
@ 2008-10-01  3:01 ` anon
  2 siblings, 0 replies; 19+ messages in thread
From: anon @ 2008-10-01  3:01 UTC (permalink / raw)


If your using GNAT.

Checkout "GNAT.SOCKET.IMAGE"  in file g-socket.adb. The "Image" routine 
converts the IP address (IPv4 and IPv6) to a string. Might give you some 
Ideas.


In <ff8b4b01-211b-4487-8184-85563f4148db@t42g2000hsg.googlegroups.com>, Joe <joesmoe10@gmail.com> writes:
>Hey all,
>I'm trying to write a function that converts a 2**32 mod type into an
>string formatted like an IP address (i.e. 192.168.2.1).
>
>Here's what I wrote
>
>with Ada.Text_IO;
>use  Ada.Text_IO;
>
>procedure Ip_packet is
>   type Word_Type is mod 2**32;
>
>   function IP_String (Word : Word_Type;
>                       IP_String : String := "") return String
>is
>      Sub_String : String := Word_Type'Image(Word mod 2**8);
>   begin
>      if Word <= 2**8 then
>         return Word_Type'Image(Word) & IP_String;
>      else
>         return IP_String( Word / 2**8,
>                           "." & Sub_String & IP_String);
>      end if;
>   end IP_String;
>
>begin  --  Ip_packet
>   Put(IP_String(123456));
>end Ip_packet;
>
>There are 2 errors with this that I don't understand.  First, Ada
>expects an integer for (word / 2**8).  Is this because word_type is
>outside the standard integer range?  How do I get a division operator
>that works on word_type?
>
>If I patch the integer error by type casting word to an integer I get
>another error.  On the second return statement, Ada complains that
>there are "too many subscripts in array reference".  Is there a
>limitation on the string I can pass to a recursive function call?
>
>Thanks,
>Joe




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

* Re: Ada Recursion with strings
  2008-10-01  0:28     ` Adam Beneschan
@ 2008-10-01  9:48       ` Georg Bauhaus
  2008-10-01 10:22         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 19+ messages in thread
From: Georg Bauhaus @ 2008-10-01  9:48 UTC (permalink / raw)


Adam Beneschan schrieb:

> The function
> 
> Ada.Strings.Fixed.Trim (S, Ada.Strings.Both)
> 
> will get rid of all leading and trailing spaces from S.
> 
> I'd much prefer to have an 'Image attribute that didn't add that blank
> (it only adds it for nonnegative numbers).

Can I suggest a stronger emphasis on the Ada packages providing
value-to-string conversions.  Isn't the 'Image attribute already
overused? (Worse, non-portable 'Img seems to be popular.)
Anyway, the 'Image attribute is not available for all types.


>  I'm not sure why the
> decision was made (back in Ada 83) to add the blank; having a
> formatted number with either a blank or a minus sign in front can help
> get numbers lined up in a report, but 'Image returns a variable-length
> string anyway so I don't think it could be used effectively to line
> anything up. 

Just speculating, maybe positioning used this procedure?

    procedure SET_COL (TO : in POSITIVE_COUNT);

Anyway, there is, and seems to have been,

    procedure PUT(TO   : out STRING;
                  ITEM : in NUM;
                  BASE : in NUMBER_BASE := DEFAULT_BASE);

where TO'Length would have to be equal to
the number of digits in Word/2**8.



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

* Re: Ada Recursion with strings
  2008-10-01  9:48       ` Georg Bauhaus
@ 2008-10-01 10:22         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 19+ messages in thread
From: Dmitry A. Kazakov @ 2008-10-01 10:22 UTC (permalink / raw)


On Wed, 01 Oct 2008 11:48:34 +0200, Georg Bauhaus wrote:

> Adam Beneschan schrieb:
> 
>> I'd much prefer to have an 'Image attribute that didn't add that blank
>> (it only adds it for nonnegative numbers).
> 
> Can I suggest a stronger emphasis on the Ada packages providing
> value-to-string conversions.

1. String conversions require multiple additional parameters for some
types. A "better" image with additional formatting parameter(s), would
quickly bring you to the problem of parallel type hierarchies
   = no solution in sight.

2. Even without the parameters, the problem is equivalent to double
dispatch
   = better not touch.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Ada Recursion with strings
  2008-10-01  1:15       ` Robert A Duff
@ 2008-10-01 11:34         ` Jeffrey R. Carter
  2008-10-01 14:29           ` Adam Beneschan
  0 siblings, 1 reply; 19+ messages in thread
From: Jeffrey R. Carter @ 2008-10-01 11:34 UTC (permalink / raw)


Robert A Duff wrote:
> 
> Do you really think that's useful in this case, where 2**8
> appears several times close together?  What would you call
> that named number -- Two_To_The_Eighth?

I'd probably do

Bits_Per_Byte   : constant := 8;
Values_Per_Byte : constant := 2 ** Bits_Per_Byte;

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail
59



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

* Re: Ada Recursion with strings
  2008-10-01 11:34         ` Jeffrey R. Carter
@ 2008-10-01 14:29           ` Adam Beneschan
  2008-10-01 15:18             ` Jean-Pierre Rosen
  2008-10-03  6:54             ` Maciej Sobczak
  0 siblings, 2 replies; 19+ messages in thread
From: Adam Beneschan @ 2008-10-01 14:29 UTC (permalink / raw)


On Oct 1, 4:34 am, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> Robert A Duff wrote:
>
> > Do you really think that's useful in this case, where 2**8
> > appears several times close together?  What would you call
> > that named number -- Two_To_The_Eighth?
>
> I'd probably do
>
> Bits_Per_Byte   : constant := 8;
> Values_Per_Byte : constant := 2 ** Bits_Per_Byte;

Yep, the advantage of that is that if the people who design the
Internet communication protocols suddenly decide that IP addresses are
going to use 9-bit bytes, all you have to do is change one constant,
rather than try to find all the 8's in your code and change them,
which is more error-prone.

                            -- Adam




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

* Re: Ada Recursion with strings
  2008-10-01 14:29           ` Adam Beneschan
@ 2008-10-01 15:18             ` Jean-Pierre Rosen
  2008-10-01 16:47               ` Georg Bauhaus
  2008-10-02  7:24               ` Niklas Holsti
  2008-10-03  6:54             ` Maciej Sobczak
  1 sibling, 2 replies; 19+ messages in thread
From: Jean-Pierre Rosen @ 2008-10-01 15:18 UTC (permalink / raw)


Adam Beneschan a �crit :
> Yep, the advantage of that is that if the people who design the
> Internet communication protocols suddenly decide that IP addresses are
> going to use 9-bit bytes, all you have to do is change one constant,
> rather than try to find all the 8's in your code and change them,
> which is more error-prone.
> 
Reminds me of an old advice explaining why named constants were better 
than numerical values. They took the example of Pi, saying it would make 
  maintenance easier "should the value of Pi change" :-)

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



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

* Re: Ada Recursion with strings
  2008-10-01 15:18             ` Jean-Pierre Rosen
@ 2008-10-01 16:47               ` Georg Bauhaus
  2008-10-02  7:24               ` Niklas Holsti
  1 sibling, 0 replies; 19+ messages in thread
From: Georg Bauhaus @ 2008-10-01 16:47 UTC (permalink / raw)


Jean-Pierre Rosen schrieb:
> Adam Beneschan a écrit :
>> Yep, the advantage of that is that if the people who design the
>> Internet communication protocols suddenly decide that IP addresses are
>> going to use 9-bit bytes, all you have to do is change one constant,
>> rather than try to find all the 8's in your code and change them,
>> which is more error-prone.
>>
> Reminds me of an old advice explaining why named constants were better
> than numerical values. They took the example of Pi, saying it would make
>  maintenance easier "should the value of Pi change" :-)
> 

See ftp://pi.super-computing.org/README  for currently known
values to be used for Ada.Numerics.["03C0"].

In case you are wondering about the Perlish spelling of that
Ada constant, it is Ada.Numerics.π encoded for those of our computers
that cleanly represent characters using 7-bit bytes.  Which should
of course be the default representation by any modern engineering
standard. Who would build a keyboard having 16#110000# keys for
just one ancient constant?



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

* Re: Ada Recursion with strings
  2008-10-01 15:18             ` Jean-Pierre Rosen
  2008-10-01 16:47               ` Georg Bauhaus
@ 2008-10-02  7:24               ` Niklas Holsti
  2008-10-02  8:18                 ` Ludovic Brenta
  1 sibling, 1 reply; 19+ messages in thread
From: Niklas Holsti @ 2008-10-02  7:24 UTC (permalink / raw)


Jean-Pierre Rosen wrote:
> Adam Beneschan a �crit :
> 
>> Yep, the advantage of that is that if the people who design the
>> Internet communication protocols suddenly decide that IP addresses are
>> going to use 9-bit bytes, all you have to do is change one constant,
>> rather than try to find all the 8's in your code and change them,
>> which is more error-prone.
>>
> Reminds me of an old advice explaining why named constants were better 
> than numerical values. They took the example of Pi, saying it would make 
>  maintenance easier "should the value of Pi change" :-)

That reminds me of a former colleague, some decades ago, who was 
tearing his hair out because his astronomical calculations with a 
FORTRAN program were going wrong. Turns out he had the value of PI 
as a variable in a COMMON area, after an array, and an indexing 
error on this array was changing the value of PI, affecting several 
trigonometric formulae...

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



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

* Re: Ada Recursion with strings
  2008-10-02  7:24               ` Niklas Holsti
@ 2008-10-02  8:18                 ` Ludovic Brenta
  0 siblings, 0 replies; 19+ messages in thread
From: Ludovic Brenta @ 2008-10-02  8:18 UTC (permalink / raw)


Niklas Holsti wrote:
> Jean-Pierre Rosen wrote:
> > Adam Beneschan a �crit :
> >
> >> Yep, the advantage of that is that if the people who design the
> >> Internet communication protocols suddenly decide that IP addresses are
> >> going to use 9-bit bytes, all you have to do is change one constant,
> >> rather than try to find all the 8's in your code and change them,
> >> which is more error-prone.
> >>
> > Reminds me of an old advice explaining why named constants were better
> > than numerical values. They took the example of Pi, saying it would make
> >  maintenance easier "should the value of Pi change" :-)

That's funny because I thought about the same thing when reading
Adam's comment.

> That reminds me of a former colleague, some decades ago, who was
> tearing his hair out because his astronomical calculations with a
> FORTRAN program were going wrong. Turns out he had the value of PI
> as a variable in a COMMON area, after an array, and an indexing
> error on this array was changing the value of PI, affecting several
> trigonometric formulae...

Looks like programmers have already made every mistake that could be
made at least once :) Actually, that's what brought me to Ada in the
first place. I thought to myself: "computer science and software
engineering have been in existence for 50 years; yet we keep on making
the same old stupid mistakes. Surely someone, somewhere, has found a
way to learn from our mistakes and prevent them in the future?"

--
Ludovic Brenta.



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

* Re: Ada Recursion with strings
  2008-10-01 14:29           ` Adam Beneschan
  2008-10-01 15:18             ` Jean-Pierre Rosen
@ 2008-10-03  6:54             ` Maciej Sobczak
  2008-10-03  7:38               ` Jean-Pierre Rosen
  2008-10-03  9:06               ` Dmitry A. Kazakov
  1 sibling, 2 replies; 19+ messages in thread
From: Maciej Sobczak @ 2008-10-03  6:54 UTC (permalink / raw)


On 1 Paź, 16:29, Adam Beneschan <a...@irvine.com> wrote:

> > Bits_Per_Byte   : constant := 8;
> > Values_Per_Byte : constant := 2 ** Bits_Per_Byte;
>
> Yep, the advantage of that is that if the people who design the
> Internet communication protocols suddenly decide that IP addresses are
> going to use 9-bit bytes, all you have to do is change one constant,
> rather than try to find all the 8's in your code and change them,
> which is more error-prone.

The people who design the Internet communication protocols will
*never* decide that IP addresses are going to use 9-bit bytes, exactly
because those tons of software that use 8's in the code. Note also
that the majority of software cannot be recompiled because the source
code is not available or just missing.

I agree that constants should be named, but even most strict coding
standards allow several literals to be unnamed and the smallest set is
0 and 1. I think that 2 and 8 are on the border of acceptance too.

So:

Buffer_Size := Get_Buffer_Size_In_Bytes;
Buffer_Bits := Buffer_Size * 8;

Is there any benefit from making the above more verbose with
additional named constant?
Or would it be confusing to anyone to see literal 8 (or some formula
that is build around it) in representation clause? I don't think so.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



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

* Re: Ada Recursion with strings
  2008-10-03  6:54             ` Maciej Sobczak
@ 2008-10-03  7:38               ` Jean-Pierre Rosen
  2008-10-03  9:06               ` Dmitry A. Kazakov
  1 sibling, 0 replies; 19+ messages in thread
From: Jean-Pierre Rosen @ 2008-10-03  7:38 UTC (permalink / raw)


Maciej Sobczak a �crit :
> Buffer_Size := Get_Buffer_Size_In_Bytes;
> Buffer_Bits := Buffer_Size * 8;
> 
> Is there any benefit from making the above more verbose with
> additional named constant?
> Or would it be confusing to anyone to see literal 8 (or some formula
> that is build around it) in representation clause? I don't think so.
Agreed. An even more convincing case of where you should *not* use named 
constants is X**2. An abuse of the rule "don't use literals" could not 
produce anything but X**Two, which is in no way better...

The rule should be: if the number has a logical meaning give it a name 
(i.e. if there are 4 people in a car, call it People_In_Car, not 4). But 
if it is a 2, and cannot change under any condition, by all means, call 
it "2" !

(and yes, with AdaControl, you can specify exceptions to the rule that 
forbids literals, like as exponent for example).
-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



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

* Re: Ada Recursion with strings
  2008-10-03  6:54             ` Maciej Sobczak
  2008-10-03  7:38               ` Jean-Pierre Rosen
@ 2008-10-03  9:06               ` Dmitry A. Kazakov
  1 sibling, 0 replies; 19+ messages in thread
From: Dmitry A. Kazakov @ 2008-10-03  9:06 UTC (permalink / raw)


On Thu, 2 Oct 2008 23:54:23 -0700 (PDT), Maciej Sobczak wrote:

> I agree that constants should be named, but even most strict coding
> standards allow several literals to be unnamed and the smallest set is
> 0 and 1. I think that 2 and 8 are on the border of acceptance too.

Yes, but not always so. In some cases you might like to have "additive
zero" instead of 0. After all literal is a name too. The difference is
whether the name is self explaining for the reader. The name 3.1415... is
worse than Pi, 8 is probably better than Octet_Size.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

end of thread, other threads:[~2008-10-03  9:06 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-30 20:30 Ada Recursion with strings Joe
2008-09-30 22:24 ` Adam Beneschan
2008-09-30 22:24 ` Jeffrey R. Carter
2008-10-01  0:13   ` Joe
2008-10-01  0:27     ` Jeffrey R. Carter
2008-10-01  1:15       ` Robert A Duff
2008-10-01 11:34         ` Jeffrey R. Carter
2008-10-01 14:29           ` Adam Beneschan
2008-10-01 15:18             ` Jean-Pierre Rosen
2008-10-01 16:47               ` Georg Bauhaus
2008-10-02  7:24               ` Niklas Holsti
2008-10-02  8:18                 ` Ludovic Brenta
2008-10-03  6:54             ` Maciej Sobczak
2008-10-03  7:38               ` Jean-Pierre Rosen
2008-10-03  9:06               ` Dmitry A. Kazakov
2008-10-01  0:28     ` Adam Beneschan
2008-10-01  9:48       ` Georg Bauhaus
2008-10-01 10:22         ` Dmitry A. Kazakov
2008-10-01  3:01 ` anon

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