Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Evil Interview Questions

by Herkum (Parson)
on Feb 10, 2008 at 20:38 UTC ( [id://667291]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Evil Interview Questions
in thread Evil Interview Questions

I rarely found these types of question fun, your right the attitude matters, but the people who use these questions are not looking for a good programmer, they are looking for a technical programmer which is not the same thing. It is easier to identify a persons technical attributes than there qualities as a good programmer. Take this example object;

package Foo::Bar; sub new { my $class = shift; my %args = @_; my $self = {}; bless $self, $class; $self->{_name} = $args{name} if $args{name}; return $self; } sub name { my $self = shift; return $self->{_name}; }

This is the implementation of the object.

my $bar = Foo::Bar->new( name => 'Bob Lambert' ); print "Name of my object " . $bar->name . "\n"; # method call print "Name from my hash " . $bar->{_name} . "\n"; # hash key

Can you see the problem? Technically they are both correct, but one of these basically creates havoc that cannot be overcome by intimate knowledge of the language.

The print statements display the same value because they access the same data. The call to access the data directly via the hash reference forces it to be a static implementation. It creates maintenance headaches and you cannot update your object without breaking code.

Focusing on technical trivia is not a replacement for development/design process.

Update: I did sloppy work on my object code (pointed out by shmem and I fixed it to reflect the problems he pointed out)

Replies are listed 'Best First'.
Re^4: Evil Interview Questions
by shmem (Chancellor) on Feb 10, 2008 at 23:08 UTC
    Can you see the problem?
    Well, some... 1 not so, and 5 serious problems:
    package Foo::Bar; # (1) strict, warnings? sub new { my %args = shift; # (2) first argument is class name. # (3) shift? really? even if @_, then # odd number of elements in hash? my $self; # (4) uninitialized scalar... bless $self; # ... can't be blessed (not a reference) # (5) not using 2-arg-bless? inheritance? if ($args{name}) { $self->{_name} } # (6) $self->{_name} /what/ ? return $self; } sub name { my $self = shift; return $self->{_name}; }

    Granted, your object implementation is just a quickly-whipped-up example. Don't let the heat of the discussion lend you to write bad code!

    But anyways -

    Technically they are both correct, but one of these basically creates havoc that cannot be overcome by intimate knowledge of the language.

    Oh, it can be overcome - if you use that knowledge to implement the object in a good way in the first place:

    package Foo::Bar; use Alter ego => {}; use strict; use warnings; sub new { my $class = shift; @_ % 2 and die "odd number of elements in \@_, aborted"; my %args = @_; my $self = \do { my $obj }; bless $self, $class; ego($self)->{_name} = $args{name} if $args{name}; return $self; } sub name { my $self = shift; defined( ego $self) or die "not a valid object"; return ego($self)->{_name} } package main; my $bar = Foo::Bar->new( name => 'Bob Lambert' ); print "Name of my object " . $bar->name . "\n"; # method call print "Name from my hash " . $bar->{_name} . "\n"; # hash key __END__ Name of my object Bob Lambert Not a HASH reference at - line 23.

    No room for mistakes - the $bar object is just an empty scalar reference. It is better to not create the pits than avoiding the pitfalls. Or as a friend of mine uses to say

    mejor la seguridad que la policia.

    better is security than the police

    ;-)

    update:

    The validity of hash key lookup for object attributes depends entirely on the purpose and declared interface of the object - its contract. Hash lookups are much faster than method calls, and there might be a speed concern.

    But back to the discussion - these sort of interview questions make sense, to test the depth of detail knowledge. If they are the only ones, the answers will reveal only a small part of the person interviewed. It certainly is short-sighted to only rely upon, or over-estimate, the technical quiz.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      BUSTED! You are right, I did not really go do good work for the object code (the problem of when you get into a hurry).

      However, I will stick to my guns with, design/process > technical knowledge.

        Before I post code here, I run it - that's what we preach to newcomers on a daily basis... ;-)

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://667291]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found