Clean Code: A Handbook of Agile Software Craftsmanship

Author: Robert C. Martin
All Stack Overflow 184
This Year Reddit 166
This Month Hacker News 1

About This Book

Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way.

Noted software expert Robert C. Martin presents a revolutionary paradigm with Clean Code: A Handbook of Agile Software Craftsmanship . Martin has teamed up with his colleagues from Object Mentor to distill their best agile practice of cleaning code "on the fly" into a book that will instill within you the values of a software craftsman and make you a better programmer--but only if you work at it.

What kind of work will you be doing? You'll be reading code--lots of code. And you will be challenged to think about what's right about that code, and what's wrong with it. More importantly, you will be challenged to reassess your professional values and your commitment to your craft.

Clean Code is divided into three parts. The first describes the principles, patterns, and practices of writing clean code. The second part consists of several case studies of increasing complexity. Each case study is an exercise in cleaning up code--of transforming a code base that has some problems into one that is sound and efficient. The third part is the payoff: a single chapter containing a list of heuristics and "smells" gathered while creating the case studies. The result is a knowledge base that describes the way we think when we write, read, and clean code.

Readers will come away from this book understanding

  • How to tell the difference between good and bad code
  • How to write good code and how to transform bad code into good code
  • How to create good names, good functions, good objects, and good classes
  • How to format code for maximum readability
  • How to implement complete error handling without obscuring code logic
  • How to unit test and practice test-driven development

This book is a must for any developer, software engineer, project manager, team lead, or systems analyst with an interest in producing better code.

Clean Code: A Handbook of Agile Software Craftsmanship


Review Date:


by fdsvnsmvas   2018-09-13
Thanks everyone, the comments are much appreciated. Here's a list of books and other media resources recommended so far in the thread:

Robert C. Martin, Clean code:

Vaughn Vernon, various: 2

Clean coder:

Hitchhiker's Guide to Python:

John Ousterhout, A Philosophy of Software Design: This one looks particularly interesting, thanks AlexCoventry!

Kent Beck, Test Driven Development:

Dan Bader, Python Tricks: The Book:

Svilen Dobrev, various:

