null object pattern unable to solve the problem of multiple return types











up vote
0
down vote

favorite












Sometimes users prefer not to disclose marital status, which is fine in my domain. So I have data where user is either married or not and I have data where I don't know the status. My goal is to make sure the function for marital status check as below returns only single type of data. Although I am using php but I am sure this applies to any language out there.



This is the interface



interface MaritalStatusInterface
{
public function status(): ?bool;
}


This means status can return true or false and in some cases null.
This was my implementation.



class MaritalStatus implements MaritalStatusInterface
{
const SINGLE = 0;
const MARRIED = 1;

private $status;

public function __construct(int $status)
{
if($status !== self::SINGLE && $status !== self::MARRIED)
{
throw new InvalidArgumentException("Given marital status '$status' is invalid. Marital status should be either single as " . self::SINGLE . " or married as " . self::MARRIED, 7263);
}
$this->status = $status;
}

public function status(): ?bool
{
return ($this->status)?true:false;
}
}


Now this is the null object when I am unable to get marital status data.



class NullMaritalStatus implements MaritalStatusInterface
{

private $status;

public function __construct()
{
$this->status = null;
}

public function status(): ?bool
{
return $this->status;
}
}


My question is I am unable to split the return type of Null object and real object status function. The primary benefit as per my understanding for using null object pattern is functions are more predictable as if it is bool return type then it will always return bool.



How can I refactor my code so that the function



public function status(): ?bool


can be written as



# for MaritalStatus class

public function status(): bool


and



# for NullMaritalStatus class

public function status(): null


Is it even possible? Surely that will not conform to MaritalStatusInterface. I am not sure if using multiple interfaces can solve it. Or am I looking it in a totally wrong manner and there is nothing wrong in having nullable return types?










share|improve this question
























  • there is nothing wrong in having nullable return types? There is nothing wrong with that, if you are handling null properly on the caller-side. For me the NullObject Pattern is an overengineering to solve problems that aren't really existing. In most the cases, the caller needs to deal with "null" values - so it doesn't matter, if you return null or a NullObject.
    – dognose
    16 hours ago












  • thanks for your insight, using NullObject seems overkill but it removes a lot of if statements in my current domain, so whether I like it or not but I need to get along with it unless I know something better which at the moment I don't. That's why I am asking
    – Waku-2
    16 hours ago

















up vote
0
down vote

favorite












Sometimes users prefer not to disclose marital status, which is fine in my domain. So I have data where user is either married or not and I have data where I don't know the status. My goal is to make sure the function for marital status check as below returns only single type of data. Although I am using php but I am sure this applies to any language out there.



This is the interface



interface MaritalStatusInterface
{
public function status(): ?bool;
}


This means status can return true or false and in some cases null.
This was my implementation.



class MaritalStatus implements MaritalStatusInterface
{
const SINGLE = 0;
const MARRIED = 1;

private $status;

public function __construct(int $status)
{
if($status !== self::SINGLE && $status !== self::MARRIED)
{
throw new InvalidArgumentException("Given marital status '$status' is invalid. Marital status should be either single as " . self::SINGLE . " or married as " . self::MARRIED, 7263);
}
$this->status = $status;
}

public function status(): ?bool
{
return ($this->status)?true:false;
}
}


Now this is the null object when I am unable to get marital status data.



class NullMaritalStatus implements MaritalStatusInterface
{

private $status;

public function __construct()
{
$this->status = null;
}

public function status(): ?bool
{
return $this->status;
}
}


My question is I am unable to split the return type of Null object and real object status function. The primary benefit as per my understanding for using null object pattern is functions are more predictable as if it is bool return type then it will always return bool.



How can I refactor my code so that the function



public function status(): ?bool


can be written as



# for MaritalStatus class

public function status(): bool


and



# for NullMaritalStatus class

public function status(): null


Is it even possible? Surely that will not conform to MaritalStatusInterface. I am not sure if using multiple interfaces can solve it. Or am I looking it in a totally wrong manner and there is nothing wrong in having nullable return types?










share|improve this question
























  • there is nothing wrong in having nullable return types? There is nothing wrong with that, if you are handling null properly on the caller-side. For me the NullObject Pattern is an overengineering to solve problems that aren't really existing. In most the cases, the caller needs to deal with "null" values - so it doesn't matter, if you return null or a NullObject.
    – dognose
    16 hours ago












  • thanks for your insight, using NullObject seems overkill but it removes a lot of if statements in my current domain, so whether I like it or not but I need to get along with it unless I know something better which at the moment I don't. That's why I am asking
    – Waku-2
    16 hours ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Sometimes users prefer not to disclose marital status, which is fine in my domain. So I have data where user is either married or not and I have data where I don't know the status. My goal is to make sure the function for marital status check as below returns only single type of data. Although I am using php but I am sure this applies to any language out there.



This is the interface



interface MaritalStatusInterface
{
public function status(): ?bool;
}


This means status can return true or false and in some cases null.
This was my implementation.



