code generate () algorithm