Skip to main content
edited tags
Link
yannis
  • 39.7k
  • 40
  • 185
  • 218
Tweeted twitter.com/#!/StackProgrammer/status/278892531712937985

I am currently writing code and want to make sure all the params that getsget passed to a function/method are valid. Since I am writing in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExistinscriptionExists(sectionId$sectionId, userId$userId) // PHP 

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java 

So I have to rely on exception throwingexceptions if I want to make sure that my params are both integerintegers.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

public static function factory ($value, $valueType, $exceptionType = 'InvalidArgumentException'); 

Which would then call the right sub process to validate based on the type.

Am I on the right way, or am I going completely off the road and overthinking my problem?

I am currently writing code and want to make sure all the params that gets passed to a function/method are valid. Since I am writing in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExist(sectionId, userId) // PHP 

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java 

So I have to rely on exception throwing if I want to make sure that my params are both integer.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

public static function factory ($value, $valueType, $exceptionType = 'InvalidArgumentException'); 

Which would then call the right sub process to validate based on the type

Am I on the right way, or am I going completely off the road and overthinking my problem?

I am currently writing code and want to make sure all the params that get passed to a function/method are valid. Since I am writing in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExists($sectionId, $userId) // PHP 

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java 

So I have to rely on exceptions if I want to make sure that my params are both integers.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

public static function factory ($value, $valueType, $exceptionType = 'InvalidArgumentException'); 

Which would then call the right sub process to validate based on the type.

Am I on the right way, or am I going completely off the road and overthinking my problem?

added 1 characters in body
Source Link
gnat
  • 20.5k
  • 29
  • 117
  • 310

I am currently writing code and want to make sure all the params that gets passed to a function/method are valid. Since I am writtingwriting in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExist(sectionId, userId) // PHP 

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java 

So I have to rely on exception throwing if iI want to make sursure that my params are both integer.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

public static function factory ($value, $valueType, $exceptionType = 'InvalidArgumentException'); 

Which would then call the right sub process to validate based on the type

Am I on the right way, or am I going completlycompletely off the road and overthinking my problem?

I am currently writing code and want to make sure all the params that gets passed to a function/method are valid. Since I am writting in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExist(sectionId, userId) // PHP 

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java 

So I have to rely on exception throwing if i want to make sur that my params are both integer.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

public static function factory ($value, $valueType, $exceptionType = 'InvalidArgumentException'); 

Which would then call the right sub process to validate based on the type

Am I on the right way, or am I going completly off the road and overthinking my problem?

I am currently writing code and want to make sure all the params that gets passed to a function/method are valid. Since I am writing in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExist(sectionId, userId) // PHP 

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java 

So I have to rely on exception throwing if I want to make sure that my params are both integer.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

public static function factory ($value, $valueType, $exceptionType = 'InvalidArgumentException'); 

Which would then call the right sub process to validate based on the type

Am I on the right way, or am I going completely off the road and overthinking my problem?

Source Link
JF Dion
  • 530
  • 3
  • 12
Loading