Because the practice of "constant parameter" exists only in C

Asked

Viewed 1,271 times

10

In C, there is the practice of applying the "const" modifier to function parameters when the function does not aim to change the parameter. As in this code:

char process_string(const char *str);

Because in other languages there is no such practice? Because it is not common to see something like:

public String processString(final String str);
  • 1

    In fact, final in Java parameters is generally a good practice to avoid improper reuse of parameters as variables. Almost nobody uses it because most of the "javeiros" do not study beyond the basic and the rest have laziness.

  • That’s interesting. Could you tell me where I can get these good practices?

1 answer

11

First, the codes you posted are not equivalent: in the first case, the pointer str may be reallocated at will, but the contents of the pointed string no (more details in that question in the SOEN). in the second, the reference str cannot be modified, but the content of the object in principle could be - if String was no longer immutable (I am thinking of Java, but if you were referring to another language please specify).

This example in ideone shows this clearly:

char *process_string(const char *str) {
    str = outra_string; // Pode mudar o ponteiro à vontade
    //str[0] = '1';     // Não pode mexer no conteúdo
    return str;
}

char *process_string_2(char * const str) {
    //str = outra_string; // Não pode mudar o ponteiro
    str[0] = '2';         // Pode mexer no conteúdo à vontade
    return str;
}

Why is this not common in other languages? Unfortunately I can’t say... It would be extremely useful for a compiler to guarantee the contract that "this method has no side effects [in this parameter at least]" but I have no idea of the difficulty of implementing (in particular in object-oriented languages, which do not support pointer arithmetic and make extensive use of references).

And speaking of references, the main reason you don’t see many parameters final is that it is common for references to objects to be passed by value. That is, the object is not copied, but the reference to it is. So, what’s wrong with the function of reassigning its own parameters? Only she really sees them... And for that very reason it’s hard to see a C function with meters just like the process_string_2 of the above example.

The only case I know that a parameter final may be necessary when it is used in a closure and language does not support "spaghetti pile":

public void prepararBotao(final String mensagem) {
    final JTextField caixaTexto = this.caixaTexto;
    botao.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            caixaTexto.setText(mensagem); // Só pode usar "mensagem" aqui porque é final
        }
    });
}

(I don’t know the exact reason why this is needed in Java - other languages allow closures to access non-terminal attributes smoothly)

  • Reminders only. The idea of "cannot change" also exists in the variables Static... Perhaps it would be appropriate to broaden the scope of the discussion... Regarding the use in other languages, perhaps interesting to remember equivalences and differences for the directive #defines (that in languages like PHP is a command).

  • I think I understand: The practice exists in C because in this language the referencing of variables is more critical, and to ensure that there are no side effects, const is necessary. But there are things that have not yet been clarified: How does the passage by value use references? Passing by reference is not the one where the address is if the function changes the value of the variable, it will change the value of the variable that was passed to it. In Java, as far as I know, it’s all about value.

  • 1

    @Sid There is a difference between "pointer" and "reference", but I don’t know exactly what it is... Anyway, what you said is correct, everything is passed by value. But as a object passed by value would be copied (in C++ for example, this can happen) a pointer to an obejto passed by value does not cause the object itself to be copied.

  • 1

    @mustache Done! :)

  • Java is said to pass "the reference value". You cannot take the address or explicitly, so this concept (address) does not exist. In C++ you can take the address of a reference explicitly if you want. In Java, when you pass a variable to a function, or assign the value of one variable to another, it is as if you are copying the reference (the ""address") of the object, not the object it "pointed to". The assignment operator "=" only assigns new reference to the variable, it does not copy the referenced object. To change the object, only through the methods and properties.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.