Вот пишу вычислительную по Delphi. (Некогда писать и отлаживать универсальные процедуры и функций.) И ужасаюсь, что у меня некоторые процедуры выходят приличной длины. Вообще-то меня они не смущают, но где-то слышал, что лучше не растягивать (вмещаться до 20 строк) и писать по-короче, но у меня не всегда так получается.
Так например:
| Код | procedure TfmDeleteRecords.sbDeleteRecordsClick(Sender: TObject); var i : Integer; begin {------------------------------------} { Якщо видаляємо записи користувачів } {------------------------------------} if fmDeleteRecords.cbChangeDB.Text='База користувачів' then begin for i:=1 to fmMain.sgUser.RowCount-1 do begin { Шукаємо I параметр. } if (leUserName.Text<>'') then { Якщо є такий I_параметр - ставимо мітку } if (pos(leUserName.Text, fmMain.sgUser.Cells[0, i])>0) then fmMain.sgUser.RowHeights[i]:=-ABS(fmMain.sgUser.RowHeights[i]);
{ Шукаємо II параметр. } if (leUserGroup.Text<>'') then begin { Якщо є такий II_параметр і I_параметр не шукали - ставимо мітку } if (pos(leUserGroup.Text, fmMain.sgUser.Cells[1, i])>0) and (leUserName.Text='') then fmMain.sgUser.RowHeights[i]:=-ABS(fmMain.sgUser.RowHeights[i]); { Якщо немає такого II_параметру, а попередній пошук знайшов - знімаємо мітку } if (pos(leUserGroup.Text, fmMain.sgUser.Cells[1, i])=0) and (fmMain.sgUser.RowHeights[i]<0) then fmMain.sgUser.RowHeights[i]:=ABS(fmMain.sgUser.RowHeights[i]); end;
{ Шукаємо III параметр. } if (leUserPassword.Text<>'') then begin { Якщо є такий III_параметр і I-II_параметри не шукали - ставимо мітку } if (pos(leUserPassword.Text, fmMain.sgUser.Cells[2, i])>0) and (leUserName.Text='') and (leUserGroup.Text='') then fmMain.sgUser.RowHeights[i]:=-ABS(fmMain.sgUser.RowHeights[i]); { Якщо немає такого III_параметру, а попередні пошуки знайшли - знімаємо мітку } if (pos(leUserPassword.Text, fmMain.sgUser.Cells[2,i])=0) and (fmMain.sgUser.RowHeights[i]<0) then fmMain.sgUser.RowHeights[i]:=ABS(fmMain.sgUser.RowHeights[i]); end; end; end;
{------------------------------} { Якщо видаляємо записи питань } {------------------------------} if fmDeleteRecords.cbChangeDB.Text='База питань' then begin for i:=1 to fmMain.sgQuestion.RowCount-1 do begin { Шукаємо I параметр. } if (leQuestionName.Text<>'') then { Якщо є такий I_параметр - ставимо мітку } if (pos(leQuestionName.Text, fmMain.sgQuestion.Cells[0, i])>0) then fmMain.sgQuestion.RowHeights[i]:=-ABS(fmMain.sgQuestion.RowHeights[i]);
{ Шукаємо II параметр. } if (leQuestionTheme.Text<>'') and (leQuestionTheme.Enabled=True) then begin { Вибираємо метод оцінки < = > } { Якщо є такий II_параметр і I_параметр не шукали - ставимо мітку } if ((cbQuestionTheme.Text='<') and (fmMain.sgQuestion.Cells[1, i]<leQuestionTheme.Text) and (leQuestionName.Text='')) OR ((cbQuestionTheme.Text='=') and (fmMain.sgQuestion.Cells[1, i]=leQuestionTheme.Text) and (leQuestionName.Text='')) OR ((cbQuestionTheme.Text='>') and (fmMain.sgQuestion.Cells[1, i]>leQuestionTheme.Text) and (leQuestionName.Text='')) then fmMain.sgQuestion.RowHeights[i]:=-ABS(fmMain.sgQuestion.RowHeights[i]); { Якщо немає такого II_параметру, а попередній пошук знайшов - знімаємо мітку } if ((cbQuestionTheme.Text='<') and (fmMain.sgQuestion.Cells[1, i]>=leQuestionTheme.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) OR ((cbQuestionTheme.Text='=') and (fmMain.sgQuestion.Cells[1, i]=leQuestionTheme.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) OR ((cbQuestionTheme.Text='>') and (fmMain.sgQuestion.Cells[1, i]<=leQuestionTheme.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) then fmMain.sgQuestion.RowHeights[i]:=ABS(fmMain.sgQuestion.RowHeights[i]); end;
{ Шукаємо III параметр. } if (leQuestionRightAnswer.Text<>'') and (leQuestionRightAnswer.Enabled=True) then begin { Вибираємо метод оцінки < = > } { Якщо є такий III_параметр і I-II_параметр не шукали - ставимо мітку } if ((cbQuestionRightAnswer.Text='<') and (fmMain.sgQuestion.Cells[2, i]<leQuestionRightAnswer.Text) and (leQuestionName.Text='') and (leQuestionTheme.Enabled=False)) OR ((cbQuestionRightAnswer.Text='=') and (fmMain.sgQuestion.Cells[2, i]=leQuestionRightAnswer.Text) and (leQuestionName.Text='') and (leQuestionTheme.Enabled=False)) OR ((cbQuestionRightAnswer.Text='>') and (fmMain.sgQuestion.Cells[2, i]>leQuestionRightAnswer.Text) and (leQuestionName.Text='') and (leQuestionTheme.Enabled=False)) then fmMain.sgQuestion.RowHeights[i]:=-ABS(fmMain.sgQuestion.RowHeights[i]); { Якщо немає такого III_параметру, а попередній пошук знайшов - знімаємо мітку } if ((cbQuestionRightAnswer.Text='<') and (fmMain.sgQuestion.Cells[2, i]>=leQuestionRightAnswer.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) OR ((cbQuestionRightAnswer.Text='=') and (fmMain.sgQuestion.Cells[2, i]=leQuestionRightAnswer.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) OR ((cbQuestionRightAnswer.Text='>') and (fmMain.sgQuestion.Cells[2, i]<=leQuestionRightAnswer.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) then fmMain.sgQuestion.RowHeights[i]:=ABS(fmMain.sgQuestion.RowHeights[i]); end;
{ Шукаємо IV параметр. } if (leQuestionPrice.Text<>'') and (leQuestionPrice.Enabled=True) then begin { Вибираємо метод оцінки < = > } { Якщо є такий IV_параметр і I-III_параметр не шукали - ставимо мітку } if ((cbQuestionPrice.Text='<') and (fmMain.sgQuestion.Cells[3, i]<leQuestionPrice.Text) and (leQuestionName.Text='') and (leQuestionTheme.Enabled=False) and (leQuestionRightAnswer.Enabled=False)) OR ((cbQuestionPrice.Text='=') and (fmMain.sgQuestion.Cells[3, i]=leQuestionPrice.Text) and (leQuestionName.Text='') and (leQuestionTheme.Enabled=False) and (leQuestionRightAnswer.Enabled=False)) OR ((cbQuestionPrice.Text='>') and (fmMain.sgQuestion.Cells[3, i]>leQuestionPrice.Text) and (leQuestionName.Text='') and (leQuestionTheme.Enabled=False) and (leQuestionRightAnswer.Enabled=False)) then fmMain.sgQuestion.RowHeights[i]:=-ABS(fmMain.sgQuestion.RowHeights[i]); { Якщо немає такого IV_параметру, а попередній пошук знайшов - знімаємо мітку } if ((cbQuestionPrice.Text='<') and (fmMain.sgQuestion.Cells[3, i]>=leQuestionPrice.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) OR ((cbQuestionPrice.Text='=') and (fmMain.sgQuestion.Cells[3, i]=leQuestionPrice.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) OR ((cbQuestionPrice.Text='>') and (fmMain.sgQuestion.Cells[3, i]<=leQuestionPrice.Text) and (fmMain.sgQuestion.RowHeights[i]<0)) then fmMain.sgQuestion.RowHeights[i]:=ABS(fmMain.sgQuestion.RowHeights[i]); end; end; end;
{---------------------------} { Якщо видаляємо записи тем } {---------------------------} if fmDeleteRecords.cbChangeDB.Text='База тем' then begin for i:=1 to fmMain.sgTheme.RowCount-1 do begin { Шукаємо I параметр. } if (leThemeNumber.Text<>'') and (leThemeNumber.Enabled=True) then begin { Вибираємо метод оцінки < = > } { Якщо є такий I_параметр - ставимо мітку } if ((cbThemeNumber.Text='<') and (fmMain.sgTheme.Cells[0, i]<leThemeNumber.Text)) OR ((cbThemeNumber.Text='=') and (fmMain.sgTheme.Cells[0, i]=leThemeNumber.Text)) OR ((cbThemeNumber.Text='>') and (fmMain.sgTheme.Cells[0, i]>leThemeNumber.Text)) then fmMain.sgTheme.RowHeights[i]:=-ABS(fmMain.sgTheme.RowHeights[i]); end;
{ Шукаємо II параметр. } if (leThemeName.Text<>'') then begin { Якщо є такий II_параметр і I_параметр не шукали - ставимо мітку } if (pos(leThemeName.Text, fmMain.sgTheme.Cells[1, i])>0) and (leThemeNumber.Text='') then fmMain.sgTheme.RowHeights[i]:=-ABS(fmMain.sgTheme.RowHeights[i]); { Якщо немає такого II_параметру, а попередній пошук знайшов - знімаємо мітку } if (pos(leThemeName.Text, fmMain.sgTheme.Cells[1, i])=0) and (fmMain.sgTheme.RowHeights[i]<0) then fmMain.sgTheme.RowHeights[i]:=ABS(fmMain.sgTheme.RowHeights[i]); end;
{ Шукаємо III параметр. } if (leThemePrice.Text<>'') and (leThemePrice.Enabled=True) then begin { Вибираємо метод оцінки < = > } { Якщо є такий III_параметр, і I-II_параметри не шукали - ставимо мітку } if ((cbThemePrice.Text='<') and (fmMain.sgTheme.Cells[2, i]<leThemePrice.Text) and (leThemeNumber.Text='') and (leThemeName.Text='')) OR ((cbThemePrice.Text='=') and (fmMain.sgTheme.Cells[2, i]=leThemePrice.Text) and (leThemeNumber.Text='') and (leThemeName.Text='')) OR ((cbThemePrice.Text='>') and (fmMain.sgTheme.Cells[2, i]>leThemePrice.Text) and (leThemeNumber.Text='') and (leThemeName.Text='')) then fmMain.sgTheme.RowHeights[i]:=-ABS(fmMain.sgTheme.RowHeights[i]); { Якщо немає такого III_параметру, а попередні пошуки знайшли - знімаємо мітку } if ((cbThemePrice.Text='<') and (fmMain.sgTheme.Cells[2,i]>=leThemePrice.Text) and (fmMain.sgTheme.RowHeights[i]<0)) OR ((cbThemePrice.Text='=') and (fmMain.sgTheme.Cells[2,i]<>leThemePrice.Text) and (fmMain.sgTheme.RowHeights[i]<0)) OR ((cbThemePrice.Text='>') and (fmMain.sgTheme.Cells[2,i]<=leThemePrice.Text) and (fmMain.sgTheme.RowHeights[i]<0)) then fmMain.sgTheme.RowHeights[i]:=ABS(fmMain.sgTheme.RowHeights[i]); end; end; end;
fmMain.sbSaveTable.Click; fmMain.sbOpenDB.Click; end;
|
Эта процедура занимает 250 строк (включая комментарии). Это нормальная практика для программиста вообще и для Delphi-программиста в частности? Так и должно быть?
З.Ы.: Не было времени переводить комментарии на русский, потому извиняйте. Также некоторые условия не оптимизированы. По-этому не швыряться тапками =) (Знаю Pascal 1 год, Delphi - 1/2 года). |