From: Martin <martin@thedowies.com>
Subject: Re: Ada2012 : In praise of 'for ... of ... loop'...
Date: Wed, 30 May 2012 01:10:22 -0700 (PDT)
Date: 2012-05-30T01:10:22-07:00 [thread overview]
Message-ID: <371a4b67-1969-4cd5-90f4-d58a9b276f29@googlegroups.com> (raw)
In-Reply-To: <4fc4e51d$0$6566$9b4e6d93@newsspool4.arcor-online.net>
On Tuesday, May 29, 2012 4:02:47 PM UTC+1, Georg Bauhaus wrote:
> On 29.05.12 16:29, Martin wrote:
> > This addition to Ada is brilliant...I've just been refactoring some Ada05 into Ada2012 and this just makes the code so much clearer! No more nested subprograms (of nested subprograms (of nested ...)).
> >
> > -- Martin
>
> Could you illustrate a bit? How much of the clarity is caused, if
> so, by being shorter, using assignment in place of nested subprograms,
> reducing the number of names, or ...
Here's a very small example... what we have now:
procedure Save (This : in out Configuration;
Filename : String;
Header_Comments : String_Vectors.Vector := String_Vectors.Empty_Vector;
Include_Last_Saved_Time : Boolean := False) is
use type String_Vectors.Vector;
File : File_Type;
begin
begin
Create (File, Out_File, Filename);
exception
when others =>
Open (File, Out_File, Filename);
end;
for Comment of Header_Comments loop
Put_Line (File, Comment_Str & Comment);
end loop;
if Include_Last_Saved_Time then
Put_Line (File, Comment_Str & "Last saved: " & Image (Date => Clock));
end if;
for Section of This.Sections loop
Put_Line (File, "[" & To_String (Section.Name) & "]");
for Pair of Section.Pairs loop
Put_Line (File, To_String (Pair.Key) & "=" & To_String (Pair.Value));
end loop;
end loop;
Close (File);
exception
-- implementation detail
end Save;
what we used to have (mocked up!):
procedure Save (This : in out Configuration;
Filename : String;
Header_Comments : String_Vectors.Vector := String_Vectors.Empty_Vector;
Include_Last_Saved_Time : Boolean := False) is
use type String_Vectors.Vector;
File : File_Type;
-----------
-- Write --
-----------
procedure Write (C : Section_Vectors.Cursor) is
-------------------
-- Write_Comment --
-------------------
procedure Write_Comment (C : String_Vectors.Cursor) is
begin
Put_Line (File, Comment_Str & String_Vectors.Element (C));
end Write_Comment;
-----------
-- Write --
-----------
procedure Write (C : Key_Value_Pair_Vectors.Cursor) is
Pair : constant Key_Value_Pair := Key_Value_Pair_Vectors.Element (C);
begin
Put_Line (File, To_String (Pair.Key) & "=" & To_String (Pair.Value));
end Write;
This_Section : constant Section := Section_Vectors.Element (C);
begin
Put_Line (File, "[" & To_String (This_Section.Name) & "]");
This_Section.Pairs.Iterate (Write'Access);
end Write;
begin
begin
Create (File, Out_File, Filename);
exception
when others =>
Open (File, Out_File, Filename);
end;
Header_Comments.Iterator (Write_Comment'Access);
if Include_Last_Saved_Time then
Put_Line (File, Comment_Str & "Last saved: " & Image (Date => Clock));
end if;
This.Sections.Iterate (Write'Access);
Close (File);
exception
-- implementation detail
end Save;
That's 54 lines down to 29 lines, no 'Access and it's perfectly clear what's happening and it happens in a single subprogram. No need to talk about Cursor or calls to Element (C)...
...It also enhances the value of other nested subprograms - they're probably doing something more interesting, so I don't mind the comment-box headers for them as much as I used to.
All thanks to a little syntatic sugar.
To my eyes, this a big win.
-- Martin
next prev parent reply other threads:[~2012-05-30 8:10 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-29 14:29 Ada2012 : In praise of 'for ... of ... loop' Martin
2012-05-29 15:02 ` Georg Bauhaus
2012-05-30 8:10 ` Martin [this message]
2012-05-30 8:15 ` Thomas Løcke
2012-05-30 16:21 ` Pascal Obry
2012-05-30 19:30 ` Jeffrey Carter
2012-05-30 20:54 ` Pascal Obry
2012-05-31 14:09 ` Martin
2012-05-31 20:58 ` tonyg
2012-05-30 22:26 ` Georg Bauhaus
2012-05-30 22:45 ` Georg Bauhaus
2012-06-07 0:19 ` Randy Brukardt
2012-06-07 12:42 ` Georg Bauhaus
2012-06-07 12:54 ` Georg Bauhaus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox