Thread Codereview Argument Validation (9 answers)
Opened by Kuerbis at 2013-08-26 15:50

Kuerbis
 2013-08-26 15:50
#169755 #169755
User since
2011-03-20
942 Artikel
BenutzerIn
[default_avatar]
Hallo,

ist hier das zweite schöner als das erste?

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env perl
use strict;
use warnings;

sub _validate_arguments {
    my ( $first_arg, $second_arg ) = @_;
    croak "Called without arguments." if @_ < 1;
    croak "Called with " . scalar @_ . " arguments. Expected arguments: 1 or 2." if @_ > 2;
    croak "The first argument is not defined." if ! defined $first_arg;
    ...
    ...
    carp "The first argument refers to an empty list!" if ! @$first_arg;
    return $first_arg, $second_arg;
}

sub my_function {
    my ( $first_arg, $second_arg ) = _validate_arguments( @_ );
    return if ! @$first_arg;
    ...
    ...
}



Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env perl
use strict;
use warnings;

sub _validate_arguments {
    my ( $first_arg, $second_arg, $arg_count ) = @_;
    croak "Called without arguments." if $arg_count < 1;
    croak "Called with $arg_count arguments. Expected arguments: 1 or 2." if $arg_count > 2;
    croak "The first argument is not defined." if ! defined $first_arg;
    ...
    ...
    carp "The first argument refers to an empty list!" if ! @$first_arg;
    return;
}

sub my_function {
    my ( $first_arg, $second_arg ) = @_;
    _validate_arguments( $first_arg, $second_arg, scalar @_ );
    return if ! @$first_arg;
    ...
    ...
}

View full thread Codereview Argument Validation