10 Minute Vim!

Using Named Parameters

· Read in about 2 min · (293 Words)

Up until recently, most of my programming was in php, which does not support named parameters (this doesn't count, and it will get you lynched) so while switching to VB.NET and python, I have come to really appreciate this handy feature.

Named parameters is one of those handy little things that can make your life a little easier. Say, in python, you have a function that takes a boolean value. Normally, I would create a variable just to hold that boolean value, then pass it in, to try to be easier for the next guy to understand.

``` python def do_stuff(): window(true) ```

Who knows what window's parameter is? What the heck is true anyway, the window? PyCharm knows (and if you are using python without it, I implore you to treat yourself to the demo) otherwise you have to look up the function like a barbarian to see what is going on. Sure, you can make it more clear with:

def do_stuff():
    collapse = true
    window(collapse)

Better then nothing, but still lame.

def do_stuff():
    window(collapse=true)

Boom! Way easier to read, no extra variables hanging around making trouble. Many times I have made function parameters, called them one thing, then made a variable with a slightly different name to pass in for legibility, but no more of that crap! Now, when I have the inclination to make a variable just to clarify something, I just name the parameter and pass in the value right there, like a civilized gentleman should.

Additionally, VB.NET has this too, with the slightly goofier syntax of the ":="

Authenticate(pAllowBackupPassword:=True)

My cohort Daniel Miladinov pointed out that this is calling the assignment operator directly, since calling it with just the equals would perform an equality operator. Smart guy, Dan.

STEVE SHOGREN

software developer, manager, author, speaker

BOOKS:

POSTS FOR:

CATEGORIES