You probably answered "No" because I just thought of it 😇
Everyone knows they should be writing semantic HTML but why not apply the same standards to a "real" programming language.
Semantic programming is all about using the constructs a language provides in such a way as to convey intent to the target audience, whether that be human or machine. In HTML we have the classic example of using the <footer> tag instead of just spamming <div> everywhere. Lets apply that logic to Javascript.
Here's some non-semantic JS.
1
ah, a for loop, I wonder what it does.
2
I have to look inside the loop to find out that we are transforming each element.
JS
let arr =[1,2,3]
1for(let i =0; i < arr.length; i++){
arr[i]=2arr[i] * 2
}
1
ah, a for loop, I wonder what it does.
2
I have to look inside the loop to find out that we are transforming each element.
now for something semantic
1
This one word tells me that we are transforming each element somehow, without me having to read any further!
JS
// we are doing the exact same thing as before, just better.
let arr =[1,2,3]
arr = arr.1map((x)=> x *2)
1
This one word tells me that we are transforming each element somehow, without me having to read any further!
I think you get the idea...
But just in case, here is another scenario...
1
Not sure what this while loop is going to do
JS
1while(i < arr.length){
...
// do some operation
res =...
}
1
Not sure what this while loop is going to do
Compare the above to this:
1
This word means we are going to combine the array in some way.
JS
let res = arr.1reduce((acc, cur)=>{
...
// do some operation
},{})
1
This word means we are going to combine the array in some way.
So.. what do you think?
Ok, maybe this is just an excuse to get you to do some functional programming.