comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Ada and "early return" - opinion/practice question
Date: Mon, 15 Mar 2021 11:12:37 -0700 (PDT)	[thread overview]
Message-ID: <cb298727-0c0b-454c-bf64-3f264096f899n@googlegroups.com> (raw)
In-Reply-To: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com>

On Monday, March 15, 2021 at 10:46:39 AM UTC-6, wrote:
> I hope this isn't a FAQ (it's hard to find relevant articles) but can someone guide me on the 'normal' treatment in Ada style of what appears to be referred to (by C/C++ programmers) as early-return. 

Exceptions, typically.
Sometimes a return itself, typically in a procedure though.

> For example, you have a C++ function (pseudo code sort of thing): 
> 
> <sometype> fn(<some parameters>) 
> { 
> if (<some undesirable condition 1>) 
> { 
> return <something bad happened 1>; 
> } 
> 
> if (<some undesirable condition 2>) 
> { 
> return <something bad 2>; 
> } 
> 
> if (<some undesirable condition 3>) 
> { 
> return <something bad 3>; 
> } 
> 
> // Only get here if everything's good... 
> <do some real stuff> 
> return <something good>; 
> } 

This is the style for a system that I'm looking at currently, it's interfacing for DLL calls related to some special hardware. My initial method is going to write a thin binding, then "thicken" it up with subtypes and procedures/functions tailored to the system so something like:

Subtype DLL_Return is Interfaces.Unsigned_8; -- Or whatever

Type Temp_Subtype is (A_OK, Out_Of_Range )
  with Size => DLL_Return'Size;

For Temp_Subtype use
( A_OK                => 14,
  Out_Of_Range => 72
);

Function Check_Temp return Temp_Subtype is
  Return_Value : Constant DLL_Return := Do_Temperture_Check;
  Result : Temp_Subtype renames Convert( Return_Value ); -- Convert is instantiation of unchecked_conversion.
Begin
  if DEBUGGING then
    Ada.Text_IO.Put_Line( "Called Check_Temp, got: " & DLL_Return'Image(Return_Value) );
  end if;
  Return Result;
End;

Heck, since the translations are so regular I could probably use Generics to handle leveraging things, meaning the most work would be getting the actual type/values defined.

> I've probably mentioned this before, but it's a long time since I used Ada in anger and I don't remember seeing stuff like that when I did use Ada a lot; does anyone write stuff like that in Ada? 
> 
> When I first learnt to program properly it was using Pascal with, as I remember it, only one return from a function being allowed,
Your memory is correct... for Pascal.
Ada has always allowed multiple return statements in a subprogram, with a "return" in a procedure acting as a "jump to the end, do clean-up".

> so over the years I've mostly looked at positive conditions and indented stuff, pulling the stuff in the middle out into its own procedure or function where appropriate, but you see so many people defending this style in C/C++ that I wonder whether it really is defensible? 

No, it's not defensible... at least IMO.

  parent reply	other threads:[~2021-03-15 18:12 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 16:46 Ada and "early return" - opinion/practice question John McCabe
2021-03-15 17:02 ` Dmitry A. Kazakov
2021-03-15 17:29   ` John McCabe
2021-03-16  7:08   ` Randy Brukardt
2021-03-15 17:31 ` Stephen Leake
2021-03-15 17:43   ` John McCabe
2021-03-15 18:15     ` Shark8
2021-03-15 20:39       ` Simon Wright
2021-03-15 20:56         ` Chris Townley
2021-03-16  7:19           ` Stéphane Rivière
2021-03-16 10:31             ` Jeffrey R. Carter
2021-03-16  8:28           ` John McCabe
2021-03-16 20:34         ` Simon Wright
2021-03-17  8:05           ` John McCabe
2021-03-17 11:43             ` Simon Wright
2021-03-18  8:08               ` John McCabe
2021-03-18 16:27                 ` Stephen Leake
2021-03-20 13:41                   ` John McCabe
2021-03-15 19:05     ` Paul Rubin
2021-03-16  8:38       ` John McCabe
2021-03-16  9:03     ` Stephen Leake
2021-03-16  9:21       ` John McCabe
2021-03-16  8:24   ` John McCabe
2021-03-16  9:13     ` Stephen Leake
2021-03-16 11:51       ` John McCabe
2021-03-16  9:46     ` Dmitry A. Kazakov
2021-03-16 10:46     ` Jeffrey R. Carter
2021-03-17  8:18       ` John McCabe
2021-03-17 10:06         ` AdaMagica
2021-03-15 18:12 ` Shark8 [this message]
2021-03-15 18:20   ` John McCabe
2021-03-15 19:08   ` Paul Rubin
2021-03-15 19:37     ` Shark8
2021-03-16  7:17     ` Randy Brukardt
2021-03-16  9:26       ` Paul Rubin
2021-03-16  9:53       ` Dmitry A. Kazakov
2021-03-16  9:16     ` Stephen Leake
2021-03-16 11:04       ` Niklas Holsti
2021-03-16 22:49         ` Stephen Leake
2021-03-15 18:37 ` Jeffrey R. Carter
2021-03-15 18:54   ` John McCabe
replies disabled

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