Skip to content

Commit b2786a3

Browse files
committed
Resolve "Code snippets should use better names than single characters"
1 parent 1bf6569 commit b2786a3

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

courses/fundamentals_of_ada/273_subprogram_contracts/02-preconditions_and_postconditions.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ Defensive Programming
7171

7272
.. code:: Ada
7373
74-
procedure Push (S : Stack) is
75-
Entry_Length : constant Positive := Length (S);
74+
procedure Push (The_Stack : Stack) is
75+
Entry_Length : constant Positive := Length (The_Stack);
7676
begin
77-
pragma Assert (not Is_Full (S)); -- entry condition
77+
pragma Assert (not Is_Full (The_Stack)); -- entry condition
7878
[...]
79-
pragma Assert (Length (S) = Entry_Length + 1); -- exit condition
79+
pragma Assert (Length (The_Stack) = Entry_Length + 1); -- exit condition
8080
end Push;
8181
8282
* Subprogram contracts are an **assertion** mechanism
@@ -85,15 +85,15 @@ Defensive Programming
8585

8686
.. code:: Ada
8787
88-
procedure Force_Acquire (P : Peripheral) is
88+
procedure Force_Acquire (Resource : Peripheral) is
8989
begin
90-
if not Available (P) then
90+
if not Available (Resource) then
9191
-- Corrective action
92-
Force_Release (P);
93-
pragma Assert (Available (P));
92+
Force_Release (Resource);
93+
pragma Assert (Available (Resource));
9494
end if;
9595
96-
Acquire (P);
96+
Acquire (Resource);
9797
end;
9898
9999
-----------------------------
@@ -196,17 +196,17 @@ Quiz
196196
.. code:: Ada
197197
198198
-- Convert string to Integer
199-
function From_String ( S : String ) return Integer
199+
function To_Integer ( S : String ) return Integer
200200
with Pre => S'Length > 0;
201201
202-
procedure Do_Something is
203-
I : Integer := From_String ("");
202+
procedure Print_Something is
203+
I : Integer := To_Integer ("");
204204
begin
205205
Put_Line (I'Image);
206-
end Do_Something;
206+
end Print_Something;
207207
208-
Assuming :ada:`From_String` is defined somewhere, what happens
209-
when :ada:`Do_Something` is run?
208+
Assuming :ada:`To_Integer` is defined somewhere, what happens
209+
when :ada:`Print_Something` is run?
210210

211211
A. "0" is printed
212212
B. Constraint Error exception
@@ -217,7 +217,7 @@ when :ada:`Do_Something` is run?
217217

218218
Explanations
219219

220-
The call to :ada:`From_String` will fail its precondition, which is considered
220+
The call to :ada:`To_Integer` will fail its precondition, which is considered
221221
an :ada:`Assertion_Error` exception.
222222

223223
------
@@ -226,25 +226,25 @@ Quiz
226226

227227
.. code:: Ada
228228
229-
function Area (L : Positive; H : Positive) return Positive is
230-
(L * H)
229+
function Area (Length : Positive; Height : Positive) return Positive is
230+
(Length * Height)
231231
with Pre => ?
232232
233233
Which pre-condition is necessary for :ada:`Area` to calculate the correct result for
234234
all values :ada:`L` and :ada:`H`
235235

236-
A. ``L > 0 and H > 0``
237-
B. ``L < Positive'Last and H < Positive'Last``
238-
C. ``L * H in Positive``
236+
A. ``Length > 0 and Height > 0``
237+
B. ``Length < Positive'Last and Height < Positive'Last``
238+
C. ``Length * Height in Positive``
239239
D. :answer:`None of the above`
240240

241241
.. container:: animate
242242

243243
Explanations
244244

245245
A. Parameters are :ada:`Positive`, so this is unnecessary
246-
B. :ada:`L = Positive'Last-1 and H = Positive'Last-1` will still cause an overflow
246+
B. :ada:`Length = Positive'Last-1 and Height = Positive'Last-1` will still cause an overflow
247247
C. Classic trap: the check itself may cause an overflow!
248248

249-
Preventing an overflow requires using the expression :ada:`Integer'Last / L <= H`
249+
Preventing an overflow requires using the expression :ada:`Integer'Last / Length <= Height`
250250

courses/fundamentals_of_ada/273_subprogram_contracts/04-in_practice.rst

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ No Secret Precondition Requirements
3636

3737
.. code:: Ada
3838
39-
package P is
39+
package Some_Package is
4040
function Foo return Bar
4141
with Pre => Hidden; -- illegal private reference
4242
private
4343
function Hidden return Boolean;
44-
end P;
44+
end Some_Package;
4545
4646
---------------------------------------
4747
Postconditions Are Good Documentation
@@ -92,18 +92,18 @@ Postcondition Compared to Their Body: Example
9292

9393
.. code:: Ada
9494
95-
function Greatest_Common_Denominator (A, B : Natural)
95+
function Greatest_Common_Denominator (Num1, Num2 : Natural)
9696
return Integer with
97-
Post => Is_GCD (A,
98-
B,
97+
Post => Is_GCD (Num1,
98+
Num2,
9999
Greatest_Common_Denominator'Result);
100100
101-
function Is_GCD (A, B, Candidate : Integer)
101+
function Is_GCD (Num1, Num2, Candidate : Integer)
102102
return Boolean is
103-
(A rem Candidate = 0 and
104-
B rem Candidate = 0 and
105-
(for all K in 1 .. Integer'Min (A,B) =>
106-
(if (A rem K = 0 and B rem K = 0)
103+
(Num1 rem Candidate = 0 and
104+
Num2 rem Candidate = 0 and
105+
(for all K in 1 .. Integer'Min (Num1,Num2) =>
106+
(if (Num1 rem K = 0 and Num2 rem K = 0)
107107
then K <= Candidate)));
108108
109109
----------------------
@@ -145,24 +145,24 @@ Subprogram Contracts on Private Types
145145

146146
.. code:: Ada
147147
148-
package P is
149-
type T is private;
150-
procedure Q (This : T) with
151-
Pre => This.Total > 0; -- not legal
148+
package Bank is
149+
type Account is private;
150+
procedure Process_Transaction (This : Account) with
151+
Pre => This.Balance > 0; -- not legal
152152
...
153-
function Current_Total (This : T) return Integer;
153+
function Current_Balance (This : Account) return Integer;
154154
...
155-
procedure R (This : T) with
156-
Pre => Current_Total (This) > 0; -- legal
155+
procedure R (This : Account) with
156+
Pre => Current_Balance (This) > 0; -- legal
157157
...
158158
private
159-
type T is record
160-
Total : Natural ;
159+
type Account is record
160+
Balance : Natural;
161161
...
162162
end record;
163-
function Current_Total (This : T) return Integer is
164-
(This.Total);
165-
end P;
163+
function Current_Balance (This : Account) return Integer is
164+
(This.Balance);
165+
end Bank;
166166
167167
-----------------------------------
168168
Preconditions or Explicit Checks?

0 commit comments

Comments
 (0)