r/learnprogramming • • 10h ago

Tutorial How can I become good at problem solving?

After getting a task some programmers just starting writing the code, while writing the code they understands all the logic behind that...how can i achieve that level of skill?

6 Upvotes

15 comments sorted by

3

u/grantrules 10h ago

How do people become good at playing piano? How do people become good at hitting a baseball? How do people become good at tapdancing? How do people become good at writing poetry?

Practice.

https://adventofcode.com/

0

u/astronautfromuranus 9h ago

Can you tell me what is this website? First time seeing it

2

u/aanzeijar 9h ago

It's a yearly advent calendar of small coding challenges wrapped into a little framing story about how the elves screwed up/try to save Christmas. Every day until the 12th there will be a new coding challenge, with an additional second challenge once you complete the first one. It used to go until the 24th, but the creator has stated that creating the problems takes a lot of time, so he only does 12 nowadays.

You don't need to register, but you do need to log in with one of the provided auth providers (reddit, github and google should be enough for most) because everyone gets randomised data sets to work on.

You can jump straight to the first challenge of this year here. It's on the easier side, so even beginners should be able to solve it.

2

u/SnugglyCoderGuy 10h ago

Practice.

The number one rule of problem solving is if you can't solve the problem in front of you then break it up into smaller problems. Keep breaking the problems up until you do get a problem you can solve, then solve it. Do this recursively and eventually you solve enough problems that you've solved your original problem.

0

u/astronautfromuranus 10h ago

Thanks for your advice...I'll definitely try your method!

1

u/SnugglyCoderGuy 9h ago

