http://www.basef.com.br/index.php?title=Use_Searchable_Names&feed=atom&action=historyUse Searchable Names - Histórico de revisão2024-03-29T13:07:58ZHistórico de revisões para esta página neste wikiMediaWiki 1.26alphahttp://www.basef.com.br/index.php?title=Use_Searchable_Names&diff=2322&oldid=prevAdmin: Criou página com 'Single-letter names and numeric constants have a particular problem in that they are not easy to locate across a body of text. One might easily grep for MAX_CLASSES_PER_STUDE...'2020-01-27T13:52:11Z<p>Criou página com 'Single-letter names and numeric constants have a particular problem in that they are not easy to locate across a body of text. One might easily grep for MAX_CLASSES_PER_STUDE...'</p>
<p><b>Página nova</b></p><div>Single-letter names and numeric constants have a particular problem in that they are not<br />
easy to locate across a body of text.<br />
<br />
One might easily grep for MAX_CLASSES_PER_STUDENT, but the number 7 could be more<br />
troublesome. Searches may turn up the digit as part of file names, other constant definitions, and in various expressions where the value is used with different intent. It is even<br />
worse when a constant is a long number and someone might have transposed digits,<br />
thereby creating a bug while simultaneously evading the programmer’s search.<br />
<br />
Likewise, the name e is a poor choice for any variable for which a programmer might<br />
need to search. It is the most common letter in the English language and likely to show up<br />
in every passage of text in every program. In this regard, longer names trump shorter<br />
names, and any searchable name trumps a constant in code.<br />
<br />
My personal preference is that single-letter names can ONLY be used as local variables inside short methods. The length of a name should correspond to the size of its scope If a variable or constant might be seen or used in multiple places in a body of code,<br />
it is imperative to give it a search-friendly name. Once again compare:<br />
<br />
<source lang="Java"><br />
for (int j=0; j<34; j++) {<br />
s += (t[j]*4)/5;<br />
}<br />
</source><br />
<br />
To:<br />
<br />
<source lang="Java"><br />
int realDaysPerIdealDay = 4;<br />
const int WORK_DAYS_PER_WEEK = 5;<br />
int sum = 0;<br />
for (int j=0; j < NUMBER_OF_TASKS; j++) {<br />
int realTaskDays = taskEstimate[j] * realDaysPerIdealDay;<br />
int realTaskWeeks = (realdays / WORK_DAYS_PER_WEEK);<br />
sum += realTaskWeeks;<br />
}<br />
</source><br />
<br />
<br />
[[Category: Clean Code]]</div>Admin