programmeerimine delphi keskkonnas mtat.03.214 programmeerimine delphi keskkonnas mtat.03.214 jelena...

18
Programmeerimine Programmeerimine Delphi keskkonnas Delphi keskkonnas MTAT.03.214 MTAT.03.214 Jelena Zaitseva Jelena Zaitseva [email protected]

Upload: francis-west

Post on 05-Jan-2016

270 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Programmeerimine Programmeerimine Delphi keskkonnasDelphi keskkonnas

MTAT.03.214MTAT.03.214

Jelena ZaitsevaJelena [email protected]

Page 2: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Tips on naming variablesTips on naming variables

Page 3: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Choose variable names with Choose variable names with carecare

x := x - xx;x := x - xx;

xxx := fido + SalesTax(fido);xxx := fido + SalesTax(fido);

x := x + LateFee(x1, x) + xxx;x := x + LateFee(x1, x) + xxx;

x := x + Interest(x1, x);x := x + Interest(x1, x);

Page 4: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Choose variable names with Choose variable names with carecare

x := x - xx;x := x - xx;

xxx := fido + SalesTax(fido);xxx := fido + SalesTax(fido);

x := x + LateFee(x1, x) + xxx;x := x + LateFee(x1, x) + xxx;

x := x + Interest(x1, x);x := x + Interest(x1, x);

Balance := Balance - LastPayment;Balance := Balance - LastPayment;

MonthlyTotal := NewPurchases + SalesTax(NewPurchases);MonthlyTotal := NewPurchases + SalesTax(NewPurchases);

Balance := Balance + LateFee(CustomerID, Balance) + Balance := Balance + LateFee(CustomerID, Balance) + MonthlyTotal;MonthlyTotal;

Balance := Balance + Interest(CustomerID, Balance);Balance := Balance + Interest(CustomerID, Balance);

Page 5: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Choosing a name for Choosing a name for variablevariable

Examples of good and bad names of Examples of good and bad names of variables:variables:

current date: CD, CurrentData, Current, Date, current date: CD, CurrentData, Current, Date, TodaysDateTodaysDate

lines per page: LinesPerPage, NumberOfLines, Lineslines per page: LinesPerPage, NumberOfLines, Lines

number of people on the Olympic team: NTM, number of people on the Olympic team: NTM,

NumberOfPeopleOnTheOlympicTeam, NumberOfPeopleOnTheOlympicTeam, TeamMembersCountTeamMembersCount

Page 6: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Common opposites in variable Common opposites in variable namesnames

first/lastfirst/last min/maxmin/max next/previousnext/previous old/newold/new visible/invisiblevisible/invisible source/target, source/destinationsource/target, source/destination locked/unlockedlocked/unlocked ……

Page 7: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Naming status variablesNaming status variables Think of a better name than flag for status variablesThink of a better name than flag for status variables

if (flag) then … if (IsDataReady) then …if (flag) then … if (IsDataReady) then …

if (printFlag = 2) then … if (ReportType = rtMonthly) then … if (printFlag = 2) then … if (ReportType = rtMonthly) then …

typetype

TReportType = (rtDaily, rtMonthly, rtAnnual);TReportType = (rtDaily, rtMonthly, rtAnnual);

TSomeNewClass = classTSomeNewClass = class

privateprivate

FReportType: TReportTypeFReportType: TReportType

publicpublic

property ReportType: TReportType read property ReportType: TReportType read FReportType;FReportType;

end;end;

Page 8: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Naming boolean variablesNaming boolean variables

Give boolean variables names that Give boolean variables names that imply true or false.imply true or false.

if (status) then …if (status) then … if (DataProcessed) then if (DataProcessed) then ……

if (IsDataProcessed) if (IsDataProcessed) then …then …

Page 9: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Naming enumerated typesNaming enumerated types

Using prefix helps to ensure if members Using prefix helps to ensure if members of a type belong to the same group.of a type belong to the same group.

typetype

TSound = (sndClick, sndClack, sndClock);TSound = (sndClick, sndClack, sndClock);

