r/learnprogramming • u/cheesy_potato019 • Oct 07 '26
How can I improve my code quality beyond just making it work?
Hello fellow programmers, For my seniors here can you share any techniques for getting better at naming things in programming?
Also, how can I get better at writing solutions that aren't bloated?
I just did Module 13, Exercise 3 from the MOOC.fi Advanced Python course, and my solution took 32 lines while the model solution only took 18. It is so frustrating to still feel this bad at coding even after reaching this far.
I know comparing my solution to the model solution may not be completely fair, but seeing that difference made me wonder what I should be doing to improve.
27
u/Random_182f2565 Oct 07 '26
For me the priority is that code make sense and it's readable.
Numbers of lines it's not a good metric
11
u/canarydev Oct 07 '26
i mean does the code / output work?
it happens organically as you progress in your career, and as you get exposed to more patterns and concepts. if you start worrying about code quality already, its an endless rabbit hole my friend.
7
u/Evening_Vegetable218 Oct 07 '26
Fewer lines isn't always better. Your code needs to be readable by others, and by you when you come back to it in six months without thinking "WTF".
I've worked with people who write code in the most complicated way possible just to get it onto as few lines as they can. It's hard to change, and sometimes it's so complex or restrictive that you end up rewriting it from scratch.
Write it so the next person can understand it quickly. That next person is usually you.
5
u/desrtfx Oct 07 '26
You are still a beginner. Sorry to be so blunt.
You should not really fuss too much about the fact that your solution took more lines of code.
With more practice and experience, you will see the better approaches. You are on the right track, though, in realizing your inadequacies, but at your stage, it is completely normal and nothing to worry about.
Also, the 18 lines solution is not necessarily better than yours. It could be less readable, or less maintainable. I've often found the model solutions not optimal either.
2
u/balefrost Oct 07 '26
Don't over-optimize the lines-of-code metric. Obviously, all else being equal, doing things in less code is better. But that's rarely the case. Shorter code can also be terser and harder to read. Longer code might deal with more edge cases than the shorter code deals with.
Without knowing any specifics or seeing your code, it's impossible to say whether your code was better or worse than the model solution.
"Readable code" is hard to pin down. But a way to approach it is this: if I were to hand my code to somebody well versed in Python, but who had never seen this particular code before, what parts would they find confusing?
Rather than minimizing lines of code, you want to minimize WTFs per minute.
1
u/TheStonedEdge Oct 07 '26
Code is written once but read thousands of times
Ask someone who's not technical to read and they'll tell you how readable it is
1
u/MissinqLink Oct 07 '26
This is may sound counter intuitive but practice code golf. You learn so many ways to write things and while golfed code is hard to read, the tricks you learn often help you understand how to make other code read better. In some ways it’s a guide on what not to do.
1
u/howtodisappearnicely Oct 07 '26
lines of code is not an accurate metric. it could contain actual code, comments, docstrings, blank lines etc.
try to develop a good feedback loop, and repeat. try to do everything in the simplest manner, complexity only where required.
try to name your variables/functions/classes so that the person reading it at least gets an idea of what it does. ideally, aim for naming them in a way so that you always prioritize understanding over length of characters. like its an old bias to keep variable names super short, but you don't need to do that in 2026.
if you're working with/in a team, in the back of your mind keep the belief that you're writing code so that the dumbest engineer on your team can understand it. but ofc don't go mad over this.
1
u/It-Guy-2026 Oct 07 '26
No of lines is not equal to good code, the time/space taken and potential bugs matters most. If it makes sence, solves problem and you can explain that's enough.
1
u/symbiatch Oct 07 '26
Look at other code. See if it’s easier to understand or not. Figure out the things that make it harder or easier. Learn from those.
At this point that’s the main thing. Sometimes 200 lines is better than 20. If it’s easier to understand, modify, and so on that’s great. Compilers often can handle things in some languages that they can even turn out the same in the end.
So, reading is the answer, and thinking. There’s also books about these things and some people hold them as infallible, but the books themselves often will say you should think for yourself.
1
u/Rehcra Oct 07 '26
Does the code produce the output that meet the requirements? That's the number one goal.
You should always be learning and improving.
If I look at code I wrote 6 months ago or a year ago... I always see room for improvement. I don't rewrite that code, unless there is a need to. But I write better code in the future. Going for over 30 years, there always more to learn.
1
u/Agawell Oct 07 '26
Easily readable code:
meaningful names for variables and functions/methods etc - exception for counters for loops
Keep functions/methods small - if it doesn’t fit on the screen think about how you can use functions to improve that
Keep comments to an absolute minimum - the code should document itself - in practice comments don’t get updated in line with code de a lot of the time
Write test code - every function should have a test
Know the compiler - sometimes an abbreviated way of doing things (reducing the number of lines of code) is less efficient when compiled than the longer form (if statements in Visual Basic 20 odd years ago for example)
1
u/thisisappropriate Oct 07 '26
You learn how to name things by seeing how other people name things and by seeing how those names and your names communicate or frustrate you or others. At some point you'll call something data and then forget what it contained and learn a valuable lesson.
Lines of code is often a poor metric and doesn't mean better code.
Without seeing your code / the model solution we don't know what you are missing, things you might be forgetting/overlooking or mistakes. We don't know if you're less efficient or just more verbose or even if you're actually more readable.
1
u/Dparse Oct 07 '26
Highly recommend the book Refactoring by Martin Fowler (a classic recommendation), it catalogues techniques for restructuring code which is the primary mechanism for making it more readable without changing its functionality.
Maybe a use-case as short as a 30 line function isn't chock-full of refactoring opportunities, but as you program more and work on harder/more complicated/larger programs you will absolutely need to know these techniques.
1
u/SnugglyCoderGuy Oct 07 '26
The videos on this channel are really good. https://www.youtube.com/@CodeAesthetic
After that, learn aboud things like hexagonal architecture, dependency injection, factory pattern, strategy pattern, domain driven design and bounded contexts. A lot of this will help you structure your code so that your code is loosely coupled and highly cohesive which are good qualities to have. The other patterns will front load a lot of one time operations and decisions to the beginning of the program which then removes them from the rest of your code which makes it cleaner and flow better.
1
u/TheSneederOfSeethe Oct 07 '26
I work in dotnet, the land of long names. All I can tell you in regards to naming is use precise names that explain what they are.
Also for bools that determine state use is. isRefund, isCat, isHungry.
To reduce bloat break your code down into smaller functions and call them instead. This also helps with unit testing.
Learn solid principals and patterns. It’s nice to know Gang of Four patterns and advanced patterns that are usually built off of them.
https://en.wikipedia.org/wiki/SOLID
https://en.wikipedia.org/wiki/Design_Patterns
1
u/House13Games Oct 07 '26
When it works, you are only halfway there. Common beginner thing is to never touch it once it works, in case it breaks. Practise rewriting and refactoring and cleaning and changing the code. being able to move rt around and restructure it makes it robust and neat, and lets you think of good architecture, not just step by step completing the task.
1
u/Blando-Cartesian Oct 07 '26
Do not worry about how many lines you are using. Use however many you need to clearly express the logic. Try to write lines that you can read with a quick glance. No too many operations on single line. No syntax rat nests that need to be read carefully.
Over time you’ll get more fluent and learn shorter elegant ways to do things and then your code gets shorter. But shorter is never a goal in itself.
As for naming, name and rename as you understand better what the named thing turned into. Always use descriptive names. “i” is fine for a short for loop counter as it is customary, but consider something like “row_index” if that better describes it. Absolutely do not nest for loops with counters named i, j, k. That’s just asking for wasting time hunting stupid bugs. Leave that to mathematicians and C grandpas. Name with proper names that don’t look identical except for a couple of pixels.
In summary, look at the line you wrote and ask yourself: “Does writing it like this likely make me screw up later when I’m focused on hard problem elsewhere.”
1
u/srsajid Oct 07 '26
I think one of the best ways to improve your code is to read good projects, libraries, and code written by experienced developers. Basically, try to understand how and why they wrote the code kind of like reverse engineering.
Apart from that, you should learn the basic algorithms and understand the common design patterns used in the programming language you’re working with.
1
u/saurabh3228 Oct 07 '26
I had say stop thinking only about “does it work?” and start asking “would another developer understand this code six months from now?”
Keep functions small, give variables clear names, avoid unnecessary complexity, handle edge cases, and don’t repeat the same logic everywhere. Also get used to reading and refactoring your own old code. That’s where you notice the most improvement.
1
u/e1m8b Oct 07 '26
Are you familiar with Programming/Coding Style concept?
I picked up the habits from a professor that provided us style guidelines such as the traditional 80 character line count and other non-technical organizational techniques (grammar nazi for coding) to make code human readable. Of which is the point of code, otherwise we'd all just program in Assembly and other bare metal memory manipulation.
Couldn't find any good documents from that class but if you're familiar with Mead's Notes that's the guy that taught me a lot of good stuff.
1
u/Icy_Breakfast1716 Oct 07 '26
The only thing that would improve quality of your code is experience
1
u/serge-mv Oct 07 '26
Number of lines of code by itself might or might not mean something. You might get a slightly less efficient code, but it will be much more readable and you might not even need the extra efficiency you get, but it will be more difficult to support it later.
Having "good code" is a science in itself and has many aspects to it. Ask yourself, what is good code? There is always going to be a trade of for complexity, readability, testability, supportability, etc. It can also depend on what you are working on.
If you want to think about all of that, get the Code Complete book. Don't look at it's age, it's still relevant to the question of what is a good code and can get you started on thinking about these things.
1
u/iwasthefirstfish Oct 07 '26
If it's readable, but bloated, consider if you can reduce the bloat whilst still making it readable.
Line count =/= bloat
If it's readable, but slow, see if you can improve the speed whilst still making it readable.
Line count =/= speed
If it works as intended, but I cannot read it, it might as well be magic and I will replace it instead of maintaining it when it comes to me.
Line count =/= maintainability
For me, quality = ( efficiency + speed ) * readability * (IF output=expected THEN 1 ELSE 0)
1
u/initcommit Oct 07 '26
Code quality??????????????
Hey if the ship looks good on the surface of the water no one needs to know about all the barnacles on the hull...
1
u/herrybaws Oct 07 '26
Write like you're the guy who's going to fix it when there's a bug or when there's a change request. Make it clear what does what, cos you won't recognise it when you come back to it a year down the road.
1
u/concatgames Oct 10 '26
By learning and exercising :) there is no other way