I'm sure that question has gone down through the ages!
At the risk of sounding naive, I can understand cause and effect as a law of action ...
Say giving alms to the poor – to offer a classic model.
Man A gives alms to be poor because he wants to be seen to be generous, a benefactor, he wants people to think well of him.
Man B gives alms to the poor because of a desire to alleviate their suffering.
So man A acts out of pride, man B acts out of compassion.
For Man C, the receiver of the alms, the motive is immaterial, the effect is the same, his situation is improved.
In terms of a mechanical karmic consequence, the outcome is the same.
In a moral sphere, the purposes are quite different, one is man serving his own aggrandisement, the other is man serving the needs of his neighbour. So in moral terms, Man A is a lesser good, if indeed a good at all.
So I'm wondering about the karmic consequence of the reasons behind the actions?
+++
As I said above ... I am sure the Dharma has got this covered ...