Quick Lessons in Optimization
After finally getting the first regex version of the WhatsApp Chatlog Interpreter (WCI) to work, I can now finally start working on the perfectionism stage.
Optimizing Code
Optimizing code is always important not only because it makes the program itself better, but it also makes you used to good coding habits.
I was revising the WCI's code, and stumbled upon these lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | function botonejecutar1() { //CSS COLORES botonusuario1.style.backgroundColor = "#005c4b"; botonusuario1.style.color = "white"; botonusuario2.style.backgroundColor = "#202c33"; botonusuario2.style.color = "white"; //CAMBIANDO DE LUGARES LOS MENSAJES for(var i = 0; i<=mensajes_usuario1.length-1; i++) { mensajes_usuario1[i].classList.add("my_msg"); mensajes_usuario1[i].classList.remove("friend_msg"); } for(var i = 0; i<=mensajes_usuario2.length-1; i++) { mensajes_usuario2[i].classList.add("friend_msg"); mensajes_usuario2[i].classList.remove("my_msg"); } } function botonejecutar2() { //CSS COLORES botonusuario1.style.backgroundColor = "#202c33"; botonusuario1.style.color = "white"; botonusuario2.style.backgroundColor = "#005c4b"; botonusuario2.style.color = "white"; //CAMBIANDO DE LUGARES LOS MENSAJES for(var i = 0; i<=mensajes_usuario1.length-1; i++) { mensajes_usuario1[i].classList.add("friend_msg"); mensajes_usuario1[i].classList.remove("my_msg"); } for(var i = 0; i<=mensajes_usuario2.length-1; i++) { mensajes_usuario2[i].classList.add("my_msg"); mensajes_usuario2[i].classList.remove("friend_msg"); } } |
So, we have two functions, botonejecutar1(), and botonejecutar2(). But this is trash.
If you look closely, these two functions do almost the exact same job; by rule of thumb, if you have a repetitive task in your program, you can almost always find a way to make it work with an automated function. Which is what we're going to do now.
First, I'm going to add arguments to the function. We can see that in both functions, the only lines that change are "my_msg", "friend_msg" (which represent the users), "#005c4b" and "#202c33" (these are the message's colors).
So we create a function with the following arguments:
1 | function botonejecutar(user1, user2, color1, color2); |
We then replace the lines that change with those arguments, and the final function looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function botonejecutar(user1, user2, color1, color2) { //CSS COLORES botonusuario1.style.backgroundColor = color1; botonusuario1.style.color = "white"; botonusuario2.style.backgroundColor = color2; botonusuario2.style.color = "white"; //CAMBIANDO DE LUGARES LOS MENSAJES for(var i = 0; i<=mensajes_usuario1.length-1; i++) { mensajes_usuario1[i].classList.add(user1); mensajes_usuario1[i].classList.remove(user2); } for(var i = 0; i<=mensajes_usuario2.length-1; i++) { mensajes_usuario2[i].classList.add(user2); mensajes_usuario2[i].classList.remove(user1); } } |
Calling the new functions in the HTML file
Now, in the HTML file, in the "<button>" tags, we insert the functions in the "onclick" properties of the tags, like this:
1 2 | onclick="botonejecutar("my_msg", "friend_msg","#005c4b", "#202c33")" onclick="botonejecutar("friend_msg", "my_msg", "#202c33", "#005c4b")" |
But, this is a problem. When calling the botonejecutar() function, we use the " " symbols, and when we insert the arguments in the form of a string with those same symbols, the HTML code gets confused and throws an error. (Therefore the red color).
This has a simple solution. What we have to do, is use different symbols for string declaring, like ' '.
So, the new code looks like this:
1 2 | onclick='botonejecutar("my_msg", "friend_msg", "#005c4b", "#202c33")' onclick='botonejecutar("friend_msg", "my_msg", "#202c33", "#005c4b")' |
Now, everything works, and we managed to delete a few repetitive lines of code, optimizing it.
Thanks for reading!
Comments
Post a Comment