7

When defining a function, is it always recommended to use the scope s: to make it local to the script and avoid overwriting a function with the same name in the global namespace ?
And is it always recommended to use the argument abort in case the function detects an error ?

function! s:SomeFunction() abort
    echo "hello world!"
endfunction

The scope s: seems a good thing, but it makes the code a little more verbose, because each time I want to call SomeFunction() from a mapping I have to prefix it with <SID>:

nnoremap {lhs} :<c-u>call <SID>SomeFunction()<cr>

Or even to store its output inside a variable:

let myvar = <SID>SomeFunction()

Are there some specific cases in which you don't want to use s: and / or abort ?
If so, for what reasons ?

1 Answer 1

6

The s: simply means you are not polluting the global namespace. Usually you would do this if it is a function that doesn't need to be called by the user. As for the abort flag, here is what the docs :help :func-abort say:

When the [abort] argument is added, the function will abort as soon as an error is detected.

If your function doesn't do any set up and won't break anything if it can't tear itself down when an error is encountered, then it probably would be okay to use it. (that is, if you want to silently abort if an error occurs)

1
  • I would think the best practice would be to always use abort, and use a try/finally for any required teardown. Is that not the case?
    – ivan
    Commented Apr 12, 2018 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.