r/learnprogramming • u/B0NKB0Y • 28d ago
How do I learn to recognize well-designed code?
I use Node.js and React, but I struggle to tell whether code is well structured or just works. How do I learn to judge things like readability, splitting responsibilities, and unnecessary complexity? What exercises or resources helped you develop that judgment?
12
Upvotes
12
u/theygaytay 28d ago
> How do I learn to judge things like readability
Well, that part is easy enough - read the code, do you find yourself getting confused, having to dart between different parts of the code, or is it cleanly formatted and easy to intuit what the code is actually doing?
1
u/TheSilentFreeway 28d ago
I'll recommend ways to learn how to write clean code. Once you get confident with this, it'll be easy to recognize whether other peoples' code is written well.
In general you want code to be as "flat" as possible (avoid conditional blocks and loops nested too deeply). You want to be a "never nester" because your code will be much easier to read through and reason about. https://youtu.be/CFRhGnuXG-4
You want each function or method to do at most one "thing". If your function reads user input, writes to a file, and sends the file to your friends computer, that's too much. Break it into three (or more) helper functions which each handle a single step in the flowchart of your program. I think this video by Logan Smith does a good job of explaining pure/impure/honest/dishonest functions: https://youtu.be/2OMRWPOSw9s
Specifically for Typescript I'd recommend learning how to leverage the language's type system. It can help enforce certain conditions in your code and make it obvious to a reader what your code is supposed to do. Matt Pocock is pretty good for this: https://www.youtube.com/live/p6dO9u0M7MQ
I recommend reading up on various design patterns, what problems they solve, and how they look in real code. With enough study and experience you'll begin to recognize where they can be applied and whether a given solution is needlessly complex. The book Design Patterns: Elements of Reusable Object-Oriented Software (1994) is still quite relevant despite being over 30 years old. Just keep in mind that not all patterns will apply to every project. Think of it like tools on your toolbelt; a hammer is good for building a table but not for sanding it.
2
u/derNikoDem 28d ago
I'd say that you get a good first hint of knowing whether code is well structured or not by looking at the repo's folder and file structure. If you realise that you can navigate it like a map before even needing to open any files, that's a good sign. Then going one level deeper entering a file and starting reading the code you notice that you don't need to jump from one corner of the codebase to another. You are basically staying within a folder. That shows that the author understands cohesion and decoupling well. Once you are on a function/class level you notice that the code blocks you are reading avoid excessive nesting. My rule of thumb is 2 levels of nesting max. More then that and I start to think about how to split the code across multiple functions.
1
u/symbatix 28d ago
"Clean Code"
Book by Robert Cecil Martin
1
u/AngelGuzmanRuiz 28d ago
You learn when things go wrong, when you need to change some logic and turns out you need to change code in 10 places, when you read code you wrote 2 days ago and don't understand it
1
u/burlingk 28d ago
You get better by reading and writing code. It takes time. :)
1
u/Delicious_Bed_4410 27d ago
One thing that helped me was pretending I had to explain a piece of code to a teammate who has never seen it. If I could not say what a function does in one short sentence, or I kept having to scroll around to follow one idea, that was usually the sign it was doing too much. Another thing is to change your mind on purpose: take something you wrote a while back and rename stuff or split a big chunk up, then see if it reads easier afterward. Your gut for this builds up faster than you would think once you start noticing what annoys you.
1
u/CosmicDevGuy 27d ago
Learn your fundamentals and learn them very good.
0
u/farb3yonddriv3n 27d ago
just explore any publicly maintained code you like, on github for instance. You will see patterns there experienced coders do and over time you'll start using them yourself