Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a common and sensible parameter order for functions?

Assuming they are all mandatory:

function search (haystack, needle)
function search (needle, haystack)

function placeObject (object, column, row)
function placeObject (column, row, object)

function newObject (parent, width, height, isVisible)
function newObject (isVisible, width, height, parent)
function newObject (width, height, isVisible, parent)

I think it is often a matter of personal choice which should be consistent throughout the project. But I wonder whether there is a deeper logic which would dictate the order for every case.

like image 927
problemofficer Avatar asked Nov 14 '22 04:11

problemofficer


1 Answers

Try to pronounce the intended invocation.

function search (needle, haystack)

Search for needle in haystack.

function placeObject (object, column, row)

Place object at (column, row).

newObject is tough: try to keep it consistent with your framework, if any, put the common parameters first. I'd put isVisible last just because it's boolean, and it can be hard to infer from a boolean literal what it does. With multiple booleans, I prefer to combine them into a flags object of integer type, built with bitmasks (or a key-value dictionary, or a string, depending on the language) to obtain readability:

openFile(path, READ | LOCKED | COMPRESSED | ...)
like image 164
Fred Foo Avatar answered Dec 15 '22 00:12

Fred Foo