Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva

Similar presentations


Presentation on theme: "Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva"— Presentation transcript:

1 Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.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: 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 NumberOfPeopleOnTheOlympicTeam, TeamMembersCount

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

7 Naming status variables Think of a better name than flag for 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 type TReportType = (rtDaily, rtMonthly, rtAnnual); TReportType = (rtDaily, rtMonthly, rtAnnual); TSomeNewClass = class TSomeNewClass = class private private FReportType: TReportType FReportType: TReportType public public property ReportType: TReportType read FReportType; property ReportType: TReportType read FReportType; end; end;

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

9 Naming enumerated types Using prefix helps to ensure if members of a type belong to the same group. Using prefix helps to ensure if members of a type belong to the same group.type TSound = (sndClick, sndClack, sndClock); TSound = (sndClick, sndClack, sndClock); TMonth = (mJanuary, mFebruary, …, mDecember); TMonth = (mJanuary, mFebruary, …, mDecember); TBaseColor = (bcRed, bcGreen, bcBlue); TBaseColor = (bcRed, bcGreen, bcBlue); TFavoriteColor = (fcRed, fcPink, fcBlue); TFavoriteColor = (fcRed, fcPink, fcBlue);

10 Kinds of names to avoid Avoid misleading names or abbreviations Avoid misleading names or abbreviations Avoid names with similar meanings Avoid names with similar meanings Avoid names with different meanings but similar names Avoid names with different meanings but similar names Avoid numerals in names Avoid numerals in names Avoid misspelled words in names Avoid misspelled words in names Avoid the names of standard types, Avoid the names of standard types, variables, and routines Don’t differentiate variable names solely by capitalization Don’t differentiate variable names solely by capitalization Don’t use names that are totally unrelated to what the variable represent 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 document your code Use boolean variables to simplify complicated tests Use boolean variables to simplify complicated tests if ( (ElementIndex < 0) or (MAX_ELEMENT_INDEX < elementIndex) or (MAX_ELEMENT_INDEX < elementIndex) or (ElementIndex == LastElementIndex) ) then … (ElementIndex == LastElementIndex) ) then … Finished := (ElementIndex < 0) or (MAX_ELEMENT_INDEX < elementIndex); (MAX_ELEMENT_INDEX < elementIndex); Repeated := ElementIndex == LastElementIndex; if (Finished or Repeated) then …

13 Enumerated types use enumerated types for readability use enumerated types for readability –if ChosenColor = 1 then –if ChosenColor = bcRed then use enumerated types for modifiability use enumerated types for modifiability use enumerated types as an alternative to boolean variables use enumerated types as an alternative to boolean variables

14 Constants use named constants in data declarations use named constants in data declarations avoid literals, even ‘safe’ ones avoid literals, even ‘safe’ ones for I:=1 to 12 do for I:=1 to 12 do YearProfit := YearProfit + Profit(I); YearProfit := YearProfit + Profit(I); for Month := mJanuary to mDecember do for Month := mJanuary to mDecember do YearProfit := YearProfit + Profit(Month); use named constants consistently use named constants consistently

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

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

17 write self-documenting code 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 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)


Download ppt "Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva"

Similar presentations


Ads by Google