comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: My first solution
Date: Wed, 15 Jun 2011 12:43:15 -0700
Date: 2011-06-15T12:43:15-07:00	[thread overview]
Message-ID: <itb4pq$qfg$1@tornado.tornevall.net> (raw)
In-Reply-To: <3420583b-a063-4634-9989-580888e38eee@f2g2000yqh.googlegroups.com>

On 06/14/2011 11:01 PM, juanmiuk wrote:
>
>       function Format_Num ( Width_Num  : Integer;

What does it mean to call this function with Width_Num not in Positive? Using an 
appropriate subtype in cases like this helps document how the subprogram should 
be used.

>                                         The_Number : Integer ) return
> String is
>
>          Temp_Length : My_Num := My_Num'Image(The_Number)'Length;
>          Separador      : String := (Width_Num - Temp_Length) * ' ';

These are both never assigned to, so they can be declared as constants. While 
this can assist the compiler in optimization, its primary benefit is letting the 
reader know you never change these.

Note that the Left parameter of Ada.Strings.Fixed."*" is subtype Natural. This 
will raise Constraint_Error if Width_Num - Temp_Length < 0. This helps define 
what subtype Width_Num should be, indicates some comments are probably needed to 
describe a precondition [Width_Num >= My_Num'Image (The_Number)'Length], and 
perhaps points to some ways the function can be improved [if Width_Num < 
My_Num'Image (The_Number)'Length, return My_Num'Image (The_Number)?]

"Separador" would probably be named "Padding" in English.

>       for I in My_Num range 1 .. 10 loop

for I in My_Num loop

What you have done here is effectively duplicate the functionality of 
Ada.Text_IO.Integer_IO.Put.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



      parent reply	other threads:[~2011-06-15 19:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-15  6:01 My first solution juanmiuk
2011-06-15  6:09 ` juanmiuk
2011-06-15  6:39 ` Ludovic Brenta
2011-06-15 18:39 ` onox
2011-06-15 19:43 ` Jeffrey Carter [this message]
replies disabled

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