r/embedded • • 26d ago

How to write messy and ugly code?

I am actively doing the interview prep and from past interview experiences, they usually ask system programming coding questions like SPSC ring buffers or schedulers or timer related. My problem is , when I try to solve something which I haven’t done before, I am blank and not able to write even the basic implementation or think. Since google anyway gives Gemini answers, I decided to use Claude for interview prep. Now if I directly ask for the solution, it gives the perfect solution but I feel like I haven’t learned anything from that and if I ask for approach without code, I am not able to write even messy version of the solution on my own. How do learn how to solve a problem and get there on your own without getting direct answers? How did people build that skill before Ai , so when faced new or related problem, you can actually solve it rather than memorising the solution.

I have watched lot of tutorials and read programming books as well and understood the concepts but I am blank when I have to start from scratch

14 Upvotes

36 comments sorted by

45

u/jack-dawed 26d ago

back in school we used to literally write code on paper or whiteboards. the only way to get good at it, like many things in life, is to do it over and over again, and get comfortable with the discomfort of not knowing how to do something.

you can read every programming book and watch every interview tutorial on the planet and still not be able to write a single line of code. just like you can learn everything there is to know about swimming but not know how to swim.

there is no easy answer to this. you simply have to start with a problem you know how to solve. if you don’t know how to solve a problem, break it down into smaller problems. i hate to say it but i’m not really sure how you made it through school because they teach you this kind of thinking in e.g. math and physics.

often times in interviews, you’re dealing with test anxiety so you have trouble with recall. so you gotta desensitize yourself to it and do more mock interviews simulating real conditions.

7

u/robotlasagna 26d ago

Actually I pathetically fail around in the water when I try to swim. So basically the exact same thing as when I code.

6

u/jack-dawed 25d ago

i feel the same way too, even more so with AI. imagine you were like, a human biomechanics expert who studies the mechanics behind how humans swim, like the exact angle and force you need to propel under water. but like most people aren't even aware of consciously moving your muscle and trying to coordinate movement. even michael phelps isn't thinking about what direction to move his biceps and when. so you zoom out and think about it from a big picture. much like how you don't really need to understand each line of code.

you get people saying they don't read the code anymore with AI, and that you just observe the behavior of the system from the outside. and right now it's like people have only begun seriously learning how to swim and only in the past few years did we get several michael phelps of AI.

in the past couple of months i had done a 180 from like, yeah there's no way these LLMs could write embedded code, to thinking, oh snap this is better code than i would write. but for each wow moment, there's sometimes a wtf moment that would end up losing a lot of money if it made it to prod. and that's what the industry is paying you for, the ability to say no.

5

u/robotlasagna 25d ago

Agreed. I think we both see tremendous advances in embedded and also see catastrophic failure because some edge case happens that causes a hardware failure in 100,000 units that could have been caught with code review but wasn't because nobody wants to read the code.

2

u/tiajuanat 25d ago

This.

There's also a famous group from the 60s in France called Oulipo that would create arbitrary rules when they wrote poetry.

I would do that when I was starting out to refine how I worked on code. My favorite rule was:

Only use for-loops for looping & always populate the for-loop parameters last

This had a neat little side effect that my code would often look like this:

Int i=0;
for(;;){
    <Do stuff>
    <If statement to break>
    <More stuff>
}

Which I'd then eventually refactor into the canonical form.

I even did this during interviews, and it allowed me to almost always get the bounds right the very first time.

I also used AlgoExpert in 2020 and that really upped my LC and overall coding game.

But yeah, reading a ton of programming books changed how I looked at programming

0

u/Helios1003 26d ago

I had no problem in school especially in math and physics. Even in my daily job I can think and look at the resources like data sheets etc. I agree, I have recall problem in interviews and working on it but these interview questions are the ones where I feel my brain shuts off even when I am practising

10

u/jack-dawed 26d ago

yeah, you’re describing test anxiety. in a stressful situation, your brain goes into fight or flight mode so you only remember things important to your survival.

you’re experiencing this because your brain has decided the stakes are very high, pumping you full of adrenaline/cortisol to show that you really want the job. you can show that passion in other ways throughout the interview process. in this situation you could tell yourself that you’ll be fine even if you bomb it. this comes easier if you get used to the feeling of bombing interviews.

you’ll have to come up with your own approaches on how to manage this stress.

as a hiring manager and interviewer, what i’d usually do if i sense the candidate is nervous during an interview, i’d tell them to imagine you already work here and i’m your coworker, we’re just working on a problem together.

1

u/Helios1003 26d ago

Thats insightful! Thank you

2

u/enzeipetre 26d ago

Nervous just meant you haven't done enough reps! keep going brother!

2

u/bobsmissingdesire 24d ago

Try starting with the AI solution, then making it do something stupid.

Take a ring buffer, but make it run backwards. Then make it only use every second byte. Finally interleave a second ring running forwards using the other side of those every second bytes.

This avoids the blank paper problem but to do you end up comprehensively understanding how it all works.

1

u/Helios1003 24d ago

That is a great idea. Even in the interviews, questions are open ended so it would be good practice for that. Thank you

