This year I’ve been focusing on reading more. In previous years I’ve read mostly read other blogs, but this year I have been been reading more books (both fiction and non-fiction) and quality news (news that requires a subscription and is not fully reliant advertising).
I subscribed to digital access of the Australian Financial Review and actually get it physically delivered on Saturdays. Confusingly, getting full digital access AND getting the paper physically delivered on a Saturday was half the price of a digital access only subscription.
In last Saturday’s paper there was an article about NSW lagging behind on introducing a digital skills curriculum, which mentioned Swinburne University of Technology computing professor Leon Sterling as saying that a lot of people were put off by the idea that computer classes were just about teaching kids how to do coding, but coding was just a way to get students thinking differently.
Reading the article made me think about how learning to code had trained me to solve problems with algorithms, and the benefits of being able to do so. I think everyone can realise and understand the efficiency of being able to automate repetitive tasks with code/algorithms, but I think that the personal enjoyment and satisfaction of engaging your brain to design the algorithms and write the code often goes unappreciated.
I demonstrated a way of writing a simple line of code to avoid to manual drudgery today, and it inspired me to write this post.
I noticed one of the One Model developers typing a series of numbers into a calculator. It turned out they were calculating how many lines of code they had edited in a pull request. The numbers were coming from the green and boxes shown in the following picture (they had a significantly longer list than this example).
How I solved this with an algorithm and some code was:
- I opened up the Chrome’s developer tools by right clicking on the first green box and selecting “Inspect” from the right click menu.
- The first part of the algorithm needs to select all the numbers in the green boxes. I changed the selected HTML element to the parent that contained all the green boxes. Observing that the element for the green box had a “lines-added” class. I wrote the following script into the developer tools console to return all the green box elements.
$(".lines-added", $0).toArray().map(box => parseInt(box.innerText))
- The second part was to add those numbers together. To do that I extended the script to what follows.
$(".lines-added", $0).toArray().map(box => parseInt(box.innerText)) .reduce((total, value) => total + value)
This output the total of 538 into the console and was much easier than typing them one by one into a calculator.