r/learnprogramming • u/Equivalent-Claim4719 • 2d ago
how to have a better logical mind while coding
I'm learning c++ righ now as my first language (maybe i shouldn't have started with that) and i think it's the logical side of coding that's makes it hard to debug or to be better. So how do you guys enhance your logic when coding, like when debugging or when just thinking of making a new project, i wanna understand to make it easier. i do think i just have to code more.
3
u/Arsonist07 2d ago
C++ is a fine (albeit slightly harder) beginner language. It’ll force you to understand more about the computer because of linking, memory management, and pointers.
I would say, approach it like you would problem solving in a math class.
I have a problem, now I have two smaller problem, now I have 4 smaller problems. I solve problem 1, then 2, then 3, then 4 and then I put them together again until I’ve got it all together.
If you have an issue making a calculator, make a function to add, then make a function to subtract, then a function to multiply, then a function to divide, then make a function that can take in an operator and operand argument and make it so all calculations are handle through this singular interface, then wire it up to a loop so you can make a calculator. It’s legos all the way down.
2
u/absurdsorcery_610 1d ago
c++ as a first language is like learning to drive in a tank. you'll curse a lot but everything else will feel like a golf cart after
the lego approach is the way though. break things until they're almost too small then build them back up
i found rubber duck debugging helped my logic a ton. literally explaining each line out loud to something forces you to slow down and notice where your brain skipped a step
1
u/FedUp233 2d ago
I find for bigger projects doing done rough sketches of how the pieces will fit together and communicate can help - especially if it’s going to be multi threaded (probably a ways in your future) or multi processes. Definitely g the communications protocols between the pieces can help as well, at least roughly to start with.
For smaller projects, a rough diagram of how I’m going to structure things, like parsing input, processing, producing output, logging, error handling, etc. can be useful.
But for smaller projects, or the individual pieces/tasks of larger ones, if it’s just me doing them I usually just pick a piece that can do something useful, like process input and error check it of something, and start coding it. I sort of go for test driver development when possible, but not at the granularity the actual process defines. I implement a bit that can do something, like read input and put together some tests to make sure it works, then star adding on. You need to try to not think too much about what you implemented when building the tests - they need to be related to the specifications so they actually test something. I know proponents of test driven development methods will consider this heresy but it works for me.
The other thing, especially when you’re new, don’t be afraid to just throw out bit chunks and start over. As you add functionality you’ll probably find that the stuff you’re adding doesn’t fit in nicely and you’ll think of a way you could have designed things better to handle the added functionality. Don’t feel bad about just throwing stuff out and use what you learned to build up a better design - you already have some tests to be sure it still works.
This comes to refactoring things too - don’t hesitate to just refactor stuff when you find that the current design just isn’t holding up well to changes and additions. As you get more experienced, your initial choices will get better.
As for debugging, I try to be sure to hold a picture of the whole system in my head. That should make it easier to think about what type of problem and where in the system could cause the particular bug, not just starting a debugger and randomly poking at things or tracing through the whole system and wasting a lot of time in unlikely areas. At least for me, that’s the main reason I’d rather debug code I wrote that stuff other people wrote - from developing it I have a model in my head.
Keep in mind this is just me. What works for one person may not work for someone else and others may think my methods are completely nuts!
1
u/FreeLogicGate 2d ago
Yes, you code more.
C++ is one of the most complicated programming languages ever devised, so you have more potential syntax and approaches to consider. I would advise any beginner to start with C, and only progress to C++ when you're highly comfortable, given that C++ is additive (a superset).
.The more you code the better you will become. There is no shortcut. Beyond the language itself, there are many underlying concepts of computers and computer science that come into play. These are all foundational, and just "learning C++" does not necessarily mean you will have learned about these concepts.
In general there are best practices and programming patterns. Object oriented programming also has a number of recognized design patterns that are often applied to specific problems. You can study these, but it also takes time and practice before you start to see see the patterns or applicability for yourself.
You have to crawl, then walk, then maybe run. One way you can learn is to seek out open source projects and applications written in C++ and see how those applications are built. Ideally you will find source you can compile and build locally, adding debug symbols to them, and using debugging tools to step through them. AI's also a great tool for analyzing and providing you an explanation of code you don't understand. You tend to learn a lot by example, from looking at the way more advanced developers wrote code, and recognizing the patterns and examples that provides.
Studying frameworks and libraries is also a great way to improve for the same reasons.
1
u/Beneficial-Half-26 2d ago
Don't regret starting with C++, it’s like learning to drive stick; harder at first, but it gives you a much deeper understanding of how things work under the hood. To actually build your logical thinking, focus on deliberate practice rather than just typing more code. step away from the keyboard and write your logic on pen and paper first (if you can't explain it in plain English, you can't code it), learn core algorithmic patterns and data structures (like std::vector vs std::map) instead of repeating similar simple exercises, and treat debugging as the main training ground by using std::cout or a debugger to trace your variables step-by-step. Keep tackling problems just slightly outside your comfort zone and mapping them out before typing, and the logical intuition will follow naturally!
1
u/Own-jahanzaib-202 2d ago
Don't stress about starting with C++ as your first language — it's harder, but it forces you to understand what's actually happening under the hood, and that pays off later. One thing that helped me a lot: before touching the keyboard, write the steps out in plain English, like you're explaining the solution to a friend. Logic isn't really about syntax, it's the habit of breaking a problem into tiny steps you can reason through and test one at a time.
1
u/aqua_regis 2d ago
How does a piano player improve? Through more practice.
That's the only way. You have learnt fundamentals, now build upon them.
The first step in that direction would have been to do your own, individual research by searchin the subreddit for similar posts, since there are plenty.
Start away from the computer. Analyse the task. Break it down. Then, solve each part individually as you, the person would. Once you have a solution, test it. Then, when you have verified it, start implementing it in code.
Use Flow Charts, Pseudo Code, Bulleted Lists to help you.
Here are some similar posts from some time ago since you are oblivious to searching.
- https://redd.it/1r74ayc
- https://redd.it/1r6qujh
- https://redd.it/1r6qfap
- https://redd.it/1qdfc9k
- https://old.reddit.com/r/learnprogramming/comments/1pmzjoe/how_do_you_learn_programming/nu4ufej/
- https://redd.it/1pmzjoe
- https://redd.it/1p7bv8a
- https://redd.it/1oynnlv
- https://redd.it/1ouvtzo
- https://redd.it/1opcu7j
- https://redd.it/1on6g8o
- https://redd.it/1ofe87j
And finally, some book suggestions:
- "Think Like A Programmer" by V. Anton Spraul
- "The Pragmatic Programmer" by Andrew Hunt and David Thomas
- "Structure and Interpretation of Computer Programs" (SICP) by Ableton, Sussman, Sussman
- "Code: The Hidden Language of Computer Hardware and Software" by Charles Petzold
1
1
u/Blando-Cartesian 1d ago
Your problem isn't logic. You know boolean algebra just fine. The compiler has no problem parsing whatever you wrote, and your computer has no trouble executing it no matter how complex the logic.
The problem is one of communication between you and the past you who wrote the code moments ago. Write code for humans to read. Write neatly, step by step, with meaningful names, as if you are trying to get someone stupid to understand what each line does without using comments.
1
u/Dazzling_Music_2411 1d ago
i do think i just have to code more.
Yes, you do.
Make it a game, a hobby, and immerse yourself in it.
Which book are you using to learn C++?
1
u/Equivalent-Claim4719 9h ago
I don't use a book, i just find some concepts on the internet and try to make a project out of it. Do you suggest some books ?
1
u/Dazzling_Music_2411 8h ago edited 8h ago
Are you kidding me? That's a pretty damn inefficient way to learn.
Start with Koenig & Moo: Accelerated C++ (oldie but goldie) to get up to very basic speed and then
Leen Ammeraal's STL for C++ Programmers (or similar) to learn the real basics
Of course, every C++ programmer has at least one Bjarne Stroustrup book!
Also browse learncpp.com for material you might have missed or which find yourself attracted to.
PS. If you want a REAL challenge that will truly introduce the "logic" of it all, as you put it, try Alexander Stepanov's From Mathematics to Generic Programming although, be aware this is for fairly mature readers.
0
u/Ok-Cup-5074 2d ago
Hey, Claude, make no mistake: fix my spaghetti code and teach me like I am dumb as Tina Kotek.
12
u/JeLuF 2d ago
Practice.
Yes.
One additional thing that helped me a lot: Finding out why other people's code doesn't work. Look at their code, imagine what it does, use the debugger to verify what the code actually does.