Bad examples of programming - i.e. code that you shouldn't use at all

Overview

End of the year is close to us and we mean that not only serious articles and examples can be useful. Sometimes, wrong approach and/or pieces or code should be helpful for awareness what do and what do not. Let's look at several practical code samples which illustrate (maybe) common mistakes and bad approach.

Using variable type with insufficient range

Code piece below tries to display day numbers within common year. Because we need at least 365 days and variable I is defined as a Byte, this procedure never ends. We should use rather "for I:=1 to 365 do ..." (where compiler checks variable range) instead "repeat..until".


procedure TForm1.Button1Click(Sender: TObject);
 var I:Byte;
begin
 I:=0;
 repeat
  Inc(I);
  Memo1.Lines.Add('Day number within the year:'+IntToStr(I));
 until I=365;
end;
Nested "with" clauses

Using "With" clause can be reasonable in certain situations but it's generally recommended to avoid using it - main reason is that code written in such way is confusing and unpredictable. Using following code, we probably cause total uncertainty of releasing appropriate objects. Many AV errors can be expected :-)


procedure TForm1.Button1Click(Sender: TObject);
 var MyList:TStrings;
     MyForm:TForm;
begin
 MyList:=TStringList.Create;
 MyForm:=TForm.Create(Self);
 with MyList do with MyForm do
 begin
  Add('Item one');
  Memo1.Lines.AddStrings(MyList);
  Free;
 end;
end;
Ommited "Next" method while going through database table

This mistake occurs very often and no wonder that going through table with few records lasts infinitely:-).


procedure TForm1.Button1Click(Sender: TObject);
begin
 Table1.Open;
 while not Table1.EOF do
 begin
  MyValue:=Table1ID.Value;
  Memo1.Lines.Add(MyValue);
 end;
end;
Naming variables anonymously

Naming your variables with short names or even letters is fast and handy but can it cause big troubles when you need to edit your code after longer period and aren't sure what every variable means. Using familiar and clear naming conventions for variables, constants and objects is much better way.


procedure TForm1.Button1Click(Sender: TObject);
 var A, B, C, D, H, I:integer;
     E, F, G:String;
begin
 for I:=1 to 1000 do
 begin
  A:=B+C;
  E:='Sum B+C is'+IntToStr(A);
  D:=B-C;
  F:='Difference B-C is'+IntToStr(D);
  H:=B*C;
  G:='Product B*C is'+IntToStr(H);
 end;
end;

Bad using "Next" method when deleting certain records from database table

Sometimes we need to delete set of records in a database table, such as all the records where price of item is greater than 100 (USD). Following approach is wrong and some records will remain undeleted.


procedure TForm1.Button1Click(Sender: TObject);
begin
 Table1.Open;
 while not Table1.EOF do
 begin
  if Table1Price>100 then Table1.Delete;
  Table1.Next;
 end;
end;

Proper way is very similar but "else" keyword is necessary:


procedure TForm1.Button1Click(Sender: TObject);
begin
 Table1.Open;
 while not Table1.EOF do
 begin
  if Table1Price>100 then Table1.Delete else
  Table1.Next;
 end;
end;

We're at the end of our New Year programmers jokes. Hope you enjoyed it and found useful. On behalf of Torry.net, I wish you prosperous new year 2018 and all the best!

About the Author

Ing. Karel Janeček, MBA, MSc.
Torry.net

More interesting links and pages


All binary option robots reviewed


Valforex.com Reviews


Breaking News & Headlines


FX Tribune - Breaking Daily News


USA News


BestVPNRating.com


Do my homework 123


My essay geek


My essay writing


Syllable words


Car hire prices comparison


College paper world


Essay writing help