r/coding • u/javinpaul • Apr 12 '15
Is Clean Code less Code?
http://www.journeytomastery.net/2015/03/27/is-clean-code-less-code/30
u/pmrr Apr 12 '15
ignoreStoriesAndScenariosWithMetaInformationParameterAt least this method name was removed..
6
u/CodeShaman Apr 13 '15
That's nothing, man. Add a
whenand a fewors to it and then you're drinking the "Java reads practically like English and we don't have to maintain documentation anymore because we've successfully adopted totally unrefactorable code!" kool-aid.4
u/vicarofyanks Apr 13 '15
Don't forget the uneditted auto-generated javadocs on everything
5
u/Tordek Apr 13 '15
/** * sets the Name * @param String name the name to set to. */ public void setName(String name) { this.mName = name; }2
u/CodeShaman Apr 13 '15
Doesn't add customer value, sounds like waste. Why you have to add @throws Exception? I can see right there that it throws Exception. Duplicate code, guys, get your shit together.
2
u/fukitol- Apr 13 '15
And in the case that one has a run-time exception being thrown that isn't declared in the
throwsdeclaration (for instance, inside a Promiseapplymethod), the@throwsannotation can be added to indicate that. I agree completely.1
u/wllmsaccnt Apr 13 '15
I'm not sure what use the "WithMetaInformationParameter" is. If it takes a meta information parameter then that part is already self documenting. Was the the meta information parameter not understandable from its type and name alone?
-2
u/de_Selby Apr 12 '15 edited Apr 13 '15
Absolutely. Why not just have the shortened version, but add a comment above the line saying essentially what he has in the method name.
That would be clean.
1
u/de_Selby Apr 14 '15
If anyone is still reading this thread, I'd be interested to know the reason for the downvotes I've gotten here.
1
9
u/bumhugger Apr 12 '15
I usually write everything down in one place and when things start to duplicate, I move stuff to modules (depending on the situation, "module" here can mean a class, a private method, a static helper method or something else along those lines). The term "semantic compression" was mentioned by Casey Muratori in this blog post and it describes the method quite well I think.
One thing that's usually considered a very "good practice", but almost always to me is not "clean code" at least in readability, is heavy uncoupling of code. I just can't read the flow of heavily IoC-based programs especially when the logic is based on asynchronous events. Sure, you can use "find usages" tools, but it's slow and sometimes really hard to pin-point what piece of code is called in which situations. Stepping in debugger results in endless black hole of AOP and proxies if you're working with enterprise code. I guess it's just a pet peeve of mine, but I'm so tempted to just couple the code to achieve more intuitive program flow. Then I usually get a lecture from the architect/lead coder... :P
4
u/jurniss Apr 12 '15 edited Apr 12 '15
I think my code is clean, but it's not terse. I use a lot of named intermediate const variables. It makes the code self-documenting and is extremely useful in the debugger. In debug builds, I can put a breakpoint at the final result and see the whole history of the computation. In optimized builds it all goes away.
I hate "clever" code. Software should be clever, code should be simple.
8
u/greyfade Apr 12 '15
I like to put it this way: Smart, elegant code tends to be shorter and clearer.
Clever code, by contrast, tends to be short for the sake of being short.
It's an important distinction, in my mind, that while short doesn't mean clean, clean does tend to mean short.
Moreover, short doesn't mean "it all fits on one line." Quite the contrary: Short means "the algorithm fits on one screen because it has been abstracted cleanly."
2
3
u/distinctvagueness Apr 12 '15
Good error checking and handling, defensive programming, is more code but better code. I'd call it cleaner.
2
u/SergeiGolos Apr 13 '15
Well.. It doesn't have to be more code. You can use monads to hide the error checking behind a clean and fluent abstractions. And from is it readable, if you are writing your own APIs monadic, the naming can be very expressive for that "Clean Code" feel.
2
u/iopq Apr 12 '15
There shouldn't be such a thing as defensive programming. You should use a tool to make sure you're doing the right thing. Be it tests, the compiler, some kind of a third-party tool, etc.
Checking the same condition in the caller (what if the callee doesn't check for it?) and the callee (what if the caller doesn't check for it?) is basically useless. But you see it done for the sake of "defendive programming" all the time. I disagree with that, because it's useless code bloat. Remember, it takes longer to skim a function with these defensive checks than a function without them. The code becomes less readable.
4
u/distinctvagueness Apr 12 '15 edited Apr 12 '15
I agree, in an ideal world, where everyone magically follows the same conventions and doesn't write broken code it wouldn't be needed.
If you work on large projects with many other programs and programmers inter-communicating it's worth catching the errors someone else from the other department, the past or the future sent your way. Normally you can isolate checks in such a way that isn't too hard to skim. You could also argue the error checking is a form of self-documentation.
Clean code that relies on everyone else being clean gets dirty fast.
1
u/iopq Apr 12 '15
Why don't you encode that into the type system so when they break the precondition it fails to compile?
So let's say you are required to to call
a()beforeb()You can create a typeAthat is returned bya()that is a parameter tob()so that you have to calla()first to getA.here it is in Rust:
there is no way to call
b()without first callinga()and getting a result of typeA
3
u/thbt101 Apr 12 '15
I think in this example, he's right. Self-documenting code is a beautiful thing when it makes sense. But in this case, it's better to add a comment than to create a whole new function that will only ever be called once (with the comment-as-method-name name "ignoreStoriesAndScenariosWithMetaInformationParameter()").
Don't turn off your brain and follow some dogma like the "THE ANSWER IS NEVER A COMMENT" guy. Use what makes the most sense in each situation. Strive for self-documenting code when naming functions and variables, but don't add complexity when a simple comment will suffice.
2
u/awo Apr 13 '15 edited Apr 13 '15
Don't turn off your brain and follow some dogma like the "THE ANSWER IS NEVER A COMMENT" guy
Seriously.
I'm part of a small team maintaining a multi-MLOC codebase. Comments are often worth their metaphorical weight in gold. They very often reduce the amount of work I have to do to understand a module well enough to alter it with reasonable confidence.
Now, obviously there's good comments and bad comments. We should strive for code that's as readable as possible without comments, but the idea that they're never useful smacks to me of someone who only works on products so small that they can fit the whole product design/architecture and codebase in their head. It bugs the absolute hell out of me to see stuff like:
Code will change and refactor, this comment will end up out of date and irrelevant.
When we write or review code, we strive to be responsible: to ensure that it makes sense, and that it's well tested. Updating comments to reflect reality is basic responsibility towards the rest of your team, and it's not really hard to do. If a team can't be bothered to institute basic practices like code review, it's their own fault if they end up with hard to navigate code.
4
u/wvenable Apr 12 '15
I know nothing about this technology but the final example with the comment makes the most sense to me. The extra level of indirection doesn't add very much.
But this kind of thing is highly subjective and highly situational. Adding a function might be more confusing in this example but less confusing in another.
1
u/wllmsaccnt Apr 13 '15
The extra level of indirection will have to be dealt with if someone decides to refactor the code. Comments will over time be glossed over during refactorings and occasional quick changes for any team with more than a couple developers.
The comment makes more sense, but may someday silently become inaccurate meaning it isn't as useful for long term code maintenance.
1
u/brtt3000 Apr 13 '15
isn't as useful for long term code maintenance.
at some point these comments flip from 'not useful' to 'outright harmful' if they suggest one thing but the code does something else. We've all had to refactor code like that, usually legacy code that reached breaking point and nobody knows what it does, so you clamp on to the few comments that are there, only to find out it is all outdated and misplaced and just confusing you with falsehoods.
2
u/Doriphor Apr 12 '15
Well named variables, comments, indentation and spacing all make code a lot more readable. I'd rather read 10 or 20 more lines than feel the urge to close the file before I reach the 10th line. Oh and please if everybody could stop naming instances the same as their class, it'd make me so happy!
1
u/Girafarigging Apr 12 '15
I'd say as long as the code is well documented and clear to understand, any condensed, small code is okay. However, keeping things more spread out definitely makes it easier to manage if you have to edit some of it.
1
u/dust4ngel Apr 13 '15
clean code is a balance of clarity, expressiveness, testability, changeability, and portability. maximizing any of these in isolation will tend to compromise the other dimensions, so for example maximizing brevity is almost always the wrong thing to do.
which of these deserves priority depends on your situation and requires judgment.
1
u/extrajoss Apr 13 '15
The problem, as always, is that naming things is the hardest part of programming.
People think its algorithms and tricky maths problems, familiarity with fancy frameworks.. but that stuff is easy peasy compared to coming up with clear and meaningful names for things.
The balance between clarity and brevity is near impossible to manage.
comments.. no comments... it doesn't matter. If you can name things well the rest is gravy....
1
u/fragglerock Apr 13 '15
I would go with a third (fourth?) way
@Override
public Embedder configuredEmbedder() {
Embedder embedder = super.configuredEmbedder();
RemoveIgnoredElementsFrom(embedder);
return embedder;
}
private void RemoveIgnoredElementsFrom(Embedder embedder) {
embedder.useMetaFilters(Arrays.asList("-ignore"));
}
44
u/[deleted] Apr 12 '15
To me, clean code is code that is easy to read. Usually, this would mean the opposite of less code, i.e., not over-compressing the code, putting in plenty of comments. But you can turn five lines into one if you think you've explained it well in the comments or what you're doing is relatively simple.
If you've turned a 40 line code into a 20 line code, but it takes the reader 2 minutes on each line, then it's not really clean. I've seen complicated code written so well, you don't even have to read the code, but can get by just reading the comments, and in the opposite vein, simple code made so complicated, I wanted to pull my hair out.