Live data from Hacker News

There’s No Such Thing as Clean Code

steveonstuff.com

191–200 of 395 posts

Re: There’s No Such Thing as Clean Code

#191

So True ! After 25+ years of coding, I know one thing. I STILL don't know how to "code correctly". And apart from a few gifted individuals (Rob Pike, Fabrice Bellard Bobby Bingham [ffMpeg team] etc) I'm HIGHLY suspicious of ppl and programmers who claim "they can program correctly" and that "this xyz is the correct way/stack/method/arch". Background:CS grad, start coding at around 13 (thank you dad !) I am well verse…

> PS2. Oh and on 'code reviews': It would be cool if code-reviews were done "anonymously"

Outside of work in the free and open source community, I find myself often wishing that I could just get a random person who has no context of the codebase to review a piece of code I've written.

Context is obviously important, but the lack of context can also highlight issues that you would be blind to if you have the context.

I would want this to exist within an ide where you could highlight code, submit for review and wait. And if you want to review you can explore pieces of code to review filtered by language within the ide.

Re: There’s No Such Thing as Clean Code

#192

So True ! After 25+ years of coding, I know one thing. I STILL don't know how to "code correctly". And apart from a few gifted individuals (Rob Pike, Fabrice Bellard Bobby Bingham [ffMpeg team] etc) I'm HIGHLY suspicious of ppl and programmers who claim "they can program correctly" and that "this xyz is the correct way/stack/method/arch". Background:CS grad, start coding at around 13 (thank you dad !) I am well verse…

I think there is another big issue related to Clean Code (and friends) that is rarely discussed.

There are two types of design methods: those that help you to generate a solution to the problem and those that only help you to optimize a solution, in some dimension, once you have one. Clean Code guidelines clearly belong to the latter.

When you first write code you should not think of Clean Code. It will distract you from solving the problem at hand, while not providing any help in solving it. Only after you have a correct working solution you should consider Clean Code and other optimization guidelines. (Code Complete is much better IMO.)

Knowing when often pays better than knowing how.

Re: There’s No Such Thing as Clean Code

#193
post #151
post #110

Earlier quoted context omitted.

Seems garbage advice to me. Now you've polluted your production code, likely forever. I don't see a situation where using an issue tracker + good commits - e.g. 1 commit for the 1 PR linked to the ticket that removes the feature - won't result in a superior situation from a maintenance and traceability perspective.

Sure, in a normal world. But with 2 devs in team, handling a clinical information system in very rapidly changing environment (new features), there is no time to document things in this way.

Jump ship then.

This sounds like you're travelling towards burnout at supersonic speed.

Rapidly changing environment/features is just newspeak for customer has no idea what they even want in the first place and project management is non-existent.

Re: There’s No Such Thing as Clean Code

#194
post #171
post #118

Earlier quoted context omitted.

The flag check could be deep within a loop, how would you split it then without repeating the whole method?

Make the loop operation something that's passed into the method. Or maybe replace the flag with polymorphism if there's some logical value that it really belongs on.

The second one is impractical because the number of classes you'd need would increase exponentially with each added flag. The first one clashes with

> Prevent over-configurability,

because you're accepting an infinite range of possible callback methods instead of asking a simple yes/no question.

I don't get why this rule even exists, actually. If you take three boolean arguments, that's not okay, but if you wrap them all into a configuration object and pass that instead, that's suddenly... okay?

Re: There’s No Such Thing as Clean Code

#195
post #120
post #96

Earlier quoted context omitted.

> If you follow them you will end up with code that's really nice to read and easier to maintain, and, most importantly, that you can confidently change. I found that a lot of those guidelines lead to the exact opposite. Examples: - Prefer polymorphism to if/else or switch/case (oh, the joy of tracing a simple task through 50 files) - Use dependency injection (same as above) - Hide internal structure (that "private"…

> One assert per test I read that as "one operation per test". Specifically, both: assertEqual(complex(1, 2) + complex(3, 5), complex(4, 7)); and: auto c = complex(1, 2) + complex(3, 5); assertEqual(c.re(), 4); assertEqual(c.im(), 7); would both satisfy that requirement, but: assertEqual(complex(1, 2) + complex(3, 5), complex(4, 7)); assertEqual(complex(1, 2) + complex(-3, -5), complex(-2, -2)); would not. -- In that…

