Live data from Hacker News

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

read.engineerscodex.com

61–70 of 204 posts

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

#61
post #39
post #2

Here's an old joke about the progression from junior to mid-level to senior developer: Junior dev: My code is simple, straightforward, and easy to understand. Mid-level dev: My code is clever, innovative, expressive, hyper-optimized, and ingenious. Senior dev: My code is simple, straightforward, and easy to understand. In software development, "clever" solutions are like poems. In the best poems, there are usually mu…

Reality: This is mostly because mid-level dev needs to justify their existence in order to not get laid off or PIP and is worried about losing their H1B and having to uproot their entire family in 60 days notice. Hyper-optimized, hard-to-read code that only they understand is one way to increase reliance on them while giving a reason that can be put into a promotion doc. Mid-level jobs are worried about maintaining t…

This is mostly because mid-level dev needs to justify their existence in order to not get laid off or PIP and is worried about losing their H1B and having to uproot their entire family in 60 days notice. Hyper-optimized, hard-to-read code that only they understand is one way to increase reliance on them while giving a reason that can be put into a promotion doc. Mid-level jobs are worried about maintaining their job.

That seems to be a matter of survival overriding ethics and professional pride. I know I came up with some really cringeworthy and complex solutions as a mid-level dev, but I would never have done it deliberately.

If people are put into a situation where they need to purposefully write substandard code to not get deported, it's something that needs to be fixed.

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

#62
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

Traditional C style for is terrible compared to what you can do with iterators in my opinion, but idk if C++ has those.

    list.iter().sum()
ezpz

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

#63
post #43

Earlier quoted context omitted.

One pet peeve of mine is people using std::for_each instead of a simple loop

Same in JS. I once worked with someone who would use [].forEach and didn't like for loops because functional good, iterative loops bad. It was essentially in the coding style of the project and the for version would be rejected in MRs. I don't miss this. IMHO [].forEach just essentially expresses the same thing as a regular for loop, although in a more verbose and convoluted way that is also likely very inefficient b…

You are making a strong evidence-free claim about what JS compilers don't do.

https://stackoverflow.com/questions/9981607/array-foreach-ru...

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

#64
post #59

Earlier quoted context omitted.

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...

[deleted]

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

#65
post #10
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

We now have return std::reduce(x.begin(), x.end()); Which is a little cleaner and is even faster (compiler is free to do the additions in any order). https://en.cppreference.com/w/cpp/algorithm/reduce - it looks like even the `accumulate` example can be made simpler with `std::plus`. I prefer the `reduce` option for a number of reasons, but understand why someone might not.

>the `accumulate` example can be made simpler with `std::plus`

Accumulate, surprisingly enough, accumulates by default:

    return accumulate(x.begin(), x.end(), 0);

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

#66
post #59

Earlier quoted context omitted.

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...

So you can't use IEnumerable.Sum if T is a custom type implementing operator+ ?

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

#67
post #4

I also find that, in C++, int sum = 0; for (int i = 0; i is a lot easier to understand than return std::accumulate(x.begin(), x.end(), 0, [](int a, b) {return a + b;}); Yet, the latter is considered more correct and better, with static analysis like cppcheck telling you to use the latter. It does have many advantages, like no mutable variables lying around, but gee it is annoying to read.

I haven’t written any C++ since the 90s. That is unrecognizable. Is that an anonymous function?

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

#68
Everyone thinks they're writing good code. Even many experienced seniors write code that they think is good code but others loathe. Best case, they can at least solve problems without introducing new ones; though more often than not, they're still making many junior mistakes while couching them in senior terminology.

Aside: Do we really need to keep rewording this exact same essay every month since the first computer program was ever written?

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

#69
There is always some part of subjectivity when evaluating complexity and clarity.

But all things being equal, this point still stands and should be not being brushed off as "skill issue".

Clarity is a difficult skill to acquire, but it pays off.

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

#70
post #68

Everyone thinks they're writing good code. Even many experienced seniors write code that they think is good code but others loathe. Best case, they can at least solve problems without introducing new ones; though more often than not, they're still making many junior mistakes while couching them in senior terminology. Aside: Do we really need to keep rewording this exact same essay every month since the first computer…

This one was a bit different. It also featured an extremely dangerous and incompetent manager.
Post reply on HN