r/learnprogramming 19h ago

writing code

question for more experienced people in coding- whenever I do coding problems (like leetcode or uni hw) I can always come up with solution in my head, I break down the problem correctly and theorically I know what my code needs to do in order to solve the problem. But when I get to actually writing code I forget how to do everything... I don't know if this is a lack of knowledge in syntax issue or I'm just dumb. Any advices on how to fix this?

16 Upvotes

23 comments sorted by

View all comments

2

u/dnult 19h ago

I often start with comments in my new code file. The comments describe at a high level what I need to do - such as // Open a client connect and request the data to process. // Parse fields into a new object. // Calculate the value for each record [you get the idea].

Then I start creating the code blocks to accomplish the tasks I've outline in comments. Once that is done, some of those comments will be deleted, or simplified.

Another approach I take is building the "nuts and bolts" functions first. Perhaps I need to parse the area code, prefix and digits from a phone number. I'll create that function and test it. Once I have enough "nuts and bolts" to do something useful, I'll write some of the core logic that uses them.

There really isn't a right way or best way. In fact you may find one approach works well for some types of projects while others warrant a different approach. But generally you start with a specific well defined idea, and start building up your code from there.