Jerunh
Jerunh

Reputation: 514

Can Ada95 child subunits be used to separate code for maintainability?

I have created an Ada class whose implementation has become quite large. I have a multiple body only methods that I would like move to a separate file for maintainability/readability purposes.

Q.1 In Ada, Is it wrong/undesirable for a Parent Body unit to depend on a child Unit?

EDIT: The above question is too vague and any answer would be subjective.

Q.2 How can I divide my code into separate files without creating an over abundance of files?

Upvotes: 0

Views: 267

Answers (2)

Riccardo B.
Riccardo B.

Reputation: 291

About the "correctness" of having a body (not a spec, you would get a circular dependency) depending on a child package, I use that all the time and I find it very convenient, especially to implement some complex data structure/service used internally by my package. The Ada Style guide (2005) agrees with me when it says

Use child packages to implement a subsystem

I guess this is your case too.

Upvotes: -1

Simon Wright
Simon Wright

Reputation: 25501

In fact you can have separate bodies for packages, protected and task types, as well as subprograms.

So you could say

package Large is
   procedure A;
   procedure B;
end Large;

package body Large is
   package Implementation is
      procedure A;
      procedure B;
   end Implementation;
   package body Implementation is separate;
   procedure A renames Implementation.A;
   procedure B renames Implementation.B;
end Large;

with Ada.Text_IO; use Ada.Text_IO;
separate (Large)
package body Implementation is
   procedure A is
   begin
      Put_Line ("large.implementation.a");
   end A;
   procedure B is
   begin
      Put_Line ("large.implementation.b");
   end B;
end Implementation;

and to check

with Large;
procedure Check_Separate_Implementation is
begin
   Large.A;
   Large.B;
end Check_Separate_Implementation;

Equally, you could have Implementation as a child package:

private package Large.Implementation is
   procedure A;
   procedure B;
end Large.Implementation;

with Large.Implementation;
package body Large is
   procedure A renames Implementation.A;
   procedure B renames Implementation.B;
end Large;

The only difference I can see is that other child packages of Large would be able to see Implementation in the child package version but not in the separate version.

The Program Structure section of the Ada Style Guide (2005 edition) strongly prefers use of child packages rather than subunits, and says (in answer to your style question)

In preference to nesting in a package body, use a private child and with it to the parent body.

but, as you might expect, opinions will differ on this. You could read the Rationale section of the Guide and see how it fits your particular situation.

Upvotes: 4

Related Questions