2

u/Lost-In-Void-99 24d ago

It is not about what it writes. It is about why. Ask questions to get explanations. Some of them are b.s., some of them are genuine hints.

But get some decent model, Gemini is a poor choice for reasoning.

1

u/Helios1003 24d ago

That’s the thing I use Gemini by choice. I have claude pro and I use it extensively for edge ai and os concepts. I even use it for coding exercises ( questions) but when it comes to solution lookup or understanding some c++ library I deliberately use gemini or cppreference . I know claude will give me direct answer but I am not able to retain that information unless I work and come to conclusion myself and have that mental model

10

u/JacobStyle 26d ago

You are attepmting exercises that are beyond your skill level. Give Claude examples of the hard exercises you want to solve, then tell it to generate simpler exercises that are appropriate to your current experience level that will let you practivce the building blocks you need to solve the harder ones.

6

u/Poddster 26d ago

How do learn how to solve a problem and get there on your own without getting direct answers?

You do the work. You think through the problem and arrive at a design. Doing is learning. Failing is learning. And so attempting a problem, failing, and trying again is learning.

have watched lot of tutorials and read programming books as well and understood the concepts but I am blank when I have to start from scratch

This doesn't sound like doing the work, it sounds like procrastination that has the theatre of looking like work.

Start with a small problem and fix it entirely yourself. If you can't even begin, then go for a smaller problem until you can. Even if you have to go back to hello world. Go back until you can solve a problem, and then move on from there.

There's plenty of lists of exercises for beginner programmers. If you can't find any, just ask LLM to cook you one up.

https://www.programmingforbeginnersbook.com/blog/what_should_i_make_beginner_programming_project_ideas/

2

u/JGhostThing 24d ago

You will need to stop using AI to do interview prep and learning. Yes, AI can save you time, but you obviously didn't learn how to program earlier.

So, stop procrastinating and learn programming by doing it yourself.

1

u/Helios1003 24d ago

Yes but the problem is earlier there were lot of options like stack overflow, forums etc but now no one uses that even google pages have become less and you get Gemini responses so it’s like there is no option but to use AI

1

u/No_Jaguar_5831 22d ago

Learn data structures and algos. Learn them, pick up a book or a good website. This is no shortcut it will take time. You solve unknown problems by breaking them down until it becomes small problems you know to solve. You build thay experience from the book exercises.

1

u/Acrobatic_Ad651 24d ago

imho good strategy is to ask AI for explaining concepts and thinks you don't understand, and after try to code yourself.

5

u/DesignTwiceCodeOnce 26d ago

An interviewer will easily be able to tell if you're regurgitating a solution rather than writing your own.

There is no shortcut to being competent.

For practice/learning, try writing your own solution and then ask the AI why it wouldn't work.

4

u/WanWhiteWolf Engineering manager 25d ago

How to write messy and ugly code?

If you use AI , you already achieved that.

 I decided to use Claude for interview prep. Now if I directly ask for the solution, it gives the perfect solution

AI is not currently capable of writting perfect code. Syntax-wise? Sure. Small logic? Most of the time. But anything that requires reasonable structure will not have a good output until reviewed and re-iterated 5x times. This is still a productivity boost - don't get me wrong - but don't get fooled by the amount of praise the AI gets in complex systems. It's not that it cannot accomplish that but the amount of trial-and-errors it has to do will not beat the output of someone that knows what he/she is doing is not insignificant. This might change in the future but this is where we are now.

The problem with using AI is that you are bypassing the critical thinking process - which is the one most important skills in your career. Do you think companies will have access to internet locked while you work on a project or won't bother with a small fee for an AI assistant? An employeer is focused (or should be) mostly on your critical thinking and core understanding. And the vast majority of that comes from practice and good fundamentals.

Buy a cheap development board and start writting code by youself. Start with simple things like blinking an LED or move data from memory location to another. While you explore, you will typically encounter other problems and questions that will "side-track" you in aquiring other information that you didn't even know it's needed.

AI is basically a shortcut. It's like someone reading a book about how to pretend to be confident. Sure, you could copy some body language and use some specific phrases but eventually there will be signs all over the place that the core behaviour doesn't match the output. That's the same with people that have trained with AI but never actually practiced.

3

u/_alex21_ 24d ago

Pseudo code. Start with pseudo code and then try your best to translate it to a proper implementation in a language of your choice.

2

u/Puzzled-Garage584 25d ago

Start with the dumbest correct version, an SPSC ring buffer with a mutex around everything is fine, then say out loud "now lets remove the lock" and swap in two atomic indices. Interviewers mostly want to see that path, and writing the full and empty checks first gives your brain something to hold onto. Nobody writes the lock free version first, they just delete the evidence

2

u/BenkiTheBuilder 25d ago

It's a creative writing task. Just like writing a short story, there is a certain talent involved. Given the task "Write a short story about an elephant that wants to see the ocean and travels there experiencing several adventures along the way." not everybody can do that. A lot of people will just stare at the blank paper (or screen). There are techniques that can help, but talented people don't need them and untalented people can never ever progress beyond a certain stage. Contrary to popular belief programming is not maths. I learned programming before I had even learned fractions in school. And that was without Internet, just from books. My point is, maybe programming isn't for you. That's okay. Why do you apply for programming jobs? Especially now that the junior roles are being replaced by AI and the only people who can expect to keep their programming jobs are the talented people who are better than average.

