Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

ES.25 -- Is it truly beneficial to always employ const for objects?

Abierto
#2,155 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Documentación
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
cpp

Línea de trabajo

Comienza leyendo la discusión de este issue y el issue 19 de CppCoreGuidelines al que se hace referencia. En el informe no se nombra ningún archivo, prueba ni punto de entrada; el trabajo requeriría primero decidir si debe cambiar la guía sobre las variables locales const y, después, documentar el resultado acordado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

"Yes, certainly!". This was my initial reaction upon encountering the recommendation. However, as I delved deeper into C++ development, engaged in code reviews, and studied existing programs in the language, I started to question whether this recommendation is universally beneficial. Seeking insights from seasoned C++ developers, I discovered that the use of const for local variables is subjective, driven by personal preferences, individual tastes, and even ideological beliefs, rather than being an unequivocally superior practice (in their opinion). Here are a few tips based on these discussions:

  1. const creates a lot of "visual noise" in the code. For example, const int* const pointer = &variable;. This is harder to read than int * pointer = &variable;. But maybe it makes sense to sacrifice readability for something more significant?
  2. "const allows you to monitor in the code which variables are changing and which are not." This is justified in large chunks of code (which it is recommended to avoid), where it is problematic to keep track of everything. But is it really worth inflating the amount of code and sacrificing readability in small methods, where it is easy to keep track of the local variables constancy?
  3. const won't help optimize the code. Modern compilers themselves are able to do static analysis and optimize constants.

Being a lead programmer, in my team I allowed to write const for local variables at will and no longer pay attention to it in code reviews. The colleagues with whom I discussed this issue have the same opinion. Writing const for local variables is a programmer's personal matter.

What's your opinion?

IMHO: In a language, it is better to have a mutable modifier rather than const. Then this problem did not exist at all. But c++ cannot afford such an approach.

P.S. Something similar was discussed in https://github.com/isocpp/CppCoreGuidelines/issues/19.

Lenguaje dominante
CSS
Estrellas
45.3k
Forks
5.6k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de isocpp/CppCoreGuidelines

Todos los issues de isocpp/CppCoreGuidelines

Issues similares

Más issues de Documentation

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.