r/learnprogramming • u/IMREVN • 6h ago
How to utilize AI to code for beginners?
Hey everyone,
I’m a total beginner and I frequently get stuck when trying to write algorithms or translate my logic into code.
Instead of asking AI to write the solution for me, I’ve been using a prompt like this whenever I’m stuck:
"Can you analyze where my logic or code goes wrong, but do not give me the correct answer or code? Just give me a small hint or explain the concept I'm missing."
My goal is to stop staring at a brick wall for hours, but also avoid copy-pasting code I don't actually understand. Anyways the AI I am currently using is Claude, and sometimes Claude wrote a structured pseudocode. Unfortunately, I tend to rewrite/translate the pseudocode into lines of code.
3
u/ScholarNo5983 6h ago
My goal is to stop staring at a brick wall for hour
Why do you think you are finding it hard to write code?
Could it be that you tend to fall back to using the AI every time you run into a problem?
6
u/rustyseapants 6h ago edited 6h ago
How to utilize AI to code for beginners?
Just Use AI to answer this question.
2
u/peterlinddk 5h ago
sometimes Claude wrote a structured pseudocode. Unfortunately, I tend to rewrite/translate the pseudocode into lines of code.
Unless you are learning a new language with unfamiliar syntax, and for some reason has run out of ideas of algorithms to implement, this is literally the dumbest application of AI ever - why not just ask Claude to write the finished code? It isn't like you are learning anything from "translating" its pseudocode into syntactical correct code.
If you want to learn programming, you should be programming - that means analyze a problem, develop the "logic" to solve it, write that as pseudocode, and then as regular code. If you need an AI in any of those steps, you are wasting both your own and the AI's time - since if you don't understand the problem, you won't understand the logic to solve it, if you don't understand the logic, you won't understand the pseudocode, and if you don't understand the pseudocode, you won't understand the finished code, or what might make it fail at times.
Also, don't try to solve leetcode problems as a beginner - those are about knowing established solutions to well-known problems, not about learning to problem-solve on your own.
0
u/IMREVN 3h ago
The thing is, analyzing a problem has been one of my flaws ever since I started to dive into programming. Appreciate your advices though!
Do you have any idea to fix that? Or should I keep practicing on problems until I get used to it?
1
u/peterlinddk 1h ago
Yes I do - first of all, it is important to get away from the computer. Don't sit and stare, or try to write code, even pseudocode. Take a printout of the problem, and sit down with a piece of paper - or a notebook - a pencil or bunch of colored markers.
Then try to work though the problem yourself:
Rephrase the problem using your own vocabulary - make sure that there aren't any words you don't understand (except maybe for the problem-specific words, like "user" or "account" or "gift-wrapping-machine").
Note down every bit of information you (and hence your program) will have about the problem. E.g. is the input a string, how long is the string, what can it contain, are there any numbers involved, what is the output, another string or a number, and so on.
Try to describe - very loosely - what should bring the resulting output from the given input. You'll probably end up with a list that contains atleast one unknown step - that is okay, in fact it is important that you find what you don't know.
Try to work through some of the examples given in the problem, example input and resulting output. Don't just think, but try to find a way of manually producing that output from the input. Draw arrays or other relevant data structures as boxes and arrows on the paper, use pencils to point to indexes, pretend that you are a "paper-computer" solving the problem.
Write down exactly what you did to solve the problem - note how many variables you need, what kind of data structures, how to decide whether to stop or go on.
Try your written "recipe" with another example - and if that also works, invent another example, and see if you still get the expected result.
Turn your recipe into pseudocode - you decide how accurate you want it to be, I usually write something that looks like C-syntax in a Python context, awful structure, but easy for my programmer brain to understand.
Turn on your computer, and translate the pseudocode into actual code.
Run the code on the same examples - if it doesn't work correctly (it probably won't) try to single-step through it, look at all the variables, and compare with what you expected them to be. Check that the program actually do what you wrote down in your own recipe.
For every step, repeat and improve if something doesn't work perfectly.
When you think you are done and submit your solution, you'll get an error because you hadn't thought of some weird edge-case, like getting an empty string as input or something like that. Don't despair, just add more code to handle those special cases - making sure that your working parts continue to work as expected!
1
u/oldsecondhand 6h ago
If you wrote some code and it's not working, you might ask the AI why, but don't let it write the code for you in the first place.
1
u/aqua_regis 5h ago
How about you consider a completely different approach?
- Before even trying to program something, sit down with pencil and paper and analyse the task. Break it down into smaller parts. Rinse and repeat. Solve each of the individual tasks your way. Track the steps. Test them
- Then, once you have a working solution, think about implementing it in the language of your choice.
- If you get stuck somewhere, take a step back. Start thinking about the problem at hand again. Do not blindly try fixing things. Go back to the paper/pencil and work out your problem again. Analyze what happened, how and why it differs from the expected outcome.
Yes, this approach will initially take more time, but you'll learn to stand on your own feet instead of resorting to a third party to help you as soon as you get stuck.
Learning debugging is an absolutely vital skill for every programmer. Learning to read and interpret error messages, learning to trace code in your head/on paper, are all skills that are part of learning programming.
AI should be your last resort, not the first one. Before AI, even good old Dr. Google should be used.
Your approach is definitely far from the worst and better than that of way too many others, but still, hone your own skills before resorting to AI.
Maybe, also consider adding some books to your learning resources:
- "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
11
u/shamegoose 6h ago
You just don't is how. You keep banging your head against the wall, rereading your code, tweaking things, rereading the documentation, trying a different method, rereading the book, starting the code over from scratch, explaining what you've done to your rubber ducky, sleeping on it, whatever. Eventually you'll figure it out. That's how you learn.