Please improve examples
Posted: Sun Sep 01, 2019 12:27 pm
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.):
If someone wants to use OpenLibrary() with #PB_Any rather than a self-chosen constant, then the first changes might look like this:
That's fine so far.
Now s/he replaces all occurences of 0 with lib and thus gets this code:
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:
So a didactically better version of the original example code is:
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:
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
Code: Select all
lib = OpenLibrary(#PB_Any, "User32.dll")
If lib
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
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
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
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