Live data from Hacker News

There’s No Such Thing as Clean Code

steveonstuff.com

231–240 of 395 posts

Re: There’s No Such Thing as Clean Code

#231
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"…

Private - or visibility scoping generally - is absolutely essential when you program in an environment with multiple teams. In its absence other people take dependencies on internal details which makes code much harder to refactor later. Lack of scoping on visibility increases the surface area of your APIs and increases the likelihood of bugs. Lack of scoping of visibility on state means losing almost all control ove…

I agree. What I'm saying is that private is a poor default.

Re: There’s No Such Thing as Clean Code

#232

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…

> It would be cool if code-reviews were done "anonymously" Sometimes during code-reviews I find myself wanting to leave 100+ comments. Instead I settle for the few major ones and leave behind most of the minor stuff. I don't want to be perceived as someone who is difficult to work with, so try to pick my battles wisely. Wonder if anonymous code-review that helps with that.

I don't know, if coding is art, then the reviews need to be more personal. Anonymous art criticism usually isn't useful. You'll have to filter too much noise to get a signal. If you have an art mentor who knows you and your work, their feedback is much more useful.

An anonymous code review might only be to make sure everything technically works (as securely as possible).

Re: There’s No Such Thing as Clean Code

#233
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…

As someone who is currently over a year into rewriting a massive system with a fundamental design error by the original designer I can assure you that failing to plan your data model up front can have huge costs not just for you but for anyone who picks up your code in the future, and can hamstring a system so that it is impossible to extend or evolve.

Re: There’s No Such Thing as Clean Code

#234

I like to measure things. Like token count, line count, variable count, method parameter count, indentation level and grokking time. Clean code minimizes these measurements. To minimize variable count I can use ternary expressions. To minimize indentation level I think carefully about early returns or extracting blocks of code to functions that do nothing if condition is true (early return). To minimize grokking time…

Sometimes, (maybe the majority of times) a five liner is clearer than a one liner. I think line count is a terrible metric to use.

Agree about indentation.

Grokking time, and time to make a correct, meaningful change are good metrics.

It’d be interesting to take some of the examples from Clean Code, and rewrite them procedurally, then functionally, etc, and then test which takes the least amount of time for an average developer to understand and modify.

Re: There’s No Such Thing as Clean Code

#235
The post showed me why my code is praised an understandable but I have the absolute worst time when it comes to testing it!

The interfaces and abstractions needed to make a code testable is something I actively avoid in order to write code that others (including me in a few months) can glance and quickly-ish figure out what is happening and if there’s a big, where could it be.

Now I can intentionally know that is a compromise I’m taking instead of feeling like a failure because my code doesn’t lend itself to testing…

Re: There’s No Such Thing as Clean Code

#236

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…

> It would be cool if code-reviews were done "anonymously" Sometimes during code-reviews I find myself wanting to leave 100+ comments. Instead I settle for the few major ones and leave behind most of the minor stuff. I don't want to be perceived as someone who is difficult to work with, so try to pick my battles wisely. Wonder if anonymous code-review that helps with that.

I do the nitpicking reviews but, when I review someone for the first time, I specifically point out that I leave nitpicking reviews and don't block approval on most of them.

Re: There’s No Such Thing as Clean Code

#238
post #235

The post showed me why my code is praised an understandable but I have the absolute worst time when it comes to testing it! The interfaces and abstractions needed to make a code testable is something I actively avoid in order to write code that others (including me in a few months) can glance and quickly-ish figure out what is happening and if there’s a big, where could it be. Now I can intentionally know that is a c…

Well, not lending itself to testing really is a big problem. But one does not need to sacrifice much in terms of understandability to make code testable. For instance, one does not always need an interface. You could also just inherit from the thing that you want to mock. Another problem that might be going on here is that you might be trying to test on a level that is too low. I think it is in many cases not a good idea to test individual classes and methods. Often it is better to test some small number of classes working together. The problem with testing one single class or method is that you are likely testing implementation details that are very much subject to change and also that at some point one is testing trivial things like 'is the standard library of my language still capable of adding items to a container'. When testing at a bit of a higher level one can write tests that are about properties of the software that are actually valued by the customer. These are much more likely to be stable. If one tests at a level that is a bit higher, one also needs to mock fewer things so the interface thing is also less likely to be much of a problem.

Re: There’s No Such Thing as Clean Code

#239
post #41

Earlier quoted context omitted.

20 short functions definitely sound as though they should be explicit. Named, documented, testable. 1 or 2 you could get away with being implicit. 20 requires a lot of understanding as to what's going on!

So here's some TypeScript code I just made up, with a lot of lambdas. It's somewhat typical of code I write all the time. books .join(authors, book => book.author, author => author.id) .filter(([book, author]) => author.lastName === searchText) .map((book, author) => `The Book ${book.title}, by ${author.fullName()}, has ${book.chapters.count()} chapters, totaling ${book.chapters.sum(chapter => chapter.pages.count())}…

This is the truth. Peace be with you.

Re: There’s No Such Thing as Clean Code

#240
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"…

I agree with my whole heart. I think you forgot one of the most devastating points in Clean Code, namely short functions and DRY (when overdoing it). It leads to the same problem "jumping through 50 files". My favorite subversive action to write clean code is using jumps. There is no end to how convoluted code people write to avoid the oh so harmful leap. I feel like much of the need for short functions comes from ne…

Could you provide an example of how you would restructure some nested ifs to jumps? I can kind of picture it but can also imagine doing it badly and being in pain.
Post reply on HN