3 ms·
I think there's a difference between "clever" and complex. You can express a complex algorithm or pattern with simple easy to understand code - complexity does
by HarHarVeryFunny 2y ago
I think there's a difference between "clever" and complex.
You can express a complex algorithm or pattern with simple easy to understand code - complexity doesn't have to manifest itself as unreadable or incomprehensible code.
To me "clever" code is more about they way you are doing something than the complexity of what you are trying to do. Clever is the opposite of straightforward and easy to comprehend without a detailed explanation.
For example you might be "forced" to write clever code as an optimization to calculate something in a non-obvious way, maybe also based on some non-obvious pre-conditions that have been assumed and make this a valid approach.
You don't normally want to write "clever" code - you want to write easy to understand straightforward code, and on the occasion when you feel compelled to a clever implementation, for the sake of future you or your teammates, you better precede it with a block comment prefixed with "here be dragons" and a detailed explanation of what it is doing and why it is doing it in this non-obvious way.
- dotnet00 2y agoI also prefer this perspective. I had an epiphany about this sort of code when I was trying to describe what my code did for a research paper, and in trying to express why I was proud of it, I called it complex, only for my research advisor to point out that complexity was not the point of the work, so calling it complex did not convey what about it made the research interesting. Although it wasn't his intention, it changed my perspective on the "clever" tricks I liked using, since it made me realize that being clever was not the kind of complexity that mattered. So, nowadays I try to write simple, easy to understand code, leaving the cleverness and complexity to the way the problem is tackled.
- SOLAR_FIELDS 2y agoJust to codify this with some examples: Here’s some recent examples of what I consider “clever” that I’ve had to work with from previous people and that I’ve written myself: Someone who loved Lisp wrote a bunch of the unit test suites where I work using Python in a very clever metaprogramming way. They would dynamically generate and attach functions to a test object for testing REST requests. This is both 1. Difficult to read and understand 2. Much more difficult to test the behavior of All to save probably maybe 100 lines of code. This is an example where I feel like code is too clever for its own good without having a good reason to be like that. It also flies in the face of what you would conventionally expect when it comes to Python unit test suites. One example where sometimes it’s necessary to be clever: I did a db migration in about 100 lines of Python/SQL that worked fine at small scale using Alembic/Python/SQL and was a straightforward update with CTE. When tested on large production grade dataset however it completely fell apart. Some clever hacks with batching and temp table later and I have something runnable, but now it’s all in sql and while well commented is much harder to grok what’s going on at first glance.
- jiggawatts 2y agoWe've built modern computing on top of encapsulated cleverness. I don't mind clever code, as long as it's a polished gem set into a nice abstraction hiding away the details. Databases have clever code in them. Network stacks have clever code. Heap allocators have clever code. We've built our simple code on top of these. If you need some clever code, write it in the same style: a self-contained library with clean abstractions and thorough documentation. Whatever you do, never "weave" clever code through simple business logic!
- rightbyte 2y ago> Whatever you do, never "weave" clever code through simple business logic! Oh I love that blog series.
- _heimdall 2y agoI've always considered code to be "clever" when its using something in a way it is rarely used, and usually specifically with something very common like an arithmetic operator. I've seen some really clever code using arithmetic operators to flip variables in ways that look like magic at first. I've also seen, and used myself, a few similar kinds of tricks in JavaScript especially when working with booleans. I never really considered code "clever" when its just unreadable or incomprehensible. IMO that's just bad code.
- bexsella 2y agoI work with embedded DSPs, and there are certainly points where the maths gets incredibly dense and hard to intuitively parse. Your last point has proven true for me many times. Luckily, with the exception one block of code, the maths is detailed plainly above the implementation. So, while there might be an occasion where I might not fully understand what's going on, I can see where the original author of the code was coming from, and can follow the following code accordingly. Indeed, I believe one of the comment blocks does start with "Here be dragons".
- dkarl 2y ago> You don't normally want to write "clever" code - you want to write easy to understand straightforward code I think what the parent comment is getting at is the relative nature of "clever" and "straightforward." At certain times and in certain programming communities, use of deep inheritance hierarchies was "straightforward," and passing a function as a method parameter was so "exotic" and "clever" that languages didn't directly support it.
- intelVISA 2y agoClever is simple, complexity is The Enemy.