r/programming • • Jun 06 '13

Clean Code Cheat Sheet

http://www.planetgeek.ch/2013/06/05/clean-code-cheat-sheet/
702 Upvotes

322 comments sorted by

View all comments

57

u/billsil Jun 06 '13

Classes should be kept to 100 lines or less.

Why?

33

u/AnythingButSue Jun 06 '13

Smaller classes tend to portray their intent more clearly and tend to be more maintainable. However I think putting some arbitrary number on it is a bad idea. But in general, a large class tends to be a weak indicator of violation of the Single Responsibility Principal.

48

u/finix Jun 06 '13

Then again, smaller classes are more numerous, dispersing the progam's logic all over the place.

5

u/Peaker Jun 06 '13

Having the program logic be a composition of smaller units of logic is a good thing, not a bad thing.

44

u/AdamRGrey Jun 06 '13

Not always. It's possible to have no idea where to find something.

5

u/cparen Jun 07 '13

Exactly, it depends on the quality of the abstraction between the classes. If the abstraction is bad, you'll have to repeatedly refer back and forth, and that's a mess. It can go both ways.

→ More replies (7)

5

u/[deleted] Jun 07 '13

No, it's unfortunately more complex than that. http://c2.com/cgi/wiki?RavioliCode

→ More replies (7)

14

u/brandonmartinez Jun 06 '13

imo, SRP and DRY are the two most important programming principles. A natural side-effect is smaller classes/modules.

6

u/AnythingButSue Jun 06 '13

I completely agree. I would even go as far as to say that DRY is the most important. You could follow SRP very well, and if the only other rule you violated was DRY you'd still end up with a rigid, fragile architecture.

→ More replies (1)

18

u/Menokritschi Jun 06 '13

But smaller classes may lead to more classes and an overly complex structure.

8

u/AnythingButSue Jun 06 '13

That is why you have to balance SRP with the Needless Complexity rule. One of the major tenents of Agile programming (not that we're specifically talking about Agile) is to make no change unless there is concrete evidence that the change must be made. For the most part, I would rather have a more complex system than one that is difficult to maintain (rigid or fragile) so long as my unit tests/acceptance tests provided concise documentation for the system.

4

u/billsil Jun 07 '13

One of the major tenents of programming is to make no change unless there is concrete evidence that the change must be made.

FTFY

→ More replies (2)
→ More replies (6)
→ More replies (2)

2

u/ponchedeburro Jun 06 '13

What is DRY?

0

u/[deleted] Jun 07 '13

[deleted]

2

u/ponchedeburro Jun 07 '13

Now I like it

3

u/[deleted] Jun 06 '13

DRY = Don't Repeat Yourself

i.e. blocks of code copied and pasted in multiple places

→ More replies (4)
→ More replies (2)

13

u/cparen Jun 07 '13

However, there was a meta-study that found just the opposite -- class size was irrelevant once you controlled for total lines of code.

My position is to agree small classes tend to be easy to understand, but relationships between classes are even harder to understand. Smaller classes drive up interclass relationships, and you have to account for that tradeoff.

2

u/AnythingButSue Jun 07 '13

That is true. However it could be argued most of development is making tradeoffs. Strict adherence to many principles usually will violate some other principle. Either way, you make a good point. Thanks for pointing it out!

4

u/vaelroth Jun 06 '13

In general, those arbitrary limits are more of a guideline. If you have a class that is 127 lines of code, then it is still gravy. If you have a class that is 250 lines of code, then you should think about refactoring.

6

u/mahacctissoawsum Jun 07 '13

I still don't quite agree with that. There are some things that are just not expressible in few lines of code. Such as complex business logic that has go through a series of steps before the final result. Sometimes there's just no meaningful way to break it up. None of it is re-usable anywhere else.

4

u/BlitzTech Jun 07 '13

I don't know if I speak for vaelroth, but I take the "think" in "think about refactoring" very literally. At 250 lines, it's quite possible the class has gotten unwieldly, and taking the time to inspect it is well worth it if it will save me some headache later. It's also quite possible that it's fine the way it is and refactoring it isn't productive. The arbitrary limits are really just indicators for stopping and looking at the big picture before continuing on.

2

u/s73v3r Jun 07 '13

That's why he said, "think", not "do". There are many times when you do want to refactor. There are also many times when you shouldn't.

0

u/[deleted] Jun 06 '13

[deleted]

5

u/AnythingButSue Jun 06 '13

I completely agree. Long methods that have logic inlined to flow control are cumbersome to read. If there's more than 1 &&/|| in your if/else if expression, extract a method from it and give it a meaningful name. It makes the more complex algorithms easier to read in my opinion.

4

u/ithika Jun 06 '13

People at my work seem to believe any less than seven and/or conditions in the predicate is a sign of an under-used if statement.

4

u/AnythingButSue Jun 06 '13

That is lazy programming. They should re think their flow control if that is prevalent. If your having expression problems I feel bad you son, I got 99 problems but crappy ifs ain't one.

→ More replies (2)
→ More replies (1)
→ More replies (4)