How to write cleaner code

How to write cleaner code
0

#1

This is just a general question about how to get better at writing cleaner and more efficient code. I’m mid way through the intermediate algorithm scripting and for the most part, I can hash out some version of a working solution if I try hard enough. However, my solutions tend to be on the “beginner” side. When I peek at the “intermediate” and “advanced” solutions, I can’t even imagine how I would make the cognitive leap to come up with such elegant solutions.

So, how does one get from “beginner” code to clean and efficient “advanced” code? Does it eventually fall into place with practice (this doesn’t seem to be working so far as I just keep using the same old tricks in my back pocket)? Should I just keep studying the advanced solutions? Is there a book/resource that can help train me to think differently and more efficiently?


#2

Hi,
Funny,
This has been one of my concern as well.
I just finished the udacity HTML CSS beginner course and they provide you with a best practices guide.
It also helped me a lot to better understand how to do things right in general and more efficiently.
This is still very interesting if you just finished your portfolio for instance

https://www.udacity.com/


#3

I wouldn’t concern with efficient code at the moment. Do your best to learn the technology and solution will come as you get more experienced.

Read codes from other campers and trying “proving” that you can get the same result in a different way. As how math can be proven and create new formulas


#4

When I’ve looked at the wiki (which is only when someone else asks about it) I haven’t always loved their “advanced” solutions that much. I think it’s hit or miss, depending perhaps on whoever added that implementation.

What you want to learn how to do is called refactoring.

The dirty secret is that experienced coders don’t necessarily bang out an amazing solution with reduce and closures on the first go either. It could easily be a mess of for loops, just to get the darn thing working at all.

The beauty of TDD (test driven design), where you have tests written for you before you code at all, is that once you get that messy looking solution, you can keep working on it, applying higher level abstractions, identifying redundancies, etc. and generally making the code better.

This could include making the code more performant, but I don’t recommend you focus on performance at all (until you know you need to).

What you should strive for is simplicity and readability.

There are definitely some books to read on the topic of refactoring, but ultimately while you can learn theory, the key is still just practice. Next time you solve an algorithm, before you look at anyone else’s solution, spend another 30 minutes examining your code and thinking about how it can be simpler and more readable. Eventually you may end up spending more time after all the tests pass than before.


#5

@frenata Never heard the term refractoring before. Good to know! And yep, the reduce function has eluded me to this day. I’m thinking that after doing a first pass of the the algorithm challenges, I’ll try and consolidate everything I’ve learned by going back and refactoring. Thanks for the advice.


#6

It’s all about practicing as I see it. If you’ll practice more you’re going to be better at coding better and without errors. If you’re getting troubles and think you should use some help there some good code security systems you can use, such as Checkmarx and others.
Good luck!


#7

One thing I’ve found useful when trying to learn a new concept is to force yourself into using it everywhere for a while, even when it isn’t necessarily a good idea…

Of course, if I’m still learning it, how do I know when it’s a good idea and when it’s not? By making myself use it everywhere for a while!

What happens is that you should start to get a feel for where the feature/concept makes things simpler, and where it doesn’t. I’ve been meaning to do this myself with closures and generators, working back over some previously solved FCC algorithms from a new approach. You could easily do the same with map, reduce, and filter!


#8

I always seem to pick up new tricks when reading other people’s code. Just by chance, i find that you will keep running into different ways of doing things that you didn’t know are possible, or just creative new ways of using things you already knew. Little by little, you will pick up many tricks as you read other people’s code.