Consider some function: $\DeclareMathOperator{argmin}{argmin}$
$$f(w) = \underset{w}{\argmin}\:(z-w)^2 + \lambda\gamma |w|\:\:\:\:\:\:\:\:\:where\:\lambda,\gamma\in\mathbb{R}^+$$
I'm quite confused when dealing with functions containing absolute value terms for the purposes of differentiation (w.r.t $w$). In some cases, I've seen people differentiate the function including the absolute value terms first; resulting in:
$${f}'(w) = 2(w-z) + \lambda\gamma\frac{w}{|w|}$$
and then considering the 2 cases where:
$$w\in\mathbb{R}^+$$ $$w\in\mathbb{R}^-$$
Meanwhile, I've also seen the case where people would consider the cases first (notice the inclusion of zero):
$$w\in\mathbb{R}^+_0$$ $$w\in\mathbb{R}^-_0$$
resulting in:
$${f}(w) = (z-w)^2 + \lambda\gamma w$$
$${f}(w) = (z-w)^2 - \lambda\gamma w$$
respectively, and then differentiating results in:
$${f}'(w) = 2(w-z) + \lambda\gamma$$
$${f}'(w) = 2(w-z) - \lambda\gamma$$
respectively.
For me there's a subtle difference in that the second way, $w=0$ can be a solution, whereas in the first way it would've been undefined. Sorry if I'm understanding this incorrectly but could someone shed some light on this issue please?
It looks as though both methods lead to $f'(0)$ being undefined. The first case makes this a little more explicit, since you it has a division by zero, but the second case also can't define a derivative at that point. The derivatives of each of the pieces of your function give inconsistent results for $w=0$: $$\lim_{a\to0^-}f'(a)=2z-\lambda\gamma$$ $$\lim_{a\to0^+}f'(a)=2z+\lambda\gamma$$
If the left/right derivatives (or semi-derivatives) at a point aren't equal, then the function is not differentiable at that point.
This same issue arises if you just look at $g(x)=|x|$: no matter how you calculate it, splitting it into pieces before or after taking the derivative, its clear that the derivative is undefined at $x=0$, as the left and right derivatives aren't equal. Geometrically, you can see this from the sharp point at $x=0$ in a plot of $|x|$.