Delving into a fascinating world of programming can pose a challenge especially for the novice, whose initial enthusiasm can easily be quenched by an overwhelming number of things to be acquainted with.
In my journey to become a programmer I often strived hard pointlessly or reinvented the wheel which was due to a lack of knowledge and experience.
As there is no silver bullet for solving these kinds of problems, on DS Hacker you will find the three tips that I wish I had heard when I have started coding, which could have helped me to spare my nerves and save valuable time.
In this article, let’s start with the first piece of advice.
Finders keepers, losers weepers
It may seem strange but in the beginning, I desperately wanted to do all stuff on my own. Using someone else’s code was nothing but stealing in my mind and proof of my programming incompetence. In reality, when searching the Internet not only can you find a solution to your problem faster, but it also enables you to learn from people who have developed expertise in particular areas. You don’t need to worry about copying code from the Internet (on the condition that the source is legal and the author consents) as long as you fully understand the code and know that it will perfectly work for your problem.
The true gold mine of problem-solving answers is stackoverflow.com. Stack Overflow is a question-and-answer site for professional and enthusiast programmers, but you need to remember that much like Wikipedia, Stack Overflow “articles” can be edited by anyone. Hence you always have to ensure that the found solution is working properly in your case – as you use it on your responsibility.
Let’s say we want to find the closest element k to a number n in a vector x with R programming language.
i.e.: x = [-5, -3, -1, 3, 4, 7] n = 2 => k = 3
Because we just started programming in R we tried out different approaches and after some time we eventually came up with our piece of code:
k <- x[ abs(x – 2) == max( abs(x – 2)) ];
Instead of working our fingers to the bone, we could have typed “R find the nearest value in a vector” in the Google search engine and checked the first result – an article on StackOverflow. After short reading, everything shows that the author of the question is facing the same problem as we do, moreover they were lucky enough to get some answers.
Our new solutions:
k <- x[ which.min(abs(x – 2)) ];
k <- x[ which(abs(x – 2) == min(abs(x – 2))) ];
work correctly as the original one, so it may be worth comparing their execution times:
You may think that such a small difference in execution times is not worth all the trouble, but don’t forget that sometimes a single line of code can be executed even hundreds of thousands of times for different size vectors, which can lead to a significant decrease in efficiency.
Analyzing your code’s correctness and its efficiency in many cases is more important than writing your own (maybe less efficient or readable) code. In this example, we saved some time on searching for the solution that we could take advantage of when analyzing the solution.
It is also crucial to understand the found code and make sure it will work in any potential situation. Here it comes to the rescue the official documentation, which is a reliable source of knowledge that is easy to find for most of the modern programming languages.
In our case we should type:
To learn more about the function we are just about to use.
Now you know how to learn all the necessary information before using a function. As you can see many interesting counsels can turn out to be pertinent to our solution. The official documentation is your friend and commit it to your memory . Knowing programming language technical aspects can be helpful, but a good programmer should always be able to put things up together rather than remembering everything by heart.
Author: Paweł Golik
A third-year student of Computer Science at the Warsaw University of Technology. During his studies he also worked as a Junior Programmer. His interests include Data Science and Software Engineering To find out more, visit his profile on LinkedIn.