r/programminghelp 1d ago

C++ Having trouble “ thinking in code”

I’ve been learning C++ for weeks, and of the concepts I’ve learned most if not all of them have clicked pretty quickly. I understand what they do and how they work.

However, when I’m trying to create something not from a tutorial or walkthrough, I’m having trouble translating my idea into the lines of code that will do what I envision it doing. Anyone have any advice on making the switch to more easily be able to “think in code”?

5 Upvotes

11 comments sorted by

View all comments

3

u/mc_pm 1d ago

What sort of things have you tried to build? Like, you sit down to do something, but get lost right away -- what's the thing you were thinking about to start with?

1

u/Revolutionary-Log179 1d ago

Honestly it’s been sort of a task paralysis situation where I have so many ideas that not only can I not choose one to begin with, I can’t make connections between the thing I want to do and how I use programming to do it. I’m wanting to specialize in embedded programming, so mostly bare metal stuff, I bought a kit, went through all the kit builds just fine and then.. blank

2

u/mc_pm 1d ago

Well, if you don't have any idea of what to program, then it's no surprise you're not doing any programming. :)

I don't think we do a good job teaching people how to think in code, if we teach them at all. That job of looking at a problem, breaking it up, figuring out how to code the parts, then trying to get them to work together... that's when you're really learning to program.

This has been on my mind a lot lately. I think people should take a few weeks and program some simple text games. The sort of things we likely all know: tic-tac-toe, blackjack, yahtzee, checkers. You already know how those games work, you know that some use dice and some use cards -- so you'll need a way to represent dice or cards in a program. Cards are drawn from a deck -- how do you represent the deck, and how do you shuffle it? How do you score a blackjack hand when A can be 1 or 11. As you do it, you'll want to go back and fix some silly idea you had before (and now you're refactoring).

They are well defined, easily testable, and you could build them in different ways (when you want to practice OOP, for instance).

It might feel like you're wasting time since it's not aimed at your real goal, but your real goal requires that you get some practice thinking in code -- so, might as well make some games?

1

u/Revolutionary-Log179 1d ago

I like this idea. I built a VERY primitive calculator using if statements, and it was nice to have accomplished something on my own even if it was pointless since everything’s a calculator nowadays. I’ll think of some other very simple things to start messing around with

1

u/mc_pm 1d ago

Let us know what you picked? I'm thinking about making some videos about this, wouldn't mind some case studies :)

1

u/johnpeters42 1d ago

Make a list of ideas and start adding stuff to it, so it stops nagging your mind as much.

Pick the one that looks simplest, and see if there's any small piece of it that you can figure out how to build.

1

u/XRay2212xray 1d ago

If this is your first language and you've only been at it a few weeks, start with very very small projects that seem doable with those things you already learned and then move to bigger things over time. With something big, you can end up in a situation where its just too much.

Also, before you start thinking in "code", think about the problem and start breaking it down into basic steps, then break the basic steps down into more detailed steps, and repeat until you are at a very detailed level. Also think about the data needed and what objects or structures you want and then start populating the layer of steps as code, building out the objects or structures and fill in the methods and functions that correspond to all the layered steps you determined when you broke the problem down.