How to write more elegant code

To coders who are more experienced, how did you eventually begin to write more compact and elegant code? I am comparing my code to answers provided by fCC and am noticing a difference in their visual aesthetics (brute force method vs. careful precision). I guess it just takes time and practice.

Time and practice, and also making yourself go over your own code and clean it up. You need to practice not only writing clean code, but fixing bad code. I often find it easier to recognize bad patterns in my code and change my approach than to just do it right from the beginning.


Yeah, I am noticing that I tackle challenges a certain way, which might not solve the problem very efficiently, so instead of taking two steps to make the code work, I take five or six.

That’s perfectly fine. What I would recommend is that when you finish a challenge you:

  1. Stand up
  2. Stretch
  3. Get a glass of water and maybe a snack
  4. Come back to your solution and start breaking it: look for opportunities to tidy things up. This will very often mean that you solution temporarily fails. That’s fine. I firmly believe that tinkering with code you think you understand is the best way to really “get it”.