Recent

Author Topic: Defensive programming  (Read 18935 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Defensive programming
« on: February 12, 2018, 10:26:38 am »
I started a wiki topic on defensive programming techniques.
The first chapter is on range errors, how to debug them and how to prevent them.
I tried to write it for both beginners and intermediate programmers.
Tell me what you think. Advice and critique is welcome.
http://wiki.freepascal.org/Defensive_programming_techniques
Specialize a type, not a var.

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Defensive programming
« Reply #1 on: February 12, 2018, 10:37:23 am »
Looking good so far - thanks!

Some suggested additions for "defensive maintenance"
1. Initial project organisation - filenames and structures, using includes, version control models, resource planning
2. Naming schemes and commenting code for future maintenance (maybe by another programmer)
3. Pros and cons of OOP for minimising bugs
4. Using strict type checking to advantage
5. Considerations for possible future multiplatform deployment and/or multilanguage deployment
6. Recent (V1.8+) language features that enhance defensive programming (e.g. generics, UTF string issues)
7. Unit testing schemes
8. Avoiding "code creep" and bloat by defensive initial planning

You could split some topics into wiki sub-pages and/or reference existing wiki articles to prevent the main page from getting too long.
« Last Edit: February 12, 2018, 10:55:18 am by minesadorada »
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Defensive programming
« Reply #2 on: February 12, 2018, 10:39:50 am »
I started a wiki topic on defensive programming techniques.
The first chapter is on range errors, how to debug them and how to prevent them.
I tried to write it for both beginners and intermediate programmers.
Tell me what you think. Advice and critique is welcome.
http://wiki.freepascal.org/Defensive_programming_techniques

Nice, I like it.

In "for in..." example, you wrote in comment "I is not an index", but you use it as index first. So, I would change that example:
Code: Pascal  [Select][+][-]
  1. program dtp_1d;
  2. {$mode objfpc}{$R+}
  3. var
  4.   anArray:array[0..9] of integer; // ten elements
  5.   I: Low(anArray)..High(anArray);
  6.   N: integer; // N is an integer here: it is not an index, but a value from the array
  7. begin
  8.   // data to show what for in do does
  9.   for i := Low(anArray) to High(anArray) do anArray[i] := 100+i;
  10.   for N in anArray do  // for every integer value that is contained in the array
  11.     write(N:4); // writes the value of an array cell, this is not an index.
  12. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Defensive programming
« Reply #3 on: February 12, 2018, 10:50:49 am »
@Zoran
Fixed. Introduced j for integer value and use i as a traditional index.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Defensive programming
« Reply #4 on: February 12, 2018, 10:54:17 am »
Looking good so far - thanks!
I will certainly take your tips into account. First I will write some subjects on simple proper programming like try/finally, ioresult vs exceptions, string-handling, pointer types, that kind of stuff.
After that we can expand to more advanced subjects.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Defensive programming
« Reply #5 on: February 12, 2018, 11:24:36 am »
BTW added a bonus subject: use a range? You may want a set too...
Specialize a type, not a var.

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: Defensive programming
« Reply #6 on: February 12, 2018, 02:16:15 pm »
Excellent article promoting best practice. Many thanks.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Defensive programming
« Reply #7 on: February 12, 2018, 04:23:52 pm »
Excellent article. Thanks.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Defensive programming
« Reply #8 on: February 12, 2018, 05:10:04 pm »
Great, a must-read guide for Pascal users.
Thank you.

May I suggest something?
- How to use Heaptrc to test memory leak
- Need to mention: by default all checks are disabled (IO, range, overflow, stack, etc)
- A newbie ever asked, why they are disabled by default?

Below I provide a screenshot of Lazarus default settings for debugging:

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Defensive programming
« Reply #9 on: February 12, 2018, 05:17:14 pm »
Nice reading!

Currently the Lazarus book is being rewritten. Why don't you contact the editor Detlef Overbeck (editor@blaisepascal.eu) and propose your text as a chapter?

silvestre

  • Jr. Member
  • **
  • Posts: 76
Re: Defensive programming
« Reply #10 on: February 12, 2018, 05:35:49 pm »
Great article and very nice reading. There are many dark subjects to deal with. Memory leaks, debugger usage, etc.

I encourage the author to write more on the subject, he will have many followers... :)
« Last Edit: February 12, 2018, 05:38:43 pm by silvestre »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Defensive programming
« Reply #11 on: February 12, 2018, 05:44:04 pm »
Great stuff, Thaddy.
One further suggestion. In the "for in do..." example use an array of Char (or an array of some non-numeric type). This would make the difference between the array index and the array element obvious, and you would not need to say "j is not an index" etc.
It is also worth pointing out the read-only nature of the variable used in the syntax "for element-variable in anArray do", not only because the compiler cannot always detect attempts to alter element-variable's value, but also because it is an unexpected restriction.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Defensive programming
« Reply #12 on: February 12, 2018, 06:25:18 pm »
Tnx so far, there is more to come. And a big thanks for formatting the hints: just what I hoped for.  Thanx Kai!!!
« Last Edit: February 12, 2018, 07:36:15 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Defensive programming
« Reply #13 on: February 12, 2018, 07:12:48 pm »
Great stuff, Thaddy.
One further suggestion. In the "for in do..." example use an array of Char (or an array of some non-numeric type). This would make the difference between the array index and the array element obvious, and you would not need to say "j is not an index" etc.
It is also worth pointing out the read-only nature of the variable used in the syntax "for element-variable in anArray do", not only because the compiler cannot always detect attempts to alter element-variable's value, but also because it is an unexpected restriction.
Good suggestion. Feel free to correct any obvious language mistakes (?!) and discuss the above kind of valuable suggestions in this thread. I already have some rephrasing to do for tomorrow. :(
« Last Edit: February 12, 2018, 07:40:16 pm by Thaddy »
Specialize a type, not a var.

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Defensive programming
« Reply #14 on: February 12, 2018, 08:04:59 pm »
I like it! I look forward to good examples of try.. Finally.

Thank you.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

 

TinyPortal © 2005-2018