* * *

Author Topic: Defensive programming  (Read 1376 times)

eny

  • Hero Member
  • *****
  • Posts: 1574
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: 4175
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: 5353
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 »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

lainz

  • Hero Member
  • *****
  • Posts: 2252
  • I'm coding :)
    • BGRAControls
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.
If you use BGRAControls please donate

Thaddy

  • Hero Member
  • *****
  • Posts: 5353
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 »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Zath

  • Sr. Member
  • ****
  • Posts: 267
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: 1574
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: 4175
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: 355
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.

 

Recent

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