To do null-able typing or not to do null-able typing , that is a question . #235
Replies: 2 comments 2 replies
-
Hey, a quick answer: the
In general I agree with you here, especially in case the public API. As for the internals I don't see much reason to be strict about it (you'll see several cases in the Detector/Decoder files). However, I'm not exactly a friend of the nullable type in parameter lists as it adds no value other than perhaps specifying it in the middle of mandatory parameters: // no value here
function whatever(?string $foo = null){}
// parameter must be specified even if it is null - annoyance at best
function whatever(?string $foo){}
// the only valid use - nullable before mandatory parameter
function whatever(?string $foo, int $bar){} What I've seen so far is a lot of the second case, perhaps because people are unaware of the fact - I don't know. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the quick reply , the explanation in it and sharing your perspective on the null able typing . It seems best to close this discussion now , so I will do that . Guessing that it can be reopened by others , if so required . |
Beta Was this translation helpful? Give feedback.
-
👋 Hallo,
In 00800ef some parameters that defaulted to
null
( as added in 700af4b ) are changed to have their default value in the list of parameters ( instead of in the method body ) .After seeing that , I found myself wondering why that happened , but the 🚿 commit message did not give me much insight into the reasoning behind the change . Perhaps someone here is willing to write something about the reasoning .
As for myself , somewhere while programming , I have come to use the practice of making optional arguments to have a default value of
null
and setting the type to be null able , while having the default value for the argument in the method body .A reason for me doing so , is that it seems to more naturally prevent the information (of the default value) from being copied over into the parameters list of overriding methods ( in a inheriting class ) .
null-able params (so args too)
extending method
To "demonstrate" the practice , the default value is changed to have some more information than just the empty type .
vs.
diffs
Diff from the initial
src/Common/BitBuffer.php
:Diff from the current
src/Common/BitBuffer.php
:Beta Was this translation helpful? Give feedback.
All reactions