Re: Code Comprehension
- From: "Rob Thorpe" <robert.thorpe@xxxxxxxxxxxx>
- Date: 28 Aug 2006 03:59:20 -0700
John W. Krahn wrote:
jimmaureenrogers@xxxxxxxxxxxxxxxx wrote:
Readability is a somewhat subjective attribute of code.
Agreed.
I humbly submit my concept of a readable solution to the problem you
have provided.
with Ada.Strings.Fixed;
procedure Remove_Unique (Target : in out String;
Source : in String ) is
Match_Found : Boolean;
I : Positive;
Limit : Positive;
begin
if Target'Length > 0 and Source'Length > 0 then
I := Target'First; -- First character in target
Limit := Target'Last; -- Upper limit of search
while I <= Limit loop
Match_Found := False;
for J in Source'range loop
if Target(I) = Source(J) then
Match_Found := True;
exit;
end if;
end loop;
if Match_Found then
I := I + 1;
else
-- delete the unmatched character
-- decrement the upper limit of the search
Ada.Strings.Fixed.Delete(Target,I,I);
Limit := Limit - 1;
end if;
end loop;
end if;
end Remove_Unique;
You could write that in Perl as:
sub fun { $_[0] =~ s/[\Q$_[1]\E]//g }
It is definitely shorter and probably easier to debug. As to readability, it
depends on how well you know Perl. ;-)
You could also write unidiomatic perl code that's a little more
readable
sub remove_unique
{
my $s = $_[0];
return s/[\Q$s\E]//g;
}
And you could document the regex. (The above may be wrong I haven't
checked it).
Perl can be made reasonably readable, it's just more work.
.
- Follow-Ups:
- Re: Code Comprehension
- From: John W. Krahn
- Re: Code Comprehension
- References:
- Code Comprehension
- From: jj
- Re: Code Comprehension
- From: jimmaureenrogers@xxxxxxxxxxxxxxxx
- Re: Code Comprehension
- From: John W. Krahn
- Code Comprehension
- Prev by Date: Re: Fibonacci implementation
- Next by Date: Re: Code Comprehension
- Previous by thread: Re: Code Comprehension
- Next by thread: Re: Code Comprehension
- Index(es):
Relevant Pages
|