Factor out common arity checking code.