Abstract Wikipedia/Updates/2024-01-25

Abstract Wikipedia Updates Translate

Abstract Wikipedia via mailing list Abstract Wikipedia on IRC Wikifunctions on Telegram Wikifunctions on Mastodon Wikifunctions on Twitter Wikifunctions on Facebook Wikifunctions on YouTube Wikifunctions website Translate

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 as they are often called.

 
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, 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 and Python on the Beta site.

 
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 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. Or it can be used for some artistic effect, to create l’art pour l’art. Or to playfully create a palindrome letter by letter, such as level.

 
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 and Python just use the + operator to concatenate three strings, referencing the wrapper twice, at the beginning and at the end. The composition uses join strings 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), Southeastern Kolami (Z1916), Tooro (Z1917), and Nupe (Z1919).