class MaritalStatus implements MaritalStatusInterface
{
const SINGLE = 0;
const MARRIED = 1;

private $status;

public function __construct(int $status)
{
if($status !== self::SINGLE && $status !== self::MARRIED)
{
throw new InvalidArgumentException("Given marital status '$status' is invalid. Marital status should be either single as " . self::SINGLE . " or married as " . self::MARRIED, 7263);
}
$this->status = $status;
}

public function status(): ?bool
{
return ($this->status)?true:false;
}
}


Now this is the null object when I am unable to get marital status data.



class NullMaritalStatus implements MaritalStatusInterface
{

private $status;

public function __construct()
{
$this->status = null;
}

public function status(): ?bool
{
return $this->status;
}
}


My question is I am unable to split the return type of Null object and real object status function. The primary benefit as per my understanding for using null object pattern is functions are more predictable as if it is bool return type then it will always return bool.



How can I refactor my code so that the function



public function status(): ?bool


can be written as



# for MaritalStatus class

public function status(): bool


and



# for NullMaritalStatus class

public function status(): null


Is it even possible? Surely that will not conform to MaritalStatusInterface. I am not sure if using multiple interfaces can solve it. Or am I looking it in a totally wrong manner and there is nothing wrong in having nullable return types?










share|improve this question















Sometimes users prefer not to disclose marital status, which is fine in my domain. So I have data where user is either married or not and I have data where I don't know the status. My goal is to make sure the function for marital status check as below returns only single type of data. Although I am using php but I am sure this applies to any language out there.



This is the interface



interface MaritalStatusInterface
{
public function status(): ?bool;
}


This means status can return true or false and in some cases null.
This was my implementation.



class MaritalStatus implements MaritalStatusInterface
{
const SINGLE = 0;
const MARRIED = 1;

private $status;

public function __construct(int $status)
{
if($status !== self::SINGLE && $status !== self::MARRIED)
{
throw new InvalidArgumentException("Given marital status '$status' is invalid. Marital status should be either single as " . self::SINGLE . " or married as " . self::MARRIED, 7263);
}
$this->status = $status;
}

public function status(): ?bool
{
return ($this->status)?true:false;
}
}


Now this is the null object when I am unable to get marital status data.



class NullMaritalStatus implements MaritalStatusInterface
{

private $status;

public function __construct()
{
$this->status = null;
}

public function status(): ?bool
{
return $this->status;
}
}


My question is I am unable to split the return type of Null object and real object status function. The primary benefit as per my understanding for using null object pattern is functions are more predictable as if it is bool return type then it will always return bool.



How can I refactor my code so that the function



public function status(): ?bool


can be written as



# for MaritalStatus class

public function status(): bool


and



# for NullMaritalStatus class

public function status(): null


Is it even possible? Surely that will not conform to MaritalStatusInterface. I am not sure if using multiple interfaces can solve it. Or am I looking it in a totally wrong manner and there is nothing wrong in having nullable return types?







php design-patterns null-object-pattern






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago

























asked 16 hours ago









Waku-2

187113




187113












  • there is nothing wrong in having nullable return types? There is nothing wrong with that, if you are handling null properly on the caller-side. For me the NullObject Pattern is an overengineering to solve problems that aren't really existing. In most the cases, the caller needs to deal with "null" values - so it doesn't matter, if you return null or a NullObject.
    – dognose
    16 hours ago












  • thanks for your insight, using NullObject seems overkill but it removes a lot of if statements in my current domain, so whether I like it or not but I need to get along with it unless I know something better which at the moment I don't. That's why I am asking
    – Waku-2
    16 hours ago




















  • there is nothing wrong in having nullable return types? There is nothing wrong with that, if you are handling null properly on the caller-side. For me the NullObject Pattern is an overengineering to solve problems that aren't really existing. In most the cases, the caller needs to deal with "null" values - so it doesn't matter, if you return null or a NullObject.
    – dognose
    16 hours ago












  • thanks for your insight, using NullObject seems overkill but it removes a lot of if statements in my current domain, so whether I like it or not but I need to get along with it unless I know something better which at the moment I don't. That's why I am asking
    – Waku-2
    16 hours ago


















there is nothing wrong in having nullable return types? There is nothing wrong with that, if you are handling null properly on the caller-side. For me the NullObject Pattern is an overengineering to solve problems that aren't really existing. In most the cases, the caller needs to deal with "null" values - so it doesn't matter, if you return null or a NullObject.
– dognose
16 hours ago






there is nothing wrong in having nullable return types? There is nothing wrong with that, if you are handling null properly on the caller-side. For me the NullObject Pattern is an overengineering to solve problems that aren't really existing. In most the cases, the caller needs to deal with "null" values - so it doesn't matter, if you return null or a NullObject.
– dognose
16 hours ago














thanks for your insight, using NullObject seems overkill but it removes a lot of if statements in my current domain, so whether I like it or not but I need to get along with it unless I know something better which at the moment I don't. That's why I am asking
– Waku-2
16 hours ago






thanks for your insight, using NullObject seems overkill but it removes a lot of if statements in my current domain, so whether I like it or not but I need to get along with it unless I know something better which at the moment I don't. That's why I am asking
– Waku-2
16 hours ago



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371633%2fnull-object-pattern-unable-to-solve-the-problem-of-multiple-return-types%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371633%2fnull-object-pattern-unable-to-solve-the-problem-of-multiple-return-types%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules