Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I read this thread on programmers today and thought that looked like a really elegant way of coding. I would like to discover more neat methods of coding.

What are the best ways of discovering new elegant ways of coding? (Im already aware of the standard design patterns)

share|improve this question
    
Put your code to the test: Code Review Stack Exchange –  Yannis Rizos Nov 25 '11 at 0:47
add comment

4 Answers

up vote 4 down vote accepted

If you want to learn about elegant code, read code and lots of it.

Look at popular open source applications and frameworks - not all code would be good or elegant, but the more code of other people you are exposed to the more you will learn. Some of the code you will see will be very elegant, some horrible - both will teach you.

share|improve this answer
add comment

I do always equate elegant code with readable code and code which is really easy to understand.

In my opinion you don't have to follow many patterns and practices.

Just lean back and think :

would a coworker or reviewer understand this immediately ?

share|improve this answer
    
+1 I modify the last line to say Would an average programmer or reviewer understand this immediately. –  Sardathrion Nov 24 '11 at 13:56
    
@Sardathrion: pardon for my bad english –  ralf.w. Nov 24 '11 at 14:14
    
Not at all. Coworker is perfectly fine. I was just saying that I tend to lower the bar on your statement to average programmers and not just to coworkers. I apologies if my statement caused offence. It was not meant to. –  Sardathrion Nov 24 '11 at 14:47
    
+1. I would add that this coworker is maybe yourself in three months, i.e. yourself without the short-term memory you have right now when writting the code. Would you want to do changes to this code? –  Clement J. Nov 24 '11 at 15:58
add comment

I would definitly recommend to read Clean Code: A Handbook of Agile Software Craftsmanship.

1 of the 10 top books for programmers, easy and pragmatic guide to clean code (design patterns are not treated in this book).

share|improve this answer
add comment

Elegance is a subjective quality, in my opinion. It is a bit like beauty in that it is in the eye of the beholder. Different people like different things. I don't know of any objective list of characteristics that equate to elegance. Elegant code is hard to write.

That being said, it is very easy to spot unelegant code and design when faced with it. Therefore I agree with the other contributor here who recommended reading lots of code. Good design is often the result of analysis of a design's bad characteristics and smells. It seems logical that good, clean, elegant code is the result of analysis of a block of code's bad characteristics and smells.

Always write code for those coming after you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.