3 ms·
I am the only one to actually like reading clever code? When I see something like Duff's Device, my first reaction is a big WTF, like everyone else, but then,
by GuB-42 5y ago
I am the only one to actually like reading clever code?
When I see something like Duff's Device, my first reaction is a big WTF, like everyone else, but then, I sit down, try to understand, and when I get it, I am happy because I learned something. In fact, seeing Duff's Device is really what help me understand switch/case in C. And Duff's Device is certainly outdated, but it is by no mean "bad". If you need to unroll a loop by hand, that a good way to do it, it is just that you don't need to unroll loops anymore.
So if you write code I have to read later, please don't hold back, use every clever trick you have. A small comment would be appreciated but not necessary. This way, I can read your code and think "I learned something today".
Does it slows down comprehension? Of course it does... once. That's the cost of learning, but it is an investment, next time, I'll be faster.
I am really talking about the cleverness the article disapproves of. I also value insight, but here, I agree with the article.
I know a common saying is "newbies should understand your code at first glance". I only agree when by newbies, it means newbies on the project with regards to domain-specific knowledge. But when it comes to general principles, including knowledge of the programming language, code should be written for the highest level of expertise you can reach (but no further, you have to understand what you a doing!). I mean, if you are not using the full power of your tools, not only you are dragging yourself down, but the newbie you are trying to be kind with will not learn anything new.
- j_wtf_all_taken 5y agoNewbie can also be yourself looking at your own code a year later. You will have forgotten a lot and will have made it harder for yourself to re-understand what you were doing. There's a difference between learning stuff and applying stuff. Its about not making the code more complicated and obscure than necessary. Using generic language constructs like switch/case - sure. I don't think that falls in the category of "clever".
- GuB-42 5y ago> Newbie can also be yourself looking at your own code a year later. You will have forgotten a lot and will have made it harder for yourself to re-understand what you were doing. I hear this argument a lot but maybe that's just me but I don't forget much. I don't have perfect memory, far from it, but remembering my thought process when faced with my old code, even decades old is not really a problem for me, especially if a lot of though was put into it, as it tends to be the case with "clever" code. And it can also an important part of the learning process, reading old code is a good way to take a step back and seeing what really are the good ideas and the ones that are terrible. Also, I don't think there is too much of a difference between learning and applying, especially not in programming. That's one thing I love with this job, there is always more to learn, and while theory is also important, it happens by actually doing things. Boring, repetitive tasks are for the computer, not the developer. And I totally agree that one should not make the code more complicated than necessary, but what it necessary is maybe the hardest skill to master. As for Duff's Device is definitely "clever", few people realize that switch/case in C are that closely related to goto, it is not how they are usually taught and I certainly didn't before seeing Duff's Device. And there are more straightforwards ways of manually unrolling loops that don't have you pause and think.
- j_wtf_all_taken 5y ago> I hear this argument a lot but maybe that's just me but I don't forget much. I don't have perfect memory, far from it, but remembering my thought process when faced with my old code, even decades old is not really a problem for me That might be true for you. But I have met too many people that say this about themselves, but then I see them "understanding" their old code (i.e. looking around and not having a clue). In their cases, its just self-deception. Its the same people that say they don't need comments, but then they look around their own files with confused faces. But yeah there are definitely also people with good memory. Those are also the people that happen to, for example, put more thought in structuring their code. > Boring, repetitive tasks are for the computer, not the developer. Unfortunately we have to do a lot of boring, repetitive tasks when developing that could be done by the computer ;-(