views:

409

answers:

8

Writing clean, elegant code is what makes coding a pleasure for me. I'm always looking for more ways to do so. Here are some of my strategies/tactics:

  • Keep large code blocks in smaller chunks of routines/methods/classes

  • Be sure every routine has one point of exit. Exit loops with logic at the top of the loop, not with breaks or some type of "exit loop" keyword.

  • Avoid globals

  • If you just performed a copy/paste with your code, it is time to create a routine/class/library.

...and one I've been toying with lately:

  • Try to replace if/branching/select cases with different method signatures (polymorphism) Clean Code Talks on YouTube
+1  A: 

Yup, those are good ones.

The best one, I think, is to assume that the first version isn't the final one. plan to rewrite and refactor. Look at smallish chunks, and think how they could be done more elegantly.

Also, read code, especially known good code.

Charlie Martin
+2  A: 

"one, two, many".

A remote wild population in some remote angle of the planet has no words in his language to count beyond two. After two, there's a word for "many". My previous boss preached this phrase to keep low the number of arguments in a routine. I tend to agree with him.

"Don't Repeat Yourself (DRY)"

if you say the same thing in two places, then you have a problem and you should refactor. Sooner or later the two (or more) places will be desynchronized.

"Keep It Simple, Stupid (KISS)"

Don't overdesign.

Stefano Borini
I originally heard "one, two, infinity" and its variations as an explanation of when to write a tool to automate a task.
Steve S
This is a great quote, thanks for sharing! =)
Alix Axel
+4  A: 
Chuck Conway
Thank you for the suggesting, it's now on my to buy list.
Alix Axel
+1  A: 

-Keep large code blocks in smaller chunks of routines/methods/classes

What I like to do is create one main function at first so I don't have to create tons of mini-functions from the get-go which hampers my train of thought, and then later refactor it into smaller functions. Refactoring is great.

Be sure every routine has one point of exit. Exit loops with logic at the top of the loop, not with breaks or some type of "exit loop" keyword.

This is not necessarily cleaner. It only makes it easier to prove a function's correctness. See http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement for more details.

Avoid globals

Sometimes globals are okay. Many times people use a singleton just to avoid using a "global" which is actually a global in disguise with more overhead.

Unknown
+3  A: 
mandaleeka
A: 

Don't procrastinate. Ever. When you think of a way to improve a section of code, go and do it right then, no matter how hard it may be, or how long it might take. Always look for potential improvements and never take the easy way out.

Now, this is not the most efficient or profitable way of developing code, which is why professional codebases are rarely examples of beauty. But that wasn't your question.

Travis
A: 

Often times, clean code is associated with short functions or better indentation. So do not fall into that trap. Spend considerable time in designing classes. There are good principles out there such as SOLID principles that will allow you to design and build good meaningful classes. Then comes the selecting appropriate design patterns to model the behavior of your classes. Of course always remember to follow the keep it simple principle. Finally, make sure there is sufficient code coverage for complex functions in your architeture. Following all these things can help you write clean code. Here is a good list of books that I recommend to my friends and colleagues.

CodeToGlory
+1  A: 

In Comp Sci I heard something that always stuck with me:

"In Programming there are only 3 numbers: 0, 1, and infinity."

Practical realities of course, force us to use other numbers. But I always feel my code is becoming less elegant if I use a number other than these three.

Mike Lewis