>I read that as "one operation per test".

This replaces a clear, well defined thing (assert) with a vague, ill defined thing. What constitutes an "operation"?

There's almost no point to the rule if it isnt trivial to distinguish one operation from a group.

Re: There’s No Such Thing as Clean Code

#196

Earlier quoted context omitted.

Hmm...polymorphism is almost invariably simpler than if/else or switch/case, so not seeing the contradiction.

Is that view not subjective though? Might someone think that if/else is simpler?

Depends entirely on the metric used.

Cyclomatic complexity would put if/else behind polymorphism while using lines of code and depth of inheritance would favour the if/else way.

That's the crux with software - there's not always an objectively better solution in terms readability/comprehensibility and some metrics that try to capture these qualities are inherently at odds with each other.

Re: There’s No Such Thing as Clean Code

#197
post #173

Earlier quoted context omitted.

Back in university I had an experience that was really instructive. For a project we had to write a program that differentiates mathematical equations. I dove in and just started writing code. Eventually, I realized that I had made a design error and that my code was much more complicated and cumbersome than it needed to be and I was getting stuck due to the complexity of the monstrosity I created. Unfortunately I fi…

Yeah this just hasn't been my experience. If you're working on a house you measure twice and cut once because the cost of reworking physical materials is a lot more expensive than the cost of doing a second measurement. If you could delete half your house and re-build it at zero cost, it might be more valuable to just go for the first attempt and learn from it rather than trying to do everything in theory up front. I…

> "If you could delete half your house and re-build it at zero cost, it might..."

I look forward to that day when atoms are commanded like bits!

Re: There’s No Such Thing as Clean Code

#198

Earlier quoted context omitted.

In the literal sense private is default though. I wonder why. Lexical sorting of private, protected and public? It makes almost no sense to have all private class fields and methods. The only thing I can think of is fat handles with friend classes or functions.

In c++ a class declared with the keyword 'class' is private by default, and a class declared with the keyword 'struct' is public by default. So if you want public by default just write 'struct' (somehow this is not commonly known). https://en.wikipedia.org/wiki/C%2B%2B_classes

I think the “somehow” is not too hard to guess… it is easy to think that “class” is the only way to declare a class, especially since “struct” is taken from C. I’m sure very few intro C++ explanations ever mention “struct” as a way to declare a class.

Re: There’s No Such Thing as Clean Code

#199

Earlier quoted context omitted.

Haha true true ! Maybe he is a cross-spectrum coder ?

From What I understand Bellard's code is fairly "dirty". FFmpeg, as I understand it, was hard to modify, maintain, or integrate with. Don't get me wrong, he's a fucking wizard. He's Mozart when the most talented of us can only ever hope to be Salieri. But part of that is, he dashes off brilliant code without much thought to its maintainability, then leaves for the next project.

Yea sorry, I guess I could have picked my "heros" with more care maybe add in a John Carmack ? :)

>Don't get me wrong, he's a fucking wizard. He's Mozart when the most talented of us can only ever hope to be Salieri. But part of that is, he dashes off brilliant code without much thought to its maintainability, then leaves for the next project.

Absolutely agree's - Well that is "maybe" one unpopular-metric we need to consider as well ? - How much brilliant code can you ship that has a big impact ? I could spent 1 year to "correctly-code some system" but if no one cares or uses it.. does the code or the "better quality" even matter ?? #BigPictureKindOfWay ?

To be clear, I have no idea what is the correct answer here :)

Re: There’s No Such Thing as Clean Code

#200
post #173

Earlier quoted context omitted.

Yeah this just hasn't been my experience. If you're working on a house you measure twice and cut once because the cost of reworking physical materials is a lot more expensive than the cost of doing a second measurement. If you could delete half your house and re-build it at zero cost, it might be more valuable to just go for the first attempt and learn from it rather than trying to do everything in theory up front. I…

When can you rebuild at zero cost? I have made similar avoidable mistakes of not thinking it through enough, could have saved me a lot of rewriting, which was pretty expensive

Probably not literally without cost, but if the code was written with disposability in mind combined with just a little bit of pre-planning, then rewriting or refactoring should be indeed trivial.
Post reply on HN