1 / 18

Programmeerimine Delphi keskkonnas MTAT.03.214

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;

maddox
Télécharger la présentation

Programmeerimine Delphi keskkonnas MTAT.03.214

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programmeerimine Delphi keskkonnasMTAT.03.214 Jelena Zaitseva jellen@ut.ee

  2. Tips on naming variables

  3. Choose variable names with care x := x - xx; xxx := fido + SalesTax(fido); x := x + LateFee(x1, x) + xxx; x := x + Interest(x1, x);

  4. 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);

  5. 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

  6. Common opposites in variable names • first/last • min/max • next/previous • old/new • visible/invisible • source/target, source/destination • locked/unlocked • …

  7. 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;

  8. Naming boolean variables • Give boolean variables names that imply true or false. if (status) then … if (DataProcessed) then … if (IsDataProcessed) then …

  9. 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);

  10. 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;

  11. Tips on writing code

  12. 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 …

  13. 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

  14. 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

  15. keep variables ‘live’ for as short a time as possible

  16. use only one statement per line YearProfit:=0; for Month:=mJanuary to mDecember do YearProfit:=YearProfit+Profit(Month);

  17. write self-documenting code for I:=2 to GivenNumber do Numbers[I].MeetsCriteria := True; for PrimeCandidate:=2 to GivenNumber do Numbers[I].IsPrime := True;

  18. 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)

More Related