For years I had been driven insane by Mac OS switching the current desktop space for no reason. Thanks to this post my problem is finally solved.
Here is the instant fix that worked for me:
defaults write com.apple.Dock workspaces-auto-swoosh -bool NO
defaults write -g AppleSpacesSwitchOnActivate -bool false
killall Dock