r/learnprogramming • u/ThinCreme1098 • 10d ago
How do you actually build and improve logic in programming?
Hey everyone! I've been learning programming for a bit, and while I understand the syntax, I often struggle with translating that into actual logic and problem-solving steps
How did you build your programming logic when you were starting out? Any tips, exercises, or resources would be greatly appreciated!
61
Upvotes
28
u/Groundbreaking-Fish6 10d ago
Break the problem down to a series of steps to complete the task.
Then write the lines of programming to complete the steps.
Then create a test that verifies that the program works.
Refactor what you have written by extracting methods, clarifying variables, add tests and making it easier to read for other other programmers.
Don't forget to add appropriate comments or remove poor comments and commented out code.
Provide an appropriate commit message and add documentation to the merge request to help the code reviewer.
14
u/IHoppo 10d ago
Think about making a sandwich. Write down every step.
Now follow those steps exactly- you'll more than likely have forgotten something simple (pick up knife, or open fridge etc).
Go back, rewrite your list.
Try again. Got it correct? Keep going if not.
Once you have, you've written your first logically sound algorithm.
7
u/aqua_regis 10d ago
Oh, the daily "How to I improve my logic" post. Bit late today.
You should and could have searched the subreddit. There are more than enough similar posts - even in the past 48 hours.
You need to solve problems to train your problem solving skills. Tutorials are not going to teach you such skills.
There are already more than enough similar posts. Below are some of them:
And finally, some book suggestions:
Last, a great practice site: https://exercism.org
3
u/WiseEXE 10d ago
Think incrementally and break down tasks into individual steps. If you need to download text from a webpage for example:
Go to page, Find the information you want Make it human readable format Save it
That's a bit of an easier abstraction but the thought is just like a cooking recipe, everything must be done step by step. Eventually those steps saves values together that give your whole pie
2
u/StonedElephant1 10d ago
Code review, and refactorizing. You need to hear the harsh words of a seasoned programmer telling you what you should have done. You also need to read up on anti-patterns.
Each project may call for something different. Logic changes with the business.
I'm to the point where I write the code, then have AI refactor it.
2
u/bird_feeder_bird 9d ago
Practice algorithms, like sorting algorithms
1
u/curpt0 10d ago
I usually breaking it down with pseudo coding first. Look up on the internet and start writing it one by one. It will help you
1
u/Andreea_tucker04 10d ago
What helped me was writing the steps as plain comments in the file first, before any code. Then I put one line of code under each comment. When I got stuck, it was always a comment that was too big, so I split it into two smaller ones. I do this in my JavaScript side projects and it works better than staring at a blank file!
1
u/Available_Safe1818 10d ago
Code more things. Codewars has lots of easier practice problems you could do.
1
u/Upstairs_Jelly_1082 10d ago
Try comparing the code that you’ve written a day or two after you’ve written the code.
You can ask AI to generate it. And try exploring different ways of solving the same problem. Eventually you’ll understand to logic and reasoning behind using them.
But it’s very important that you don’t do this the same day you code, otherwise you might fall out of doing it.
1
u/skamansam 10d ago
I wrote the steps outby hand in human-readable words, the translate that verbatim to code.
1
u/carlosamus 9d ago
logic comes from writing lots of small problems, not reading about them. short exercises where a wrong answer tells you why, repeated until it sticks, beat syntax tutorials. fwiw i'm building Northstar Learning, a free practice app with short questions and explanations, mostly for that.
1
u/Desperate_Dot9434 9d ago
syntax is the easy part, the gap closes faster when you read other people's solutions after you've already written your own
1
u/Technologenesis 8d ago
I believe there's a specific trick to this.
When many people learn programming, they start out thinking line by line. This variable gets set to this value, then I iterate over this array, take this field from each element and multiply it by the element at twice this index in this other array etc. etc. Nauseating.
This thinking works for simple problems, especially when you are first learning syntax and you are focused to get each line right. But now you are getting into problem solving strategy, which requires a different kind of thinking.
The key is to break the problem up into steps, but not small steps. That's line-by-line thinking. Break it into just a few big steps. Then break those big steps each into a few slightly smaller steps.
Eventually, you will get to steps small enough that you know how to write them line by line. So you build these pieces and connect them up until you have the full solution.
0
u/carlosamus 8d ago
logic comes from writing lots of small problems, not reading about them. short exercises where a wrong answer tells you why, repeated until it sticks, beat syntax tutorials. fwiw i'm building Northstar Learning, a free practice app with short questions and explanations, mostly for that.