Live data from Hacker News

Clever code is probably the worst code you could write (2023)

read.engineerscodex.com

51–60 of 204 posts

Re: Clever code is probably the worst code you could write (2023)

#51

I think “clever” is more related to unfamiliarity. There is actually a lot of cleverness going on that people just become familiar with. Structured programming is actually very clever if you think about it. Function calls are, when you look at it closely, very clever. It encapsulates how to jump to a function entry point, how to pass on values in registers or in memory, how to adjust stack pointers, and all other sor…

Yes. A relevant quote from Alan Kay in his talk “the power of simplicity”:

> one of the things that's worked the best the last three, or four hundred years, is you get simplicity by finding a slightly more sophisticated building block to build your theories out of. its when you go for a simple building block that anybody understand through common sense, that is when you start screwing yourself right and left, because it just might not be able to ramify through the degrees of freedom and scaling you have to go through, and it's this inability to fix the building blocks that is one of the largest problems that computing has today in large organizations. people just won't do it

https://youtu.be/NdSD07U5uBs

Re: Clever code is probably the worst code you could write (2023)

#52
post #25
post #16

This is true, but in code reviews and such, it often boils down to familiarity above anything else, like someone preferring names = [] for record in records: names.append(record["name"]) to names = [record["name"] for record in records] Now, I might say something if I saw this: import operator names = list(map(operator.itemgetter("name"), records)) Seems a bit unidiomatic given that list comprehensions are in the lan…

Great example. My personal preference is the first one, but I put that down to me being inexperienced and not working in a professional development environment. I've been using Ruff with most of the rules enabled, and they have a page for this scenario here: https://docs.astral.sh/ruff/rules/manual-list-comprehension/ Another one that trips me up is the ternary operator: https://docs.astral.sh/ruff/rules/if-else-bloc…

Let me see if I can sway you.

The nice thing about both the ternary and the list compression is that they become statements of the form derived_thing = some_computation. The code flows better when you’re skimming it at a high level and thinking “then we get this”, “then pluck this”. You can think more about your reformed data and less about how it was reformed.

The alternative is that the branching obscures what you’re trying to create at each step.

Sometimes I’ll even use 2 list comprehensions instead of a loop (even though it’s slower) because it’s clearer to read something like:

    odds = […]
    evens = […]
That’s my experience, anyway.

Re: Clever code is probably the worst code you could write (2023)

#53
I keep to a simple rule: smart code is an asset, clever code is a liability.

Smart code is usually simple and clear, while also being short and efficient. Achieving this is not easy, but reading, understanding, and using smart code is easy. (Otherwise it's not smart, but, well, ordinary.) An example of smart code for me would be the merge sort algorithm, or the Lisp interpretation loop.

Clever code is usually some kind of last-resort hack, a trick, applied in dire straits, or to achieve a unique effect. An example would be the inverse square root hack, or the Duff device.

Beside the actual writing time, code review time is good for making code smarter and less clever.

Re: Clever code is probably the worst code you could write (2023)

#54

Earlier quoted context omitted.

C++ makes this (and many other things!) needlessly painful. In C# it is just return numbers.Sum();

Where did "numbers" come from, and why are you so sure you can Sum() it? The original C code offered has some data structure (perhaps an array?) called x. Do C# arrays have a Sum method? I don't think so. In Rust you would probably just write: x.iter().sum()

This comment makes no sense. You dismissed the C# code, and then wrote the same thing in Rust, but with an extra non-conceptually-meaningful boilerplate step.

May as well ask, "where did x come from, and why are you so sure you can iter().sum() it?"

C# has generic types, so yes, C# arrays of numbers have a Sum method.

https://stackoverflow.com/questions/2419343/how-to-sum-up-an...

Don't make bold dismissive comments about things you are ignorant of. It makes you look Blubby.

Re: Clever code is probably the worst code you could write (2023)

#55
This is true. That's why the software engineering recruitment process is completely broken... They select for people who revel in unnecessary complexity.

What kind of person enjoys spending their time practicing many variations of pointless complicated coding puzzles instead of working on useful side projects or learning new concepts and technologies?

The big tech recruitment process is all about puzzle solving under time constraints though. Pragmatic engineers have no chance of competing against the puzzle solvers.

Re: Clever code is probably the worst code you could write (2023)

#56
A bit of a clickbaity title, but I'll bite

I think "cleverness" is a function of your (and your team's) experience in a particular language / domain space

I worked with teams where verbose Java is considered "simple, straightforward, and easy to understand"

I also worked with teams where terse Haskell is considered "simple, straightforward, and easy to understand"

and of course these codebases look nothing alike

Re: Clever code is probably the worst code you could write (2023)

#57
post #24

I would argue that what constitutes clever code varies a lot by language. There's always a "cleverness" threshold where being able to read or refactor the code becomes harder, but this threshold isn't universal. Python in particular makes it very easy to be too clever, since its extremely rigid syntax was designed specifically to discourage it, but it ended up giving the user the necessary tools to be clever anyway,…

What would qualify as clever Python? These kind of broad and vague statements make me wonder if I am guilty..

Re: Clever code is probably the worst code you could write (2023)

#59

Earlier quoted context omitted.

Where did "numbers" come from, and why are you so sure you can Sum() it? The original C code offered has some data structure (perhaps an array?) called x. Do C# arrays have a Sum method? I don't think so. In Rust you would probably just write: x.iter().sum()

C# arrays don't. However, C# arrays are IEnumerable , which does.

IEnunerable of course does not have Sum.

Only collections of numeric types (including arrays) have Sum.

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...

Re: Clever code is probably the worst code you could write (2023)

#60

I wonder how old this advice is. I know that it predates the century but I'm not sure how far back. (A favorite quote of mine that I believe is from the 1970s says "It's easier to make working code fast then to make fast code work." Since the fundamental problems of programming have not changed over the centuries, I wouldn't at all be surprised if there's an anti-clever saying from the dawn of computing)

> the fundamental problems of programming have not changed over the centuries

It hasn't been a whole century yet. Grace Hopper's career started in 1944, eighty years ago.

Post reply on HN