The order in which arguments are given to a function. Why is it necessary this way?

Question:

Recently I thought about the beauty of my code.

A big question arose when it seemed to me that the arguments in a function should go in a different order than now. Take, for example, a function (in C ++, but the language does not play a special role here, the question is different):

int FindСitу (string *СitiеsList, string сity_nаmе, int сitiеs_cоunt);

The function returns the id city ​​(from the СitiеsList array СitiеsList length сitiеs_cоunt ), the name of which is сity_nаmе .

What is the best order to write the arguments to this function? What would you do and why? Is there a standard for the correct placement of function arguments?

Answer:

Of course, there is no single standard, but I would say that in 9 cases out of 10 the parameters СitiеsList and сitiеs_cоunt would go one сitiеs_cоunt .
And in 99 out of 100 cases the names would be written in the СitiеsList : СitiеsList and СitiеsСоunt , or citiеsList and citiеsСоunt , or citiеs_list and citiеs_cоunt .

The programming language is, of course, important, and there are often official guidelines on the style of code for a particular language, but often the conditions are dictated by specific frameworks, libraries, platforms, their observance is even more important.

You also need to understand that for an English-speaking programmer, a program is written almost in their native language, and there is a natural desire to replace

удалить(базаДанных, запись)

to a more meaningful

удалить(запись, базаДанных)

But often monobase api more important

количество(базаДанных)
вставить(базаДанных, запись1, запись2...)

In functional programming languages, they try to bring the program even closer to a natural language, for example, in Scala

1 to 10 by 2 map (_ * 2) // Vector(2, 6, 10, 14, 18)

this is just an alternate notation for the chain of use

1.to(10).by(2).map(x => x * 2)

and in Haskell currying, partial application, and infix function applications allow you to do this kind of thing.

Prelude> on f g x y = g x `f` g y
Prelude> add x y = x + y
Prelude> mul x y = x * y
Prelude> addOnMul10 = add `on` mul 10
Prelude> addOnMul10 3 7
100

This also places restrictions on the order of the arguments.

In general, use an order that you think is natural, but with an eye to how others do – excessive individualism is rather harmful.

Scroll to Top