The on-wiki version of this newsletter can be found here:
https://meta.wikimedia.org/wiki/Abstract_Wikipedia/Updates/2024-01-25
Due to the formatting, the on-wiki version might be easier to read than the
email.
--
Getting debug information from code
Contributors to Wikifunctions can provide Implementations in two different
ways: either by composing existing Functions, or by providing code in
either JavaScript or Python. And as everyone who has ever created
programming code will confirm, it is unavoidable to have mistakes in the
code one writes, or bugs <https://en.wikipedia.org/wiki/Software_bug> as
they are often called.
<https://meta.wikimedia.org/wiki/File:First_Computer_Bug,_1945.jpg>*An
actual bug that was found in a 1940s computer that caused an error, as told
by Admiral Grace Hopper.*
A very widespread way for debugging is to pepper your code with print
statements, and then remove them again once you figured out and fixed the
bug. A print statement either displays the argument of the statement on the
console or adds it to a logfile. This is usually done in order to inspect
the state of a variable at the point in the code where the print statement
is added. Let’s assume the following JavaScript code was written with the
goal to create a string with five stars:
let result = ""for (let i=0; i=5; i++) { result += "★"}
Some folks may recognize the error immediately, but for others it might be
obscure: why does the computer not return from that code?
One way forward could be to add a print statement to the code. One way to
add a print statement in JavaScript is to write to console.log:
let result = ""for (let i=0; i=5; i++) { console.log(i) result +=
"★"}
And we see on the console that we get many, many lines with a 5, instead of
the expected 0, 1, 2, 3, 4. This can be an indication of the problem in the
given code, that the condition in the loop should not be i=5, which means
that i is set to the value 5, but should be i!=5, where we compare i with 5
(and even a bit more robust, it should be i<5).
This method is usually called print debugging or tracing
<https://en.wikipedia.org/wiki/Debugging#Techniques>, and although it has a
bad reputation with some folks, it is an often and widely used effective
method to find issues in code and also to just understand code. And it is
now also available in Wikifunctions! You can use the command
Wikifunctions.Debug one or more times with a string argument, and after
running the Implementation your debug strings will be present in the
Details view of the Function result output. Two examples for using
Wikifunctions.Debug can be found for JavaScript
<https://wikifunctions.beta.wmflabs.org/view/en/Z11647> and Python
<https://wikifunctions.beta.wmflabs.org/view/en/Z10057> on the Beta site.
<https://meta.wikimedia.org/wiki/File:Debugger_output_in_Wikifunctions.png>*Details
of a function run. The last bullet point contains the log.*
We would suggest keeping the code free of calls to Wikifunctions.Debug
unless an Implementation is being actively debugged. This will be
particularly helpful once we allow the feature to call one function from
another through code, which could otherwise lead very quickly to very
unreadable log files. We would suggest that the Wikifunctions community
should consider adding this to a style guide for published and connected
functions.
Congratulations to Cory, who shepherded this functionality from idea to
deployment! David assisted by updating the Details view to nicely display
the debugging strings.
Function of the Week: Wrap string
Wrap string <https://www.wikifunctions.org/wiki/Z11145> takes two strings,
the *string to wrap* and the *wrapper*, and returns the former wrapped
between two occurrences of the latter. This can be used to quote some
content with straight quotes, as in the first Test the Function had
<https://www.wikifunctions.org/wiki/Z11147>. Or it can be used for some
artistic effect, to create *l’art
<https://www.wikifunctions.org/wiki/Z12829> pour l’art
<https://en.wikipedia.org/wiki/Art_for_art%27s_sake>*. Or to playfully
create a palindrome letter by letter, such as level
<https://www.wikifunctions.org/wiki/Z12830>.
<https://meta.wikimedia.org/wiki/File:Falafel_%26Tzatziki_Wrap_-_Lavash.jpg>*Wrap(Falafel,
Lavash). Note that the pictured wrap is three dimensional, whereas the
Function we discuss here is only working on a single dimension.*
The other two Tests linked here have been written in preparation for this
post. The Function used to have a single Test, and now has three. Just as
last time, it would be great to have more Tests, in order to ensure that
the Implementations work with a larger number of possible inputs. Does the
Function work well with other scripts, e.g. Chinese or Arabic? Does it work
with Emojis? What about numbers? Does it work with empty strings? In
general, we should strive to have more Tests on our Functions, in order to
increase the confidence in our system and our Implementations.
The three implementations we have are quite straightforward: both JavaScript
<https://www.wikifunctions.org/wiki/Z11148> and Python
<https://www.wikifunctions.org/wiki/Z11149> just use the + operator to
concatenate three strings, referencing the wrapper twice, at the beginning
and at the end. The composition <https://www.wikifunctions.org/wiki/Z11146>
uses join strings <https://www.wikifunctions.org/wiki/Z10000> twice, first
to add the wrapper to the end of the string to wrap, and then once more to
add the string to wrap before the result of that first call.
Recent Changes in the software
This week was mostly a quiet one in terms of new code deployed, as the team
focussed on planning future work. The biggest change was our first
production release of our back-end services this calendar year; none of the
commits included are intended to bring any user-visible changes (beyond
being a little safer and more rigorous). We also fixed a bug with certain
kinds of uses of types that meant the system previously didn't recognise
the call as a valid Object, and improved our code documentation for people
getting started with our code. We also added four new languages that
MediaWiki supports: Rutul (Z1915) <https://www.wikifunctions.org/wiki/Z1915>
, Southeastern Kolami (Z1916) <https://www.wikifunctions.org/wiki/Z1916>, Tooro
(Z1917) <https://www.wikifunctions.org/wiki/Z1917>, and Nupe (Z1919)
<https://www.wikifunctions.org/wiki/Z1919>.