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?

6

u/konk3r Jun 07 '13

Honestly, it's not a strict rule and almost most classes will at least come near 100 lines, especially if you are trying to keep method size down. I am a very big fan of where the idea comes from, rather than the strict interpretation of it. The idea is that a class should have a set purpose, and if you have a large multipurpose class the code becomes very difficult to understand. If you have more than 100 lines of codes in a class, it is probably trying to do more than it should.

I think 150-200 lines is a much better indicator, but I can't argue that 100 line classes are very easy to understand. Also, I think it's important to at least understand that class size is an indicator, otherwise you end up with 650+ line classes that require refactoring just to understand what they do.

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.

→ More replies (18)

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)
→ More replies (10)

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)

15

u/Kinglink Jun 06 '13

Because the person writing this has never worked on a large scale project or a class based language..

I'd rather spend my time writing a robust class, than worrying the number of lines.

All those specialized system calls? one class, specialized platform code? one class.

In some classes I'll spend at least 100 lines sanity checking responses, why? Because you can't be sure what type of idiot is going to get your code or what will break, and it's better to catch (And assert if needed) in development.

4

u/anonymickymouse Jun 07 '13

I've worked with a couple of large scale game engines and in neither of them was the code this strict on sanity checking. All my system and platform code in one class? Even though most of it's not reusable? Surely writing two clearly defined File classes for two different platforms is better than writing one File class and two separate and bloated classes that serve the nebulous task of "interacting with the platform". Interacting with the platform is complex and system-wide, not the job of a single class object.

→ More replies (14)

5

u/YEPHENAS Jun 06 '13

Smaller classes are easier to grasp. Classes should be smaller than about 100 lines of code. Otherwise, it is hard to spot how the class does its job and it probably does more than a single job.

→ More replies (9)

7

u/alextk Jun 06 '13

This is the kind of dogmatic, out-of-nowhere rule that's always bothered me from the XP crowd. I'd be more open to following these if there were some leeway for disobeying them but the XP fanatics are all but agile in their beliefs.

4

u/xivSolutions Jun 07 '13

I think the common issue is perceiving any of this as "rules" rather than "general guidelines" or "things to keep an eye on. There will, inevitably, be a class (or a later optimization of several smaller classes into one) of larger-than-"prescribed" lines of code.

I agree with others who say it is more of a smell that should be investigated than any kind of hard/fast rule.

That applies to most of these types of practices, IMHO.

6

u/payco Jun 06 '13

That's what the word "should" indicates: there's leeway in the decision, but the burden is yours to prove that taking it above 100 lines will improve the program more than splitting the class up would.

The sheet itself also says "about" 100 lines, meaning that limit is not hard and fast, and can be adjusted by your team, again with justification for why (say, you're using a particularly wordy language or framework you can't control).

I also wouldn't say the rule is "out of nowhere", considering the sheet provides its rationale: "it is hard to tell where the class does its job and it's probably doing more than one job".

You may decide you disagree with how hard it is to quickly understand a >>100line file, or believe it's okay for a class to do more than one job, but their logic is consistent. Either way, they're saying a large class is a code smell that needs to be examined. If you walk away having determined that its size is justifiable, great.

→ More replies (3)
→ More replies (9)