Since you're asking me to point out stuff you could do better, you can leave that part out of the code entirely.
if (!$2) { set %target $remove($chan,$chr(35)) }
else set %target $iif($2,$2,$mid(#,2-))
is exactly the same thing. $remove($chan,$chr(35)) is the same as $mid(#,2-) assuming there aren't double ##, which isn't allowed on twitch to begin with. And if (!$2) isn't necessary as there's a check for $iif($2)
$iif works this way: $iif ( $2 , $2 , # ) If $2 exists, use $2, else use #
So, tl;dr, all you need is
set %target $iif($2,$2,$mid(#,2-))