Given independent invention is apparently not a defense against infringement, that makes a lot of sense. I can’t even imagine trying to screen the codebase for that.
"Infringement" is not specific enough there. Independent invention is not a defense against patent infringement, but is a defense against copyright infringement.
It's not easy to prove independent invention as a defense, because you have to have set it up to do so in the first place, not after the fact.
No, but a patent can be invalidated if you can show that the idea is obvious to practitioners of the trade, i.e. given the same problem most software engineers would arrive at the same solution.
Same with the copyright. Purely functional code is not creative expression and cannot be copyrighted.