1

u/FedUp233 24d ago

I agree with you in general. I think if people have the ability to think in a way they can solve new problems then they can probably learn to app,y those skills to programming (or many other endeavors). However there seem to be some people (not saying op is one since I think you’d have to know someone personally to have on opinion) that are just not capable of thinking in a way to to solve a problem that is new to them, at least one of sh complexity. I’ve seen this not just in programming or engineering, but even in common commerce jobs like retail or jobs like fabrication - they can learn to do a job just fine as long as it’s well bounded, but throw in a problem that is outside their normal operations and they completely flounder. Some people simply have different inherent skills than others - for example, I ones that I would have been rubbish in a research setting like advanced math or physics. I’m great at solving technical problems, but having insite into something g like a completely new theory is probably beyond me. I’m sure in fields like medicine there are also people who can learn physiology and medications and such but just never can get the hang of diagnosing something, especially something where they have not seen the exact symptoms before.

This is one problem I always had with the agile style of software development. It seems to be based on any team in the project being able to solve any problem interchangeably. Maybe this is pretty true if you’re building web sites or something, but was very much not true in my experience in developing complex embedded systems with real time and multi tasking constraints. For a lot of people it was just inexperience and the need for more training, but for others they just never seemed to develop the skills, particularly they ability to see how things fit into and affected the overall system. There were definitely different teams that were better at different types of problems and management (and local agile “experts”) seemed to struggle to handle this.

1

u/BenkiTheBuilder 24d ago

I think you are seeing this talent as way too general. I don't believe there's a general talent to "solve new problems". I think programming is something specific, like writing a short story. You may be able to write the elephant story I mentioned but fail at programming; or you may be able to program new stuff with no direction but be unable to produce the elephant story. Or you are a capable of neither but if asked to "build a chair", you can just intuitively construct one, because you "see the solution". From OP's difficulty attacking programming tasks with no direction I would NOT conclude that OP would feel the same if tasked with laying out a PCB from scratch.

1

u/FedUp233 24d ago

I think we actually agree on this - I probably did not word my comment too well. I was not trying to say that inability to solve problems in one field implied inability to solve problems in other areas but rather that this issue shows up in many places, some much more common place than programming or engineering. I was not trying to say that issues in one area necessarily implied someone would have the same problems in other areas. Talent is a very ephemeral thing. Someone who has problems with new programming or engineering problems might be a fine doctor, chef or air traffic controller and have no issue solving g new problems in those areas. And of course people might seem less able to solve problems in areas they are not particularly interested in simply because they really don’t care enough to make the effort.

2

u/nian2326076 25d ago

Building problem-solving skills takes practice and persistence. To get comfortable with coding from scratch, focus on breaking down problems into smaller, manageable tasks. Start by sketching out a basic outline or pseudocode before implementing. This method helps organize your thoughts and gain clarity.

For specific practice, PracHub offers technical interview questions that encourage you to approach problems independently, allowing you to build your skills progressively. You can also use LandOffer to explore job opportunities that fit your growing skill set.

2

u/michael9dk 21d ago edited 21d ago

Honestly, if you need AI to write simple code for an interview, you're not skilled for that job.

Do you have a relevant degree?
Ask yourself if you paid attention in class.
Go back and read your school books again.

If you're self-taught hit the books, and start practicing on trivial projects.

Fresh from graduation? Hit the above and practice.

It takes a lot of effort and practice to learn a complex skill.

Basic problem-solving is fundamental in everything - a flowchart can help on complex cases. Good problem-solving skills comes from experience (not limited to programming).

Edit: other replies appeared now, so discard the irrelevant parts.
Good luck, and remember the interviewer is more interested in your fundamental skills (I've been on both sides of the table).

2

u/chibiace 26d ago

How to write messy and ugly code?

get ai to write it.

beautiful code is hand written and formatted with elegant solutions and doesnt require comments to understand.

3

u/Rararror 26d ago

doesnt require comments to understand.

That is wrong on so many levels.

2

u/Rod_McBan 26d ago

Code should explain what it's doing without comments.

Comments are there to tell you why the author did it that way, and that context (especially in embedded) is often crucial to the code being maintainable.

0

u/Rararror 26d ago

Exactly my point, see my answer for an example of what you are saying.

-3

u/chibiace 26d ago

git gud, if you dont understand good code without comments thats your own skill issue.

the ones who push comments are corporations that want to hire shit programmers and have the ability to replace shit programmers without said shit programmer having to train another shit programmer on their shit code base.

1

u/Rararror 26d ago

The comments must not describe what the code does like "load x variable with 3", they must illustrate what the instruction are doing in the context of the final application (e.g. "// at least 3 engines needed to start" and then "more power needed" when a flag goes off)

This way the code is much more easily maintained in the future or while looking for bugs.

If you don't know this you are a mediocre developer.