by anonymous   2018-05-09
Re, "...random name for test," The _[cleaner your code](, the more/better help you will get when you ask for help with it.
by igeligel   2018-03-19

Less code does not mean it is better. Code should be understandable and maintainable. If this is the case for your code you are fine. If there is a native functionality you can use, then use it carefully so the overall workflow of your code snippet is still understandable.

I can recommend you getting this book: Clean Code which explains how clean code should look like. It is Java focussed but you can transfer the knowledge to any other language too.

Also if you really want to improve your code use divide and conquer. Split out the code into managable parts and optimize from there.

by reddilada   2018-03-19

> it's hard to change things later

Can you identify why it's hard to change? Stop doing those things. Reflecting on what is the root cause of a problem is essential to improving one's self. When you have trouble always look back and ask yourself, how could I have avoided this problem in the first place?

There are a lot of practices that can help. Be organized. Be consistent.

A nice old list of guidelines to follow
Writing Solid Code
The Practice of Programming
The Pragmatic Programmer
Clean Code

by enteleform   2018-03-19

I've been coding for a few years and for a while was just focused on getting things to work.  Now I'm at a point where I know I'll figure out any given problem with my accumulated knowledge and/or additional research, and I'm noticing that not planning ahead is the most significant (lack of) action that will set me back at this stage in my practice.

To remedy this, I've been looking into architectural patterns (MVC, MVP, MVVM, etc.), and UML diagramming.

Architectural patterns were initially difficult to grasp, as many of the explanations available online dig into topics that might not make sense yet if you are inexperienced with architecture.  The MVC Java Tutorial by Derek Banas is the best introductory explanation of architectural patterns that I've come across (he also has some great videos on design patterns & other topics).
Some books that cover code structure & architecture in more detail:

The author of Clean Code, Bob Martin, has a ton of talks that are a great source of info, and has also produced the CleanCoders series.

Also check out some Software UML Examples.  I find that mapping out an overhead view of your project before starting to code gives you a chance to make sure the majority of relationships, life-cycles, and high-level details are planned out correctly from the start.  You'll still have to make inevitable adjustments while coding, but it's much more efficient than figuring it all out from the inside while you're building it.
yEd Graph Editor is a good freeware option for creating UML diagrams.
If you find that you really like the UML approach, check out Enterprise Architect.  It's definitely worth picking up since it allows you to generate file structures from your UML diagrams, with all of the boilerplate filled out already (class/method/variable definitions, etc.).

Also related, SourceMaking has some good wikis on:

(Although their UML material is pretty in-depth and may be overkill if you just want to use UML as a quick way to plan out code.  But it's legit if you want to go all-out UML mode)

by anonymous   2018-03-19

Your implementation is correct, since we could not use Transaction in Membership Provider.

You will need to make sure user enters valid data before calling Membership.CreateUser. So I'll do some type of validations before that line. Otherwise, you will end up with dangling data.

Just a minor improvement which is not related to Membership. If you pass more than 3 arguments to a method, you might want to consider using object. You can read more at Clean Code by Robert C. Martin.

Public Function CreateUser_AugmentedUpdate(ByVal user As UserModel) 

You can use ternary operator to shorten if statement.

SQLCmd.Parameters.Add("@TelNum", SqlDbType.NChar).Value = 
   If(String.IsNullOrEmpty(TelNum), DBNull.Value, TelNum.ToString)
by anonymous   2018-03-19

Microsoft has a reference document with their coding conventions which may be useful to look at, even if you aren't using C#. Beyond style and basic conventions, best practices books recommended by others here, like Clean Code, are essential reading for creating maintainable, readable code that will make sense to future maintainers of your code base.

If you're using Visual Studio (and some other modern IDEs), basic inconsistencies with recommended conventions are automatically underlined (with suggestions on hover in recent versions of VS). In VS, if your team wants to use different standards, just open the Project properties and change settings for "Code Analysis" (last left-hand menu item). More here: Using Rule Sets to Group Code Analysis Rules

by framebit   2018-02-17
2340 (the mandatory group project course) also covered a lot of this stuff, though not in as much detail.

The suggested reading for that course is a book that blew my mind when I first saw it as an undergrad: Clean Code by Robert Cecil Martin. I believe the examples are all Java, but the lessons are applicable to any language. Stuff like naming your variables more carefully than you name your children.

by Harpoi   2018-02-16

Clean code -

by Nicholas-DM   2018-02-16

After the MOOC, I'd say that you have a solid foundation.

After the solid foundation, there are a few things that you need to learn-- a two pronged approach would be good. Projects/Experience, and Theory.


Real projects, where you use what you learn. Here is a list for ideas. Do simple ones.

Post them online on Github, like a portfolio, in their own repositories. Or find a different provider. I recommend Github because it has a massive ecosystem and uses Git.

Find some opensource projects and begin doing simple things for them. Start off with contributing documentation, or a small fix, or something else simple. This is absolutely critical; getting involved and actually coding for stuff that is used by others.

Theory topics

  • Source Version Control: If you ever want to collaborate with other people, you want to be able to do so well. I recommend Git, and specifically git on the command line. -- And for learning how to use it --

  • Data Structures: How do you structure data, so that you can access it well and easily, for its purpose? You need to learn how to build data structures, and how to use them. Most importantly, you need to learn when and why you want to use a particular one. This seems like a good intro, but googling and self-study will be important here.

  • Algorithms: How do you perform tasks on your data? How do you sort it? How do you interact with it? Again, not only do you need to learn how to do the common ones, you also need to know when and why. This seems like a good overview, but someone else is likely able to suggest better resources for algorithms-- searching this very subreddit for algorithms is bound to turn up good results.

  • Clean Code: You probably don't have great style with what you write. That's okay. Most people don't. But being able to write code that others can read is an art. I recommend this book: ,

Focus on the above. If you need/want more, then go ahead and check out more.

Once you are beginning to feel comfortable-- if you plan to get a job in this field, I recommend applying at places, even as a near beginner. Try to get a few phone interviews. Go in with the idea that you won't get it, and it's just a learning experience-- you'll figure out how interviews work and you'll get some idea of what different places are looking for. Be honest with your interviewers.

If you want something else-- after you have some understanding of the above topics, this is the time to specialise. Do you want to do android development? Game development? Enterprise Software development? Begin looking into what you want to specialise in, learn their tools, and begin getting familiar with it. Above all else, experience is king.

Good luck! Ask if you have any questions, and I'll do my best to answer them.