On Thu, Sep 12, 2019 at 10:11 AM Rowan Tommins <rowan.coll...@gmail.com>
wrote:

> On Thu, 12 Sep 2019 at 14:55, Claude Pache <claude.pa...@gmail.com> wrote:
>
> > Le 12 sept. 2019 à 15:33, Marco Pivetta <ocram...@gmail.com> a écrit :
> >
> > $foo[$key1][$key2] = ($foo[$key1][$key2] ?? 0) + 1;
> >
> > Marco Pivetta
> >
> >
> > That violates blatantly DRY (twice the exact same lengthy expression
> > `$foo[$key1][$key2]`), so it is not a satisfactory solution.
> >
>
>
> Agreed; it's certainly neater than all the isset() checks, but it's
> definitely a bit ugly.
>
> To clarify my point, the reason why people write this:
>
> $foo[$key1][$key2]++;
>
> Is not because they're lazy, it's because *it expresses their intent*.
>
> The ?key syntax was one suggestion for how to express the intent safely in
> that particular scenario. Another way might be that the array is
> initialised a different way; completely off the top of my head, something
> like this:
>
> $foo = new Dictionary<string, Dictionary<string, int=0>>;
>
> That could express the intent of "this variable is going to be used as an
> accumulator with these dimensions".
>
> The "if isset" lines, in my opinion, don't express any intent, and they
> don't protect against any real errors; they're just noise to work around a
> short-coming in the language.
>
> But, if you're dealing with a counter, then, the intent is that you are
going to start counting at 0 and increase it. In that case, if the variable
hasn't be initialized, or, the array key doesn't exist, it makes sense to
assume it's 0.

If you need to do something else besides assuming it's 0 and counting from
there, then put in the extra code to check for that.
I can do this already:
if(!isset($i)){
  return false;
}
$i++;

So, why should I start having to do
if(!isset($i)){
  $i = 0;
}
$i++;

when
$i++;

works just fine.


> Regards,
> --
> Rowan Tommins
> [IMSoP]
>


-- 
Chase Peeler
chasepee...@gmail.com

Reply via email to