6
\$\begingroup\$

My code works perfectly fine but I was wondering if there was any better ways I could write my code like tidy it up a bit or rewrite a part of it. I want to learn how I can write my code in a better way. The code prompts the user to type the details of 3 different boats and then prints them.

program BoatDetails;
uses TerminalUserInput;

type 
Boat = record
    ID : integer;
    Manufacturer : string;
    Model : string;
    Registration : string;
end;

Boats = Array of Boat;

function ReadBoat(const prompt : string): Boat;
begin
    WriteLn(prompt);
    ReadBoat.ID := readInteger('Please enter the Boat ID: ');
    ReadBoat.Manufacturer := readString('Please enter the manufacturer of the Boat: ');
    ReadBoat.Model := readString('Please enter the model of the Boat: ');
    ReadBoat.Registration := readString('Please enter the registration number for the Boat: ');
end;

procedure WriteBoat(b: Boat);
begin
    WriteLn('ID - ', b.ID);
    WriteLn('Manufacturer - ', b.Manufacturer);
    WriteLn('Model - ', b.Model);   
    WriteLn('Registration - ', b.Registration);
end;

function ReadAllBoats(count: integer): Boats;
var i : integer;
begin
     SetLength(result, count);
     for i := Low(result) to High(result) do
         result[i] := readBoat('Enter Boat details');  
end;

procedure WriteAllBoats(BoatArray: Boats);
var i : integer;
begin 
    for i := Low(BoatArray) to High(BoatArray) do
        WriteBoat(BoatArray[i]);
end;

procedure Main();
     var Boats: Array of Boat;
         index: integer;
begin
    Boats := ReadAllBoats(3);
    WriteAllBoats(Boats);
end;

begin
    Main();
end.
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$
  • Your program header
    program BoatDetails;
    
    does not list input and output as program parameters, but you are (implicitly) making use of input and output. You are apparently using a Pascal dialect that ignores the program header, so it does not cause any problems for you. Therefore I would actually recommend omitting it entirely. For reference, in Pascal a proper program header would look like this:
    program boatDetails(input, output);
    
  • This design
    procedure Main();
         var Boats: Array of Boat;
             index: integer;
    begin
        Boats := ReadAllBoats(3);
        WriteAllBoats(Boats);
    end;
    
    begin
        Main();
    end.
    
    is totally unnecessary. It is equivalent to
    var Boats: Array of Boat;
        index: integer;
    begin
        Boats := ReadAllBoats(3);
        WriteAllBoats(Boats);
    end.
    
    Use routines as reusable pieces of code, or if isn’t meant to be reused, then at least to structure your code, e. g. you can limit the lifetime of a variable this way.
  • Your readBoat function has the following header:
    function ReadBoat(const prompt : string): Boat;
    
    It takes one parameter. However, in the entire program you are calling readBoat at one place with the same constant string. Use parameters if it somehow “changes” the routine’s behavior, in other words if “the result” of the routine depends on the parameter, its value. Here I recommend to place the prompt at the call site of readBoat, like so:
         for i := Low(result) to High(result) do
         begin
             writeLn('Enter Boat details');
             result[i] := readBoat;
         end;
    
  • In ReadAllBoats you are using the implicit result variable your compiler provides to you. But in ReadBoat you are using Pascal-style result definition, i. e. the variable bearing the same name as of the function (here ReadBoat). I think it’s best to stick to one style.
  • Last, but not least: Use comments. Maybe for this program it’s unnecessary, so it’s fine. I just noticed the lack of comments. At least a brief summary at the top of your source code file would be nice, though.
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.