How does nobody of these morons understand that phones are in reality at least 80% pocket entertainment and content consumption machines. Yeah you can text, call and browse the web for answers I guess, but I bet that tiktok, instagram and youtube eclipse anything else in terms of time spent in these apps. Its not surprising that our phones are 6"+ pocket screens.
Essentially a function that doesn’t produce side effects, like modifying variables outside of its scope or modifying the function parameters. This something you should always try to incorporate into your code as it makes it much easier to test and makes the function’s use less risky since you don’t relay on external unrelated values.
To give you an example in JavaScript, here are two ways to replace certain numbers from an other list of numbers with the number 0
first a way to do it with a non pure function :
let bannedNumbers = [4,6]
const nums = [0,1,2,3,4,5,6,7,8,9]
function replaceWithZero(nums){
for (let i = 0 ; i < nums.length; i++){
if (bannedNumbers.includes(nums[i])){
nums[i] = 0
}
}
}
replaceWithZero(nums)
console.log("numbers are : ", nums)
here the function replaceWithZero does two things that make it impure. First it modifies its parameter. This can lead to issues, for example if you have Second it uses a non-constant variable outside of its scope (bannedNumbers). Which is bad because if somewhere else in the code someone changes bannedNumbers the behavior of the function changes.
A proper pure implementation could look something like this :
const nums = [0,1,2,3,4,5,6,7,8,9]
function repalceWithZero(nums){
const bannedNumbers = [4,6]
const result = []
for(const num of nums){
result.push(bannedNumbers.includes(num) ? 0 : num)
}
return result
}
const replaced = replaceWithZero(nums)
console.log("numbers are : ", replaced)
Here we are not modifying anything outside of the function’s scope or its parameters. This means that no matter where, when and how often we call this function it will always behave the same when given the same inputs! This is the whole goal of pure functions.
Obviously in practice can’t make everything 100% pure, for example when making a HTTP request you are always dependent on external factors. But you can try to minimize external factors by making the HTTP request, and the running the result only through pure functions.
For new code I’m writing I’m using mostly JsDoc function headers on public methods of classes and exported functions. With one or two sentences explaining what function does.
Also try to explain what to expect in edge cases, like when you pass am empty string, null, … stuff of that nature - for which I then create unit tests.
I also always mention if a function is pure or not or if a method changes the state of its object. On a sidenote I find it odd that almost no language has a keyword for pure functions or readonly methods.
If I add a big new chunk of code that spans multiple files but is somewhat closed off, I create a md file explaining the big picture. For example I recently added my own closed off library to my angular frontend that handles websocket stuff like subscribing, unsubscribing, buffering, pausing,… for which a created a md file explaining it.
I don’t know if these are uncommon but I have a few cool usecases besides the regular 1:1 folder syncing, maybe someone else finds them useful.
Also you should know that the way I have all of this setup is that I have a container that hosts a bunch of SMB Network drives and a syncthing container that stores all of the fodlers on that drive. Having them also easily accessible through smb is great when I just wanna quickly copy something or back the folders up.
So here are some of my maybe unorthodoz usecases :
I also used to have a setup that would sync Minecraft Bedrock and Stardew Valley saves between devices (where Windows and Android saves are compatible) but Android 11 introduced a stupid restriction that prevents synching from accessing the the saves are located on Android.
not to mention that you have all the dotnet ecosystem at your fingertips. Wanna write a WPF application? Go ahead. You want to use ML.net? A bit clunky but doable.
My introduction or what got me intrested was Farming Simulator 2009 when I was 10.
The game had amazing mod support and I found out that each mod, like a new tractor, had a modDesc.xml file in it that contained values like in-game price, name of the tractor, fuel tank volume, … .And I found out that changing these values would change these values in game, which made me feel like an absolute hackerman.
This isn’t any great C++ low level optimization but I have an Angular Project which contains a list that shows the live logs of various backend services and other things comming in through websockets (I keep up to 500K lines in memory). Essentially a extremly long scrollable list. First I obviously used lazy list which only renders the elements in view that are visibile instead of one that renders everything all the time. Secondly I removed as many html elements from each list element as possible, mostly divs that were just used to apply a css class.
When it comes to JS code I got a huge performance improvement by replacing a bunch of pure array.map, array.filter, array.toSorted, … method calls with a big ol for loop. These pure functions caused an insane memory churn because they create a shallow copy each time and doing that for an array with 500K entries caused the garbage collector to work overtime. Also the whole filtering and searching in the first draft was done every time new elements were added to the list via websocket for the whole entire list. I changed that so it only does it for new elements.
Overall this allowed me to increase the number of logs kept in memory from about 20K (because then performance would start to dip) to now 500K.
I always added the “server” as an introducer but for some reason the server itsself gets a prompt to add all the folders from the client but the client doesn’t get a list of all the folders the server is sharing which is what I’m after. The introducer does however work by directly giving me a list of all the other devices the “server” has been connected to. I’m using syncthing version 1.20.1 on my server btw.
She was a lua girl, he was every other programming language guy. It was not ment to happen.