* * *

Author Topic: Defensive programming  (Read 6758 times)

eny

  • Hero Member
  • *****
  • Posts: 1580
Re: Defensive programming
« Reply #15 on: February 12, 2018, 08:15:43 pm »
I would not waste too much real estate on erroneous code.
Show the examples how it should be done and only briefly mention the pitfalls.
All posts based on: Win10 (Win64); Lazarus 1.8.0 'stable' (#56594 win64) unless specified otherwise...

wp

  • Hero Member
  • *****
  • Posts: 4836
Re: Defensive programming
« Reply #16 on: February 12, 2018, 08:27:52 pm »
I would not waste too much real estate on erroneous code.
Show the examples how it should be done and only briefly mention the pitfalls.
No - the tutorial is much more instructive this way, with erroneos code.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Thaddy

  • Hero Member
  • *****
  • Posts: 6556
Re: Defensive programming
« Reply #17 on: February 12, 2018, 08:59:44 pm »
@eny
@wp
That's my intention because what seems obvious is not so obvious in the real world. I intentionally create "easy to spot" bugs that are often much harder to spot, but the same kind of bug.
Think 100.000 lines of code (I have done much more than that - within a team - in some jobs).
It must be manageable, repeatable and understandable.
Feel free to help me! It's a wiki! Only thing is: let me fill in the empty subjects first: I have code for that. And carefully weighted text.
Also note it is about defensive programming, not programming...(well, it is... :D :D :D )

Note it is actually very hard to create common bugs.....for educational purposes.. And that what is needed to teac defensive programming: start with the error.
And... eny... did you know that? "use ranges instead of integers for your index and think about range when writing your code! It will prevent you from introducing range errors and you will catch them at compile time."..... :P
« Last Edit: February 12, 2018, 10:40:34 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

lainz

  • Hero Member
  • *****
  • Posts: 2820
    • Home
Re: Defensive programming
« Reply #18 on: February 12, 2018, 09:15:44 pm »
Nice article in progress.

I agree on that instead of using J for the for.. in you can change it to "item" or "element", that is common in forEach (JS) and is more descriptive.

Thaddy

  • Hero Member
  • *****
  • Posts: 6556
Re: Defensive programming
« Reply #19 on: February 12, 2018, 09:19:27 pm »
Point taken, lainz.
Also a good suggestion.

Question: should I split low() high() from range?
[edit] Now it reads item... tnx!! I didn't change it everywhere, though.
May solve Howardpc's issue too. will think.
« Last Edit: February 12, 2018, 10:34:58 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Zath

  • Sr. Member
  • ****
  • Posts: 288
Re: Defensive programming
« Reply #20 on: February 13, 2018, 01:11:06 am »
Very nice, I hope you complete it.
I like the idea of a range and then set of range. Made sense to me first time too, not a common occurrence !

eny

  • Hero Member
  • *****
  • Posts: 1580
Re: Defensive programming
« Reply #21 on: February 13, 2018, 06:33:49 pm »
No - the tutorial is much more instructive this way, with erroneos code.
Of course not.
When I want to learn another language, I do not want to first see a long list of erroneous examples.
And then have to dig for the right words or phrases.
I want to see how I can properly use the language elements.
Exactly the same with this.

To make the topic useable, instruct people how to properly use the language constructs (indeed use ranges and predefined set types where applicable).
But do not let them dig through randomly chosen erroneous code fragments and at the end force them to forget all of the above, and make them remember the last bits that supposedly are correct(-ish).

Would be funny if people went to any training, school or course, and half of the time get explained how nót to do something.
Waste of time.
All posts based on: Win10 (Win64); Lazarus 1.8.0 'stable' (#56594 win64) unless specified otherwise...

del

  • New member
  • *
  • Posts: 14
Re: Defensive programming
« Reply #22 on: February 13, 2018, 06:52:22 pm »
I like it. After many years my brain is very "C" like but you can't play fast and loose with memory in Pascal either. This range discipline stuff is pretty slick. If there's any doubt I usually clip or modulus my indices - but that's kinda sloppy.

wp

  • Hero Member
  • *****
  • Posts: 4836
Re: Defensive programming
« Reply #23 on: February 13, 2018, 07:45:26 pm »
No - the tutorial is much more instructive this way, with erroneos code.
Of course not.
When I want to learn another language, I do not want to first see a long list of erroneous examples.
And then have to dig for the right words or phrases.
I want to see how I can properly use the language elements.
Exactly the same with this.

To make the topic useable, instruct people how to properly use the language constructs (indeed use ranges and predefined set types where applicable).
But do not let them dig through randomly chosen erroneous code fragments and at the end force them to forget all of the above, and make them remember the last bits that supposedly are correct(-ish).

Would be funny if people went to any training, school or course, and half of the time get explained how nót to do something.
Waste of time.

This would be correct if this wiki would address beginners. But it does not. A beginner will not understand what Thaddy is talking about. But if the reader at least has some experience Thaddy's approach is much more instructive because the reader always can compare his own coding practice with the "correct" one. He directly sees the code which he normally would type and can find what's wrong with it.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 392
Re: Defensive programming
« Reply #24 on: February 14, 2018, 07:16:15 pm »
This is actually about: never declare something more than once.

Two other good ones would be: never expect your parameters to hold legal values, and don't optimize anything before it really is too slow or complex.

If you want, I'll add those and more myself.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1183
Re: Defensive programming
« Reply #25 on: February 25, 2018, 01:29:48 am »
http://wiki.freepascal.org/Defensive_programming_techniques

Awesome.  Took me far too long to get into a defensive mindset - now I push it on every beginner I find.  Having a reference document set up makes that far easier.  Many thanks, and good luck
Lazarus Trunk/FPC Trunk on Linux & Windows [7, 8.1]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

kupferstecher

  • Full Member
  • ***
  • Posts: 208
Re: Defensive programming
« Reply #26 on: April 11, 2018, 12:21:13 pm »
How about iterations through lists? Like TList and TStrings and descendants. Is it it somehow possible to use the IN keyword there?

Code: Pascal  [Select]
  1. for ii in MyList.___ do;

Is any property/field capable of providing that information?
Currently I do it like that (which is ugly...):
Code: Pascal  [Select]
  1. for ii:= 0 to MyList.count-1 do;


@Thaddy: When do you go on with the other chapters?  :)

RayoGlauco

  • Jr. Member
  • **
  • Posts: 76
  • Beers: 1567
Re: Defensive programming
« Reply #27 on: April 11, 2018, 12:41:43 pm »
kupferstecher, you can do like this:

Code: Pascal  [Select]
  1. var slist:tStringList; plist:tList; s:string; p:pointer;
  2. begin
  3.   ... // initialize lists
  4.   for s in slist do;
  5.   for p in plist do;
  6. end;    
« Last Edit: April 11, 2018, 12:46:12 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

kupferstecher

  • Full Member
  • ***
  • Posts: 208
Re: Defensive programming
« Reply #28 on: April 11, 2018, 01:47:15 pm »
Thats great, thanks!
I did some trials and the following seems to work as well. I don't like the type cast, but seems not possible without it.
Code: Pascal  [Select]
  1. var
  2.   LabelList:tList;
  3.   lbl:TLabel;
  4. begin
  5.   ... // initialize lists
  6.   for pointer(lbl) in LabelList do lbl.Color:= clRed;
  7. end;    

Thaddy

  • Hero Member
  • *****
  • Posts: 6556
Re: Defensive programming
« Reply #29 on: April 11, 2018, 02:10:13 pm »
I have two more chapters written and almost ready (1 short, 1 very long) but "simple" examples need extensive testing. Especially when some of them need to fail in a not very obvious way....That is harder than you might think.
Probably over the weekend.
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus