Re: Code Comprehension



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.

.



Relevant Pages

  • Re: Need help understanding how a file input block works
    ... here is a section of the Perl code that I am having ... ^ says 'match if we are at the start of the string', ... 'next' is documented in perldoc perl, in the sextion "Loop Control". ... which will stop you from using globals by accident. ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: How to get "perl -V"s output programmatically?
    ... > JB> Oh, I agree, the most important thing to me is readability, and I try ... Sadly I miss some of the Perl fun, ... sure you can do many string things without regexes, ...
    (comp.lang.perl.misc)
  • Re: efficiency question about split
    ... out of the splitting of the big string $Dictionary->contents ... However a temp file would defeat the purpose. ... And 2) - what is a very general perl question: ... Since I will be running the loop many many times, ...
    (comp.lang.perl.misc)
  • Re: bytes To Hex
    ... if i have a long loop ..i tend to do as little stack pops as i can ... which tends to bottleneck a lot more. ... paragon of readability. ... StringBuilder isn't a cure-all for string performance problems. ...
    (microsoft.public.dotnet.languages.csharp)