180 likes | 301 Views
Programmeerimine Delphi keskkonnas MTAT.03.214. Jelena Zaitseva jellen@ut.ee. Tips on naming variables. Choose variable names with care. x := x - xx; xxx := fido + SalesTax(fido); x := x + LateFee(x1, x) + xxx; x := x + Interest(x1, x);. Choose variable names with care. x := x - xx;
E N D
Programmeerimine Delphi keskkonnasMTAT.03.214 Jelena Zaitseva jellen@ut.ee
Choose variable names with care x := x - xx; xxx := fido + SalesTax(fido); x := x + LateFee(x1, x) + xxx; x := x + Interest(x1, x);
Choose variable names with care x := x - xx; xxx := fido + SalesTax(fido); x := x + LateFee(x1, x) + xxx; x := x + Interest(x1, x); Balance := Balance - LastPayment; MonthlyTotal := NewPurchases + SalesTax(NewPurchases); Balance := Balance + LateFee(CustomerID, Balance) + MonthlyTotal; Balance := Balance + Interest(CustomerID, Balance);
Choosing a name for variable • Examples of good and bad names of variables: current date: CD, CurrentData, Current, Date, TodaysDate lines per page: LinesPerPage, NumberOfLines, Lines number of people on the Olympic team: NTM, NumberOfPeopleOnTheOlympicTeam, TeamMembersCount
Common opposites in variable names • first/last • min/max • next/previous • old/new • visible/invisible • source/target, source/destination • locked/unlocked • …
Naming status variables • Think of a better name than flag for status variables if (flag) then … if (IsDataReady) then … if (printFlag = 2) then … if (ReportType = rtMonthly) then … type TReportType = (rtDaily, rtMonthly, rtAnnual); TSomeNewClass = class private FReportType: TReportType public property ReportType: TReportType read FReportType; end;
Naming boolean variables • Give boolean variables names that imply true or false. if (status) then … if (DataProcessed) then … if (IsDataProcessed) then …
Naming enumerated types • Using prefix helps to ensure if members of a type belong to the same group. type TSound = (sndClick, sndClack, sndClock); TMonth = (mJanuary, mFebruary, … , mDecember); TBaseColor = (bcRed, bcGreen, bcBlue); TFavoriteColor = (fcRed, fcPink, fcBlue);
Kinds of names to avoid • Avoid misleading names or abbreviations • Avoid names with similar meanings • Avoid names with different meanings but similar names • Avoid numerals in names • Avoid misspelled words in names • Avoid the names of standard types, variables, and routines • Don’t differentiate variable names solely by capitalization • Don’t use names that are totally unrelated to what the variable represent if if=then then then := else else then := if;
Boolean variables • Use boolean variables to document your code • Use boolean variables to simplify complicated tests if ( (ElementIndex < 0) or (MAX_ELEMENT_INDEX < elementIndex) or (ElementIndex == LastElementIndex) ) then … Finished := (ElementIndex < 0) or (MAX_ELEMENT_INDEX < elementIndex); Repeated := ElementIndex == LastElementIndex; if (Finished or Repeated) then …
Enumerated types • use enumerated types for readability • if ChosenColor = 1 then • if ChosenColor = bcRed then • use enumerated types for modifiability • use enumerated types as an alternative to boolean variables
Constants • use named constants in data declarations • avoid literals, even ‘safe’ ones for I:=1 to 12 do YearProfit := YearProfit + Profit(I); for Month := mJanuary to mDecember do YearProfit := YearProfit + Profit(Month); • use named constants consistently
use only one statement per line YearProfit:=0; for Month:=mJanuary to mDecember do YearProfit:=YearProfit+Profit(Month);
write self-documenting code for I:=2 to GivenNumber do Numbers[I].MeetsCriteria := True; for PrimeCandidate:=2 to GivenNumber do Numbers[I].IsPrime := True;
document unclear dependencies with comments • comments should say things about code that the code can’t say about itself – at the summary level (focusing on why rather than how)