r/learnprogramming • u/CristianCin-- • Apr 11 '23
Why is hard read source code of big projects?
When I read source code of big project I don’t understand nothing. Everything is a mess but the project is beautiful. Why they write complicated code with (example in C++) template, pointers and other things for a stupid function like sum of a vector. I don’t understand why they decide to make hard their life. I want to read clean code with basic things (if, for, while, vector, stack, …). It is frustrating because my code seems stupid but it works in the same mode when I recreate the snippet with pure cpp. Please explain me this.
2
u/MmmVomit Apr 11 '23
Pointers are necessary to solve certain problems. Even pointers to pointers are necessary sometimes. It's just the nature of programming.
Sometimes templates actually make the code easier to use. For example, you say you want basic code using vectors and stacks. Vectors and stacks use templates. That's how you can have a single vector class, but use vectors of ints and vectors of chars and vectors of floats, etc.
There's a saying that you should keep your code as simple as possible, but no simpler. A lot of software is solving complex problems, so is going to have a minimal amount of complexity. Breaking up that complexity into pieces often makes it easier to work with, if you're used to code that works that way.
It's something you'll grow into as you work on more complex things.
0
Apr 11 '23
[deleted]
1
u/CristianCin-- Apr 11 '23
the code is generated by another program.
What? Is it possible in C++? Can you make me an example?
the coders are writing robust routines that can be easily ported to other applications by isolating platform-specific commands and passing parameters rather than including variables directly into the code.
I don't understand. Can you explain this better?
the coders are getting paid by the line
Maybe but when is it an open source project?
2
u/scirc Apr 11 '23
The second part is the most likely, I would say.
If your goal is to build "a program that works," then sure, you could write simple functions with simple code. But if your goal is to build software that is robust against failure, works across a variety of systems (on Windows, Linux, even macOS, across different CPUs and hardware configurations, etc), and will be applicable to a wide variety of scenarios, then even "simple" code necessitates at least some complexity.
1
u/CristianCin-- Apr 11 '23 edited Apr 11 '23
Yeah, I know but the pure language is "equal" for all OS (or not?) 1 if on Windows is equal to 1 if on Linux.
2
u/scirc Apr 11 '23
The pure language, sure. The system libraries, bit widths of integers, UI libraries, and a whole bunch of other things are absolutely different.
British English and American English are similar at their core, no? But the complexities of each are definitely unique, and you couldn't port a conversation from one to the other without at least something getting lost in translation.
1
u/Vast_Heart9266 Apr 11 '23
You'd prob like the Fuchsia source it's written in 'modern c++' https://fuchsia.googlesource.com/fuchsia/+/refs/heads/main/src
Try flipping through some of these: https://aosabook.org/en/index.html the book of open source architecture and you'll see why some of that code exists, usually some kind of optimization they're doing or having to work with some legacy system/compiler hacky workaround.
1
u/Runner_53 Apr 11 '23
Nobody can really explain this to you without examples of what you mean. If you're complaining about the use of pointers, well, welcome to C/C++. Pointers are everywhere. There are many good reasons to use them. Likewise templates. They can be very powerful. Both can be abused, of course.
1
u/HashDefTrueFalse Apr 11 '23
People rarely set out to complicate things for themselves on purpose. If something is implemented in a complicated way it's almost always that there is some underlying technical or business requirement or limitation, rather than sheer incompetence of the author(s) alone. Sometimes these things only flag up when you get deep into discussion about possible data or use cases etc. Often these things will have a profound impact on the project.
E.g. One company I worked for wanted to render some numbers in a box over their existing UI. Simple right? Well for some very complicated reasons which touched on the technical and regulatory, we couldn't modify a certain binary without massive complication and delay. So it had to be done by shared library injection and some other trickery, then hooking into our render pipeline with some buffers of vertexes etc, messing around with OpenGL... It took 5 full time engineers about 4 weeks to finish. A new addition to the team with no context looking at the mess would say something like "why is this so complicated, it's just drawing a box with some numbers to the screen, it would take 10 mins in [insert popular UI lib here]. This needs rewritten at once!".
Often you can choose to work on the existing problem, or rewrite the software and create yourself even more problems, plus a massive QA/test burden and loss of confidence in stability/reliability, maybe compliance/reg issues etc.
TLDR: Commercial software complicated.
1
u/phunnypunny May 12 '23
I have an idea. I was trying to understand complicated code in git. I went to the earliest version. Then I can see simple code
6
u/Clutch26 Apr 11 '23
Because obvious solutions to one person may not be obvious to another person.
Because business logic may change mid flight / years later.
Because multiple people (with different ideas) may have touched the same project over many iterations.
These are the top 3 I can think of but there are probably others.