Do you prefer library functions or writing them yourself? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Do you prefer library functions or writing them yourself?

Sometimes library functions don't exactly do what you need. Sometimes they're tricky or undocumented, sometimes they don't fit your data types. How comfortable are you in writing your functions?

5th Jul 2019, 6:09 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
6 Answers
+ 8
Lol didn't noticed! It is essentially the same thing. Upvoted 👍
5th Jul 2019, 6:40 PM
AZTECCO
AZTECCO - avatar
+ 7
Yeah, library function are not always to do your task.... They are just used to make your task easy. For eg, Let's make a function to find sum of all numbers of array! function sumarr(arr){ sum=0 for(i=0;i<arr.length;i++){ sum+=arr[i]; } return sum; } The above was a user-defined way to find a sum of all numbers of array. Let's try library function function sumarr (arr){ return arr.reduce((v, c)=>v+c); } See! How easily we found the sum of numbers and we made a pure function too! This is good for performance of client But sometimes, library function are not enough, you need to define yourself. But it's true that, Library functions have made our coding easy and safe
5th Jul 2019, 6:20 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 7
Standard library got you covered most of the times, you can bet ;) For example to_string has a fixed precision of 6 digits, it's a standard. If you want to change the standard you need to #include <iomanip> and use setprecision() to set the precision of your stream. out << std::fixed << std::setprecision(x) << a_value ;
5th Jul 2019, 8:49 AM
AZTECCO
AZTECCO - avatar
+ 6
> How comfortable are you in writing your functions? As comfortable as my skills let me write. I use library functions when present. Else write one from scratch, do the necessary research when needed. Possibly also try other ways which are easier & simpler than my 1st few tries. If that can avoid writing that complex algorithm, all the better.
5th Jul 2019, 6:17 AM
Lord Krishna
Lord Krishna - avatar
+ 2
Yesterday I needed to google this: template <typename T> string to_string_with_precision(const T a_value, const int n = 6) { ostringstream out; out.precision(n); out << std::fixed << a_value; return out.str(); } C++ library to_string() function is really useful, but when converting doubles, it has a fixed 6 digits precision. This inspired me this post.
5th Jul 2019, 6:24 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
+ 2
AZTECCO I didn't need to output double, but to manipulate it as a string ☺️ Please check: https://code.sololearn.com/c7f6gonKZ2Lm/?ref=app
5th Jul 2019, 3:43 PM
Paolo De Nictolis
Paolo De Nictolis - avatar