TMonth = (TMonth = (mJanuary, mFebruary, … , mDecembermJanuary, mFebruary, … , mDecember););

TBaseColor = (bcRed, bcGreen, bcBlue);TBaseColor = (bcRed, bcGreen, bcBlue);

TFavoriteColor = (fcRed, fcPink, fcBlue);TFavoriteColor = (fcRed, fcPink, fcBlue);

Page 10: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Kinds of names to avoid Kinds of names to avoid Avoid misleading names or abbreviationsAvoid misleading names or abbreviations Avoid names with similar meaningsAvoid names with similar meanings Avoid names with different meanings but similar Avoid names with different meanings but similar

namesnames Avoid numerals in namesAvoid numerals in names Avoid misspelled words in namesAvoid misspelled words in names Avoid the names of standard types, Avoid the names of standard types,

variables, and routinesvariables, and routines Don’t differentiate variable names solely by Don’t differentiate variable names solely by

capitalizationcapitalization Don’t use names that are totally unrelated to what the Don’t use names that are totally unrelated to what the

variable representvariable represent

if if=then then then := elseelse then := if;

Page 11: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Tips on writing codeTips on writing code

Page 12: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Boolean variablesBoolean variables

Use boolean variables to document your codeUse boolean variables to document your code Use boolean variables to simplify complicated Use boolean variables to simplify complicated

teststests

if ( (ElementIndex < 0) or if ( (ElementIndex < 0) or

(MAX_ELEMENT_INDEX < elementIndex) or (MAX_ELEMENT_INDEX < elementIndex) or

(ElementIndex == LastElementIndex) ) then …(ElementIndex == LastElementIndex) ) then …

Finished := (ElementIndex < 0) or Finished := (ElementIndex < 0) or

(MAX_ELEMENT_INDEX < elementIndex);(MAX_ELEMENT_INDEX < elementIndex);

Repeated := ElementIndex == LastElementIndex;Repeated := ElementIndex == LastElementIndex;

if (Finished or Repeated) then …if (Finished or Repeated) then …

Page 13: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

Enumerated typesEnumerated types

use enumerated types for readabilityuse enumerated types for readability– if ChosenColor = 1 thenif ChosenColor = 1 then– if ChosenColor = bcRed thenif ChosenColor = bcRed then

use enumerated types for use enumerated types for modifiabilitymodifiability

use enumerated types as an use enumerated types as an alternative to boolean variables alternative to boolean variables

Page 14: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

ConstantsConstants

use named constants in data use named constants in data declarationsdeclarations

avoid literals, even ‘safe’ onesavoid literals, even ‘safe’ ones for I:=for I:=11 to to 1212 do 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);YearProfit := YearProfit + Profit(Month);

use named constants consistentlyuse named constants consistently

Page 15: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

keep variables ‘live’ for as short a time as keep variables ‘live’ for as short a time as possiblepossible

Page 16: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

use only one statement per lineuse only one statement per line

YearProfit:=0; for Month:=mJanuary to mDecember do YearProfit:=0; for Month:=mJanuary to mDecember do YearProfit:=YearProfit+Profit(Month);YearProfit:=YearProfit+Profit(Month);

Page 17: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

write self-documenting codewrite self-documenting code

for I:=2 to GivenNumber dofor I:=2 to GivenNumber do

Numbers[I].MeetsCriteria := True;Numbers[I].MeetsCriteria := True;

for PrimeCandidate:=2 to GivenNumber dofor PrimeCandidate:=2 to GivenNumber do

Numbers[I].IsPrime := True;Numbers[I].IsPrime := True;

Page 18: Programmeerimine Delphi keskkonnas MTAT.03.214 Programmeerimine Delphi keskkonnas MTAT.03.214 Jelena Zaitseva jellen@ut.ee

document unclear dependencies with document unclear dependencies with commentscomments– comments should say things about code comments should say things about code

that the code can’t say about itself – at that the code can’t say about itself – at the summary level (focusing on the summary level (focusing on whywhy rather than rather than howhow))