What is the purpose of writing 'elegant code'?

Hello lovely community :slight_smile:

I’m confused as to why we need to know about the ‘elegant code’ version of programming, aka the ability to write in a single line. Could anyone please give me an example of this in practice? Learning the ‘non-elegant’ way is challenging enough and I struggle to then translate this into the one-liners, so to speak.

Thank you so much!

1 Like

Hi, and welcome to the forums!

It’s not something that’s necessary, it’s a type of coding that not everybody likes or finds to be useful. However it’s important when learning to learn about all different aspects of coding, so that you can make an informed choice on how you want to go about it.

I will speak to my own experience though. I work for the Scottish Health Service, and we work a lot in R doing data processing and analysis. The thing with this is that the scripts can get incredibly long, we have many scripts that are more than 3000 lines long, some even surpassing 5000 lines. A lot of this was down to things being done the simplest way initially as we had to react to an evolving situation, and didn’t have time initially to perfect our code. However when you have scripts of this size, it can be very hard to navigate and debug effectively. Half your time can be spent scrolling looking for the one line that’s breaking.

That can be where elegant code comes in, as by working to shorten these as much as possible, it can something make it much easier to locate problem code and fix it. Most modern applications will have several thousands of lines of code, and being able to cut some lines here and there can be very useful. However of course there is an argument to be made that fitting a bunch of code in a single line can be harder to understand and then actually make it harder to debug.

That’s why in our work, I like to go for a more balanced approach. I reduce the line count as much as I can by combining operations, but I will always try to keep some level of readability. For an example in python:

list = [5, 10, 15, 20, 25, 30, 35, 40]
new_list = []

for num in list:
  if num % 2 == 0:

# prints [10, 20, 30, 40]
list = [5, 10, 15, 20, 25, 30, 35, 40]

new_list = [num for num in list if num % 2 == 0]

# prints [10, 20, 30, 40]

The above two pieces of code do exactly the same thing. However personally, I find the second one to be much cleaner. It is using list comprehension to combine all the operations into a single line, saving script space, and making thing easier to locate in a large script. However it could definitely be harder to understand if you don’t really get list comprehension or use it very often. So it’s all about a trade-off of what you prefer to use. The course is just trying to give you as many tools and options as it can so you can make your own decision.


I think you need to find a balance, too short code (bordering on obscurity) is not a good thing.

The reason we want elegant code is so the code is readable and maintainable in the long run. That if a feature or requirement changes in the future (like after 3 months or so), you can easily find, read, understand and modify the code

This is something that takes time and practice to learn.

I am going to borrow from @adamgaffney96 answer, I would dread if I had scripts which where over 3000 lines long. I do OOP (object orientating programming), I try to avoid classes over 500 lines long and each method (function) should preferable be less then 50 lines. These are not hard-limits, but if I would exceed these self imposed soft limits, I would take a moment if I need to refactor (re-write/re-structure) the code/classes

But this comes with experience and practice, I noticed I became better and better at writing re-usable components, structuring code the more I programmed and worked on real-world web applications


Thank you both so much, this makes it so much clearer, especially as it puts it into a more practical context.

1 Like

Another consideration is that regardless of whether you write “elegant code”, you will be looking at a lot of other people’s work. It’s important to be able to read the code just as much as it is to write your own, and if you weren’t taught that a certain syntax is valid, you would waste a lot of time needlessly trying to figure out those syntax issues rather than focusing on the content itself.

I personally don’t mind the “elegant code” concept, but I wish some languages (looking at you, javascript) would be more strict. Having five different ways to write the same line of code is, to me, a failure in the language design. It’s like trying to learn a spoken language where the same concept is described with multiple words or phrases, and there’s no distinction between the proper way and variants. I hope language designers read this and realize that there really is such a thing as being too elegant/succint/simplified.


Once you program for 5+ years, syntax becomes easy. You will see that a lot of languages share a lot of the concepts, so learning a bit of syntax is easy.

JavaScript has actual an interesting history, the person who wrote the very first version of JavaScript was given 10 days, which is not enough (not by a long shot). So he decided to make it as flexible (given the circumstance a sensible decision). Then JavaScript grew and grew, became dominant on the internet. This created a problem, where some other language (python, php) made breaking changes, JavaScript (the people behind JavaScript) don’t want this. Because you don’t want to break the internet. Fascinating how certain decisions still have consequences decades later

So I have somewhat forgiven JavaScript (still a programming language I would rather not use), and if I had too, I would use TypeScript (developed by FB or google, not sure). Given us more static typing

any large programming language these days is not maintainable by a single person, usually its a group of people or an organization. These people are generally very skilled and know what they are doing, but also face certain challenges. Problems in a magnitude a “beginner” problem can barely comprehend.

I put beginner in quotes, because I think this apply to a lot of people who don’t have more then 10 years experience (with a few exceptions of course)


That makes a lot of sense @stetim94, and thank you for taking the time to explain. If there is such a thing as “cluless beginner” level that’s exactly where I’m at right now :rofl: I hope that feeling improves at some point so I’m grateful for the support I get.

1 Like

I must admit I find „elegant“ rather confusing when talking about one liners. Elegant code for me is structured in way that makes it easy to understand and read. One line of code isn’t necessarily that. But it depends as always. As some pointed out it totally makes sense in super long scripts to cut some lines here and there. But other than that they do not really add clearance and/or readability - in my opinion.
Elegant - not really, but definitely effective and crisp.

1 Like

I think it is always a balance, I seen people do this:

if x == y:
   return True;
   return False;

then I am like:

return x == y

Then I find my one line more elegant.

But I agree, structure and organization of code is also very important. I have several hundred (if not more) classes in my projects, if those are poorly structured/organized…


Interesting concept, as a beginner myself with just less than a year of coding in JavaScript sometimes I feel my codes are long and when I find a cleaner way of writing same code, I feel am not a good of a developer. Just a few hours ago I solve a codewar challenge which is finding a character in a string and replacing it. Not aware of the string replace method, I used arrays and for loop to solve it but the replace method is just straight forward and can solve the problem with just a line of code. I know as I practice more my coding skills will improve and will write better and cleaner code.