Please improve examples

Found an issue in the documentation ? Please report it here !

Moderator: Documentation Editors

Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Please improve examples

Post by Little John »

This is not a bug report, but a suggestion for improvement of the documentation.
However, I didn't find a better thread for posting it.

Summary:
Several examples in the help can be improved by using constants with meaningful names, rather than using 0 for different purposes.


In several examples in the help, 0 is used pretty often with different meanings. This is didactically not good, and can even be confusing as was recently seen here on the forum. The following code illustrates the problem (It's the second example from the "Prototypes" chapter, with just comments and some blank lines removed.):

Code: Select all

Prototype.i ProtoMessageBoxW(Window.i, Body.p-unicode, Title.p-unicode, Flags.i = 0)

If OpenLibrary(0, "User32.dll")
   MsgBox.ProtoMessageBoxW = GetFunction(0, "MessageBoxW")
   MsgBox(0, "Hello", "World")
EndIf
If someone wants to use OpenLibrary() with #PB_Any rather than a self-chosen constant, then the first changes might look like this:

Code: Select all

lib = OpenLibrary(#PB_Any, "User32.dll")
If lib
That's fine so far.

Now s/he replaces all occurences of 0 with lib and thus gets this code:

Code: Select all

Prototype.i ProtoMessageBoxW(Window.i, Body.p-unicode, Title.p-unicode, Flags.i = 0)

lib = OpenLibrary(#PB_Any, "User32.dll")
If lib   
   MsgBox.ProtoMessageBoxW = GetFunction(lib, "MessageBoxW")
   MsgBox(lib, "Hello", "World")
EndIf
But this code is not working anymore!
This is because the 0 used with the MsgBox() function has a different meaning than the other zeros here, and must not be replaced with lib :!:

The proper code with #PB_Any is:

Code: Select all

Prototype.i ProtoMessageBoxW(Window.i, Body.p-unicode, Title.p-unicode, Flags.i = 0)

lib = OpenLibrary(#PB_Any, "User32.dll")
If lib   
   MsgBox.ProtoMessageBoxW = GetFunction(lib, "MessageBoxW")
   MsgBox(0, "Hello", "World")
EndIf
So a didactically better version of the original example code is:

Code: Select all

Prototype.i ProtoMessageBoxW(Window.i, Body.p-unicode, Title.p-unicode, Flags.i = 0)
#Library = 0

If OpenLibrary(#Library, "User32.dll")
   MsgBox.ProtoMessageBoxW = GetFunction(#Library, "MessageBoxW")
   MsgBox(0, "Hello", "World")
EndIf
There are other example codes in the help, that contain even more zeros for different purposes (window number, gadget number etc.). Those examples can be pretty confusing, especially for beginners.


BTW, since I'm on the subject right now:
Another improvement of example codes would be, to make them ready for usage with EnableExplicit. So I actually would write the respective examle code like this:

Code: Select all

Prototype.i ProtoMessageBoxW(Window.i, Body.p-unicode, Title.p-unicode, Flags.i = 0)
Define MsgBox.ProtoMessageBoxW
#Library = 0

If OpenLibrary(#Library, "User32.dll")
   MsgBox = GetFunction(#Library, "MessageBoxW")
   MsgBox(0, "Hello", "World")
EndIf
Last edited by Little John on Sun Sep 01, 2019 2:19 pm, edited 1 time in total.
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Please improve examples

Post by skywalk »

YES!
I would prefer we force EnableExplicit always on.
Without it, code is wide open for bugs and automation tools must be nearly as intelligent as the compiler.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Please improve examples

Post by normeus »

+1
I think better examples would really help acquire more users, and dare I say "younger programmers".
Maybe something can be setup where we collaborate with HTML formated examples so it could be easier to add to the help file:

Code: Select all

<title>FileSize()</title>
<h2>Example</h2>

<code=Purebasic>
fname$="example.txt"
If FileSize(fname$) > 0
     debug "file exist and has readable content"
Else
      debug "File does not have content or is a directory"
 EndIf
</code>

I know, its a dumb example, but the HTML is what I wanted to show.

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Please improve examples

Post by Bitblazer »

+1

Several examples teach a very questionable coding style to beginners. I realize that the examples arent meant to teach coding style, but actually by repeatedly showing a bad style to beginners, you shouldnt be surprised if thats what they use.

and if you say "Too bad for them, but who cares? The purebasic documentation isnt a beginners course to programming!"

Well a beginner who gets frustrated due to not finding a bug thanks to learning a bad style like this, is likely somebody you lose in the long run - either to another hobby or language ... and there you lose a future customer and deal with a shrinking community.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Please improve examples

Post by davido »

+1
DE AA EB
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Please improve examples

Post by Tenaja »

A wiki, and we could help fix this...
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2058
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Please improve examples

Post by Andre »

Good points! But as this is something general (the "style" of PB codes), which would need the adapting of a lot of code examples in the PB manual, I wait for a comment of Fred here... 8)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Please improve examples

Post by Mijikai »

I would also 'force' EnableExplicit

In the beginning i never used it and though i dont need it... obviously a mistake :)
Now i always use it (i also use End btw.).

I think it would be a good thing to have it only as check box option in the compiler settings that by default is always on.
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Please improve examples

Post by Bitblazer »

If Fred creates a wiki with the pb existing example codes, links to them in the manual, copy&pastes the existing ones to the wiki and gives a few trustable community members editing rights - i am pretty sure a mutual clean standard can be agreed upon and all examples being reformatted, adjusted, enhanced and new ones probably added :)

ps: yes enableexplicit is a must. I consider the fact that such a switch is even optional, as part of a more wild history ;) Its the first command in every source i write with PB.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Please improve examples

Post by BarryG »

EnableExplicit: Optional, yes. Mandatory, no. I don't want it always on when I just need to test some quick code from scratch, because it wastes my time declaring all the variables. That's part of PureBasic's charm: it's quick and easy to write some short code. The user can always use EnableExplicit for their release executables, if they wish.
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: Please improve examples

Post by Lord »

BarryG wrote:EnableExplicit: Optional, yes. Mandatory, no.
...
:!: :!: :!:
Image
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Please improve examples

Post by Denis »

+1
EnableExplicit by default will be good.
A+
Denis
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Please improve examples

Post by Mesa »

I let Fred answer, if he wants but from my point of view, this will not happen because the small examples of the documentation must be understandable at first sight. So we will keep a '0' rather than a '#pb_any'. There are here to illustrate a function, not to be used 'as is'. And add 'enableexplicit' could divert attention from the essential.

In addition, the philosophy of Basic is not to use 'enableexplicit' but to have the choice to use it, unlike the philosophy of C-like languages, so why put it in all the examples.

I can add in the doc that the use of 'enableexplicit' is recommended for large projects or to avoid typing errors if you like.

Mesa.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Please improve examples

Post by DK_PETER »

Revised examples: Sure.

Is it really that hard to write

Code: Select all

EnableExplicit
or

Code: Select all

DisableExplicit
?
Extremely redundant request... :?
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please improve examples

Post by Little John »

Andre wrote:Good points! But as this is something general (the "style" of PB codes), which would need the adapting of a lot of code examples in the PB manual, I wait for a comment of Fred here... 8)
Thank you, Andre.
Post Reply