ES.25 -- Is it truly beneficial to always employ const for objects?
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Documentazione
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- cpp
- Ambito
- documentation
Direzione di ricerca
Inizia leggendo la discussione in questa issue e l’issue 19 di CppCoreGuidelines a cui si fa riferimento. Nel report non sono indicati file, test o entry point; il lavoro richiederebbe innanzitutto di decidere se modificare le indicazioni sulle variabili locali const e poi di documentare il risultato concordato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
"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:
constcreates a lot of "visual noise" in the code. For example,const int* const pointer = &variable;. This is harder to read thanint * pointer = &variable;. But maybe it makes sense to sacrifice readability for something more significant?- "
constallows 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? constwon'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.
- Lingua principale
- CSS
- Stelle
- 45.3k
- Fork
- 5.6k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di isocpp/CppCoreGuidelines
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 75/100
isocpp/CppCoreGuidelines#2338 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
isocpp/CppCoreGuidelines#2205 · 8 commenti ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 64/100
isocpp/CppCoreGuidelines#1649 · 3 commenti ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 42/100
isocpp/CppCoreGuidelines#2336 · 4 commenti ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
isocpp/CppCoreGuidelines#2335 · 2 commenti ·
Tutte le issue di isocpp/CppCoreGuidelines
Issue simili
-
sync-en
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
community-request
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
NVIDIA-NeMo/Curator#2464 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
good first issue hacktoberfest
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
elnachto/laya-triage#5 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
[integration]: InferrailApertaexternal
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
langchain-ai/docs#6328 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno