The problem with “the ends justify the means” is that the “ends” are often too narrowly interpreted. The “ends” are not simply the intended consequences; they include the entire constellation of consequences. foreseen and unforeseen.
I think specifically of how the US has managed the internment and interrogation of “War on Terror” suspects. The stated “ends” of the policies, incapacitating terrorists and uncovering their networks, are indisputably worthy. But the actual global consequences of those policies, the real ends, go far beyond that to include an undermining of our most cherished priciples of human rights and justice.
So the “ends” must be viewed realistically, not myopically, before deciding whether they justify the means. The problem is that we can rarely envision the full consequences of any action, since cause and effect ripples out through the world in unexpected ways. This is why we’ve come to rely on institutionalized codes of conduct, whether religion-based or legally-based, that proscribe actions that have been largely found to have nasty long term consequences. While we don’t know that doing these will always turn out badly, we’re probably better off assuming that it will.