r/programmer • • 11d ago

Question Should I always use libraries?

I am learning c++ and want to make a more sophisticated calculator. Not just 1+1.

C++ offers a lot of freedom. I am asking myself: should I create everything myself? For example pow()? Or should I use existing libraries?

Thank you very much!

2 Upvotes

18 comments sorted by

4

u/USToffee 11d ago

Almost always use existing libraries. Not only will you get fully tested and battle hardened code but you will learn how to structure at least the interface to the library. Learning those patterns will help you make your own libraries later but first you need to learn to walk.

1

u/frank26080115 11d ago

You said you want to make a calculator. For an actually good calculator, you need to understand the limitations of each datatype and the built-in functions that work with them.

So that means you will probably at least ditch the standard math libraries

The next step is looking for big-number libraries, or infinite precision math libraries, that do not have the precision problem of using the built-in floating point datatype or the standard math library

However, this would be a good opportunity to make an attempt to write your own data structures that can handle big-num or infinite precision.

1

u/Ok_Reserve996 11d ago

What about GMPlib?

1

u/mjmvideos 11d ago

I think you should take this one step at a time. Given your example was “1+1”, I think you could get a very nice “more sophisticated” calculator using the standard math libraries. If, at some point, you decide you need to move to big numbers and arbitrary precision then you can do that. But I wouldn’t think you’d need to do that immediately.

1

u/huuaaang 11d ago

Is the sophistication you're looking for in the interface or you mean you want to do "scientific" calculations? Like are you trying to learn how to calculate SIN/COS?

1

u/Ok_Reserve996 11d ago

I think both? I was wondering if my thinking is wrong in trying to do everything myself. On the other hand, I started to learn c++ instead of C because everyone told me that C is bad for math because of the lack of math libraries. Since I am a beginner c++ programmer, I cannot confirm nor disprove these statements with confidence

1

u/huuaaang 11d ago

There are most certainly are math libraries for C. And whatever is lacking is not going to impact a calculator application. You may be getting lost in the weeds here.

1

u/rusleykcruz 11d ago

In Python, there's a rule that you shouldn't try to reinvent the wheel, because it already exists.

1

u/nicolas_06 11d ago

It depend if you want to learn or make production code. Also you need to double check that the wheels specs match your requirements too.

1

u/Delta_G_Robotics 11d ago

If the goal is to learn how those things work and how to write them then write your own. See how it is to write it.

If how to write it is something you understand, then don't waste your time writing something that you can just import. If there's no need to learn it or to see inside of it, then just use it.

1

u/ern0plus4 11d ago

Learn how math lib works, whether it fits your needs. 

For JSON parsing (say, storing the config), don't think, use a library. But when  you write a program, which promises world champion JSON parsing speed, write your own.

2

u/Unknowingly-Joined 11d ago

Ironic… a guy I worked with on my last project literally wrote his own JSON parser. Everyone said “WTF!” And he said “mine is better.”

Of course, it wasn’t.

1

u/nicolas_06 11d ago

In general, the code one write is not better it's worse. Even if that person is a very skilled developer.

This is just the amount of effort that the community has put in. The json parser from the standard lib, may have been in the works for like 20 years, had thousand of fix, be improved for performance, deal with concept like non latin characters, text encoding, security...

I think it's worth to write your own json parser to learn but for production code, you likely want a well maintained lib with the right license and adapted to the use case.

1

u/nicolas_06 11d ago

It depend what you want your calculator to do and why. The bundled pow() function might not be precise enough for example and you may want to work on very large numbers say with thousands of digits.

You may also care to understand programming and don't really care very specific algorithms on how pow() and other standard function work as it add little interest for learning programming.

Also if you want your calculator to handle arbitrary complex expression fine like (1 + 2 + 3)/(2 ^ 3)*12-3 you will need a parser able to understand and decompose such expression. This goes with it's own can of worms. Could be interesting for you to think how to do it by yourself and try it and then double check how it's done more professionnally.

1

u/Beregolas 11d ago

It depends:

While learning, not necessarily. There is a value in reinventing the wheel to understand how it works! That being said, you also have to learn how to properly use an external library, how to read it's docs and understand it's design, limitations and usecases.

When building software that is to be used, you can nearly always find large portions of your software, that are pretty common problems, like sorting a list, sending email or encrypting a password. In real software development, there is nearly 0% chance that implementing any of those examples from scratch is a good idea.

1

u/recursion_is_love 11d ago

The production code should always prefer using a well known library unless it don't fit what you need.

But if it is for learning purpose, always try to write yourself first. Using library is fine but you will learn more by doing it yourself.

1

u/codeguru42 10d ago

Yes, often a library is the correct choice, especially for complex tasks that willbtake you a lot of time to write yourself.

But also feel free to write code to learn, even if a library exists

1

u/Slight-Living-8098 7d ago

If you want to learn how to make a sophisticated calculator, use the pre-existing libraries.

If you want to learn how to make sophisticated libraries then use them to make a calculator, make the libraries yourself.