Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
A dúvida é a seguinte.
Qual seria a melhor forma de programar ?
1- Criando códigos sem utilizar funções/métodos expecíficos de uma dada linguagem ?
2- Utilizar funções/métodos expecíficos que só um programador desta linguagem entenda ?
Exemplo 1
No primeiro caso, apenas um programador PHP sabe o que está acontecendo, caso seja necessário passar este código para JAVA por exemplo, um programador JAVA teria dificuldades em entender o que está acontecendo, teria que estudar um pouco de PHP.
<?php
/**
* Client
*/
abstract class Client
{
protected $storage;
public function setName( $name )
{
$this->storage[ 'name' ] = $name;
}
public function getName( )
{
return $this->storage[ 'name' ];
}
} public function setLogin( $login )
{
$this->storage[ 'login' ] = $login;
}
public function getLogin( )
{
return $this->storage[ 'login' ];
}
} protected $Client;
public function __construct( Client $Client )
{
$this->Client = $Client;
}
public function setName( $name )
{
$this->Client->setName( $name );
}
public function getName( )
{
return $this->Client->getName( );
}
public function __call( $method, $args )
{
if( method_exists( $this->Client, $method ) ) return call_user_func_array( array( $this->Client,$method ),$args );
} if( $this->Client instanceof self )
{
return $this->Client->__call( $method,$args );
}
else
{
throw new Exception( sprintf( 'Method %s not found', $method ) );
}
}
}
} public function acceptComment( $commentID )
{
printf( 'Comment #%s was published!', $commentID );
}
} public function sendUserToHell( $userID )
{
printf( 'User #%s is now burning with the devil...', $userID );
}
}
Exemplo 2
No segundo caso, o mesmo programador JAVA teria facilidade em passar este código para linguagem JAVA, só que também o programador PHP tem que escrever bem mais códigos ao meu ver desnecessários.
<?php
/**
* Client
*/
abstract class Client
{
protected $storage;
public function setName( $name )
{
$this->storage[ 'name' ] = $name;
}
public function getName( )
{
return $this->storage[ 'name' ];
}
} public function setLogin( $login )
{
$this->storage[ 'login' ] = $login;
}
public function getLogin( )
{
return $this->storage[ 'login' ];
}
} protected $Client;
public function __construct( Client $Client )
{
$this->Client = $Client;
}
public function setName( $name )
{
$this->Client->setName( $name );
}
public function getName( )
{
return $this->Client->getName( );
}
} public function setLogin( $login )
{
$this->Client->setLogin( $login );
}
public function getLogin( )
{
return $this->Client->getLogin( );
}
public function acceptComment( $commentID )
{
printf( 'Comment #%s was published!', $commentID );
}
} public function setLogin( $login )
{
$this->Client->setLogin( $login );
}
public function getLogin( )
{
return $this->Client->getLogin( );
}
public function acceptComment( $commentID )
{
$this->Client->acceptComment( $commentID );
}
public function sendUserToHell( $userID )
{
printf( 'User #%s is now burning with the devil...', $userID );
}
}
Ps. Os exemplos são meramente para ilustrar minha dúvida. Usei JAVA como exemplo, mas poderia ser qualquer outra linguagem.
Carregando comentários...