(this all got away from me into a stream of consciousness going way out into left field lol. New ADHD medication ftw. I think its useful information though so have at it, or don't. Up to you)

Corollary to this is that you've got to really think in a very fine grained and detailed step-by-step method which doesn't come naturally to most people. It's the difference between top-down and bottom-up thinking.

The vast majority of people are top-down thinkers primarily and by default, like 97% of people. Autistic folks like myself are primarily and by default bottom-up thinkers.

Top-down thinking (often favored by non-autistic people): Starts with the big picture, expectations, past experience, or general concepts, then filters or checks details against that framework. It helps people move fast and rely on intuition or shortcuts, though it can miss small details or bring in biases.

Bottom-up thinking (often a stronger default for many autistic people): Starts with raw details, sensory input, and individual pieces of data, building up toward a bigger concept over time. It takes more time and energy, yet it can catch subtle details and patterns that get filtered out early.

Both of these are important things to have when writing programs because you need to know what you're building and you also need to know precisely how it should work because you've got to tell the computer precisely how it works.

What I often do is start big picture "this is what I want the program to do" and then start to go into "What's core thing that needs to happen?" "What do I need to make the core thing to it's thing?" "What do I need to make the supporting things to do their thing?" etc etc etc. And this is all just boxes and arrows on a piece of paper in real language like I'm describing this to a human (because I am, me). This gives a very high level view of what the process flow will be like.

Then I start to identify the packages I'll need based on that (I program in Go). Usually a database package for database stuff, a logging package to handle logging things. An errs package for custom error types. A clock package for handling clock things (I always inject a clock mechanism into my objects that need time instead of just using the clock directly in my code. If anything reaches out of my core part of the application then it is done via a polymorphic interface, which in Go is simply the interface type. Whatever language you have will have something you can use, even C (a struct with function pointers) By accessing the clock via an interface I can then inject my timewarper package https://github.com/SnugglyCoder/timewarper/ and control how fast or slow time moves, or even jump forward and backward. Still a WIP).

Then I start to define the stuff in my core application package, the business logic of it all. Business logic is usually the term used to refer to the stuff your application does that gives it its entire purpose for being. Everything else is just ancillary necessities to fulfill that goal and to operate it effectively. It's all just stubs. Function to create an instance of the thing to run or perform the business logic when called. Types to fill in the spaces where I'll need custom structs. The interfaces it will need to do the thing, all of which are written from the business logic's point of view.

Then I'll start to define the ancillary packages as needed to create instances of those things, like a database client wrapper that defines the methods defined by the interfaces in the core business logic package. Your core business logic packages should never inherit from the ancillary and supporting packages, the ancillary and supporting packages always inherit from the core business logic package.

The connections all flow from the original pencil and paper diagram I drew earlier. Within each function you think very detailed and step by step "What's the first thing that should happen? (stub that function in).", "What's the next thing that should happen? (stub that function in).", etc etc until I get to "And now this function should return". Rinse and repeat that until the answer to "What should happen next?" can't be described in a few words which means it must now be described in code so the computer can actually do some thing.

The names of your functions should serve as your "Why does the code in this function exist?", the code in your function serves as "What does this function do?". If you find yourself needing to put comments, that's a smell that you've written bad code and should reexamine the words you're using. If statements are a great opportunity for improving clarity by adding some extra lines simply for the readability of it all. Consider the following:

if e.Status == "T" || e.Status == "P" {

This isn't clear at all as to what is going on. You would have to spend quite some time trying to figure out what is going on and what it means exactly, in a real world business sense. However, with 2 extra lines it becomes a hell of a lot clearer what is going on and much more natural to read and tells a lot of information right on the face of it.

employeeIsTerminated := employee.Status == "T"
employeeIsOnLeaveOfAbsence := employee.Status == "P"
if employeeIsTerminated || employeeIsOnLeaveOfAbsance {

Now we see that employees are considered terminated when their Status field has the value "T" (which we could further clean up by using a const like `const EmployeeTerminatedStatus = "T"), and we can see that employees with a status of "P" are on a leave of absence. And we can write the if condition in a way that reads like regular ol' English which seems like a big whoop now, but when you go to speak about it in the future it is sooo much easier to link back into the business context. I've been able to copy and past sections of code to people who don't really know how to code and be able to grok what is going on in those sections.

Time's up, thanks for coming to my Ted Talk. I can always continue further if desired.

3

u/JohnsTech_dev 10h ago

a lot of those people who just start typing are rewriting stuff they've already done 20 times, so the logic looks free to them. it isnt really. what helped me was writing the input and the expected output of the function as a comment first, then one tiny example by hand on paper. if i cant do the example by hand i have no business writing the code yet. after a few months you start seeing the same shapes, like two pointers or a lookup table, and it gets quicker

1

u/boundlessrepayment 9h ago

I still keep a notebook of solved patterns, tbh half the time I'm just pattern-matching from that

1

u/BrannyBee 9h ago edited 9h ago

a lot of those people who just start typing are rewriting stuff they've already done 20 times 20,000 times

Everyrhing you said is is more or less accurate and continues to apply outside of the learning environment and into the real world. Coding is hard for beginners and they spend like 90% of their effort on writing the code, which makes it hard to solve problems cause they're only using 10% if their brains. Then they might learn and relearn the same thing 20 times to get it down pat and ace the test, and even look like amazing programmers to other beginners because they got a good grade and code "fast" now that they've learned the material enough to devote 60% of their effort towards the code and now have 4x more "brain power" available to work through the logic

Where people plateau and become expert beginners who cant struggle outisde school, is they dont realize that experienced devs are devoting like 95% of their effort towards the logic and the code is just the easy (and fun) part that is almost automatic. You start to notice patterns like you said pretty quick even at 20 repetitions, but once you've written many many many more repetitions of similar pieces of code, thats when you're cooking with gas.

A big reason for that is the amount of times you fuck up, experienced devs arent smarter than you because just because they've written more loops than you. They've successfully written a loop a million more times AND they've written absolute garbage that doesn't even look like a loop or work properly just as many times lol

You'll see some immaculate code from beginners that have drilled the same labs and exercises over and over again, and they can whip put todo apps like its no ones business... but the second something weird happens or a test has code written in a slightly different way, they struggle. If a loop stops working, the guy who can write perfect loops isnt really the goto man most of the time. You want the idiot like me who has seen every possible way an idiot could mess something up with a loop.

A lot of A+ students don't get nearly enough practice debugging garbage that is common in the real world, which is a shame because its great for learning and if you are the one who wrote the garbage in the first place, you get double the practice fixing it.

Idiots like me are able to swing way above our weightclass because good students and smart people dont put in the reps or have realized coding is easy. Im dumber than a lot of people... but Ive done so many reps that writing or reading code is just typing at this point. I cant compare to anyone actually smart... but if half their time and brain is focussed on code... well I look like Im smart in comparison cause I get to use my entire dumb brain on logic and reasoning. OP you gotta put in a million reps, don't get baited by being intelligent, or you'll get passed by idiots who are just more efficient

1

u/LifeguardImportant44 9h ago

Half the time they already wrote that exact loop fifty times before so it looks like magic from the outside

1

u/EszterSantos72 9h ago

Write code for at least 50 small problems before expecting it to click, because that's when you start seeing patterns repeat. Early on, force yourself to write out your logic in comments first so you don't get lost mid-solution.

1

u/Ahmed_Hisham0 7h ago

they can't do that because they're smarter. they've just seen that shape of problem before, so the first draft is already roughly right.

the way you get there is by writing the steps in plain words before touching the keyboard. like "read the file, keep a running total, skip blank lines". if you can't write the steps, you don't know the problem yet and typing will just be guessing.

also, start smaller than feels useful. ten tiny problems teaches more than one big one you fight for a week.

1

u/aqua_regis 7h ago

By learning to first do your own, individual research, as in just typing "problem solving" into reddit's search box.

Also, practice, practice, practice, practice, and more practice.

You need to solve problems to train your problem solving skills. Tutorials are not going to teach you such skills.

Below are some of similar posts (where the posters usually proved way more effort than you did):

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

Last, a great practice site: https://exercism.org

1

u/Jackson999Jc 6h ago

honestly the thing that helped me most was writing the plan in plain english comments before any code. like 4-5 lines of "first this, then that, edge case here". the people who just start typing usually have done that exact kind of problem 50 times so the plan lives in their head. you get there by solving lots of small problems and then asking yourself what pattern it was after, not just whether it passed.

1

u/Downtown_Savings_730 6h ago

Keep on solving problems, ultimately problem solving boils down to pattern recognition and those who can figure out what pattern to use can solve problems easily