by pwdisswordfishc · 3y ago · 0 comments
- writing "return condition" instead of "if condition then return true else return false end"
- using the conditional-value ("ternary") operator in any capacity
- early returns/goto cleanup instead of nested if conditions
- using basic higher-order functions like map or reduce in any capacity
- any kind of metaprogramming whatsoever
One person's "clever" is another's "elementary". If you want to point out that some code is hard to understand, that's fine. But don't call it "clever". Using "clever" as a criticism is implicitly an ad personam argument, passing value judgement on the person who wrote it ("Oh, they must have written it to show off") instead of an argument about the code itself.
Also, this:
> Hi friend! Hope I didn't startle you. Can I let you know about my newsletter?
is missing a "fuck off to hell, don't you dare disrespect my attention like that ever" button.
Metaprogramming tho... You want to make a magic black box that takes hours to debug and conceals real bugs, invest heavily in metaprogramming to save a hundred lines of code that can be maintained with three find and replaces/SED calls and a recompile a year... Most metaprogramming shouldn't exist in production code.
Programming is social. You have to weigh your decisions heavily by who you work with now and who you might work with in the future. If you work in a niche language or toolset you might think "this is my chance to do whatever I want!" But the reality is, it should be the complete opposite.
Most compilers make clever code silly. You can write a 15 line hard to read pipe or method chain, or a 25 line double for loop with the same runtime characteristics. The latter is always better to maintain, while the former is always more clever. One is good for the team the other for someone's self esteem.
You’ve just told us which one you’re more familiar with, that’s all, which is exactly what the other commenter was pointing out. This one also belongs on the list in that comment.
Beyond familiarity and subjective preference, there are benefits to the pipe approach that come from its functional nature. Determining that a for loop is a pure pipeline can be tricky in general, which contradicts your idea about which one is harder to read. And the ability to get reliable parallelism for free - e.g. Java’s parallelStream - is not a feature of for loops.
You can grill me if you want and say I am the problem! Zing ouch got me so good! Those HN points will get racked up, so much winning...
Or you can read between the lines. I drew an unspecific example with microseconds of thought behind it. People who know what I am saying know what I conveyed is fine. People who know me, know I regularly use pipes and chained method calls all day. The point is, the paradigm could go either way, it depends on culture(reading the room), and design decisions. Had I of flipped my example and said "method calls can make code way cleaner than for loops" which in some contexts is equally valid your antiparticle HN contributor would make the same argument you made calling me the problem. Get it?
I would say people trying to shit on everyone around them for being casual are more of a problem then anything else. Especially in collaborative development environments. That supercedes any code anyone could contribute. But that's my take and I'm not going to suggest it belongs on some arbitrary list of ad hoc HN rules (of which none hold water)... I won't be writing a five pager with examples where what I said was sound valid and best praxis, I have nothing to prove, and that was never the point. Everyone who knows what I'm talking about gets it. Happy flag planting with imaginary enemies.
Why not? As in, it just seems like a language design issue, there's no reason why you technically couldn't use the "simpler" syntax. For example, what prevents us from having:
parallel for (Type instance: collection) {
// code for each iteration
}
Of course, there are other reasons to consider using streams and such, though admittedly they can sometimes be cumbersome to work with: especially when you would like to step through all of the transformations that a particular object instance undergoes (or when you map stuff and create new ones, or reduce lists into fewer items), as opposed to needing to tinker with lots of conditional breakpoints in the debugger.If the construct you propose were feasible, why do you think it doesn’t exist?
The reason is that it’s not feasible. Compositional pipelines involve constraints and properties that an imperative for loop doesn’t support.
This is the big one. For large datasets or computationally intensive processing the speed difference will be noticeable.
One of the issues with "cleverness" is also that code that's trivial for John Carmack to understand might not be for $BODY_SHOP_RESSOURCE_200353.
I recall a self taught dev (or maybe from a bootcamp) coming up with a cascade of nested if-else, nested 8 deep. Someone with a background in CS asked him what he was trying to do and basically concluded that what he was trying to do could be expressed as a state machine. To which the initial dev replied that it was "way too fancy" and that he didn't need the code to be fancy, just work.
Weirdly, in rust there's lots of cases where the compiler will generate better assembly if you give it map/filter/fold than it will if you give it a for loop. The reasons are weird - like, its easier for the compiler to know it doesn't need bounds checks for list.iter(), and to work around some integer overflow errors while iterating through ranges and things like that.
The difference usually doesn't matter in practice, but I still find myself thinking about it in performance critical sections.
The challenge with DO NOT TOUCH this code comments that intense performance tuning leads too(figuratively and literally) is it makes people who don't understand the code build bizarre(and often slow) monuments all around it that don't usually need to exist and deter from the minor gains won over a cute optimization. I've seen this happen ALOT.
Of course there are times where you should opinionate your code to better serve the machine. But my big thing is, it's rarer then the overly complex design decisions favoring it are and compilers are getting better every day.
I desperately wish I could say this, but I have seen it happen
I've seen both cases for some of these types of things. The rotten culture being the most common
Go ahead and try. Many of the libraries you will use use metaprogramming of one flavour or another for ergonomic reasons.
Python decorators are metaprogramming. For example putting @cached above a function declaration esentially wraps your function into another that caches it's values without you ever having to see all of these details.
I am not sure if your production code would really gain from writing out that caching logic over and over again for each of your functions that need caching. I am sure however that it would become less readable and harder to reason about.
In Rust #[derive(Serialize, Deserialize)] can be used to automagically give your data-types serialization and deserialization. This is also metaprogramming. It also reduces the code you have to read and write and the mistakes you will make if you roll this on your own. It also makes your code easier to read to all people who understand what it does (so nearly everyone who programs Rust).
Metaprogramming is okay, if it is done in the right places for the right reasons and doesn't obscure the logic of the program.
I often use it for custom decorators in flask e.g. @admin_user or @authenticated_user to quickly wrap the functions for some http routes with the ever-same logic for authentification. Sure you could also do this with a function, but the ergonomics could be worse and you could accidentally place the function call in the wrong place and thus exposing some parts of a route to unauthenticated users. I don't think this makes it harder to reason about my code.
Like all "don't do X" idioms in programming the one about metaprogramming should be taken with a grain of salt. There are cases where metaprogramming is the best (most reliable, futureproof, usable, etc) way of solving a given problem. The warning is true in that you should avoid using it everywhere without reason. But there are places where it makes sense and there it would be a waste not to use it
Agree that “clever code” is silly but also important to remember that people need to entertain themselves and “grow professionally.” People get paid more if they write the 15 line pipe and it’s job security. Maybe gpt will help since it’s a lot better to have gpt write easy to debug for loops, but then programming will be less fun. People need to entertain themselves.
I am pro have fun screw up and grow. But macros present a certain kind of danger a lot of other tools don't. If you are playing with them be careful of their scope is my only real warning.
> using the conditional-value ("ternary") operator in any capacity
Looks like author of some code I had to comb through recently, maybe had that among guidelines. Said code was replete with:
if(function_that_returns_boolean()){
return true;
}else{
return false;
}
...and... if(foo()){
return true;
}else{
if(bar()){
return true;
}else{
return false;
}
}Why stop there?
switch (byte_value) {
case 0: return 0;
case 1: return 1;
...
case 255: return 255;
}Doing the same for not tiny boolean expression: way more
Just because you think it is cool cuz profs forced it on ya during college is not a argument
What most people object to is leaving the last level of if-then-else and returning true or false, when you could have returned simpleBooleanExpression and do away with 1 level of nesting.
If a And b Then
Return True
Else
Return False
End If
programs. Not sure why they were so scared of Return a And b. Apparently they got a new curriculum after I graduated; my teacher was very happy about it but I didn't get to see it myself.The 2021 final exam (which is the most recent I can find) [1] somehow creates XML records for a SQL database (section C, question 7), and hints at using "data validation" to avoid an SQL injection (section C, question 11.c), which is quite exciting. And the 3.5 gigabit wireless connection (section A, question 13) is definitely not using an ISP-supplied router.
[0] https://www.vcaa.vic.edu.au/Documents/exams/technology/2019/..., answers https://www.vcaa.vic.edu.au/Documents/exams/technology/2019/...
[1] https://www.vcaa.vic.edu.au/Documents/exams/appliedcomp/2021..., answers https://www.vcaa.vic.edu.au/Documents/exams/appliedcomp/2021...
case Big_Expression_Here is
when true => Do_Something;
when others => Do_Something_Else;
end case;
and I found myself wondering whether they realize what this is.I don't know how you got this conclusion.
I can see judgement being passed to the code with respect to not just who wrote it, but also who's going to need to understand it later, who's going to need to make changes to it. In that sense, calling code "too clever" makes sense, right?
There's hyper-specific reasons why you'd need code that's going to be complex/too clever for that group, esp if it saves cost or is faster than the simpler alternatives, but in most cases there's nothing that justifies it.
I am personally on the fence about this one. It's tremendously useful when writing code, and I've done quite a fair share of it myself; but on the other hand, when you are reading the code, trying to understand what the hell is actually being called and why, it really sucks when your F12 bottoms out at some meta-generic piece of machinery that invokes something when called from somewhere but what and where, exactly, is a mystery that requires a global search over the repo in the best case (in the worst case there is either nothing to search on or the search term will turn up thousands of hits).
But, like all programming: it should help readability and it should help to avoid bugs, not be used for its own sake, aka cleverness.
I don’t get it; why?
Anecdotally, me and my colleagues are fine giving ourselves feedback that some code is too clever, without malicious undertones.
But I would want to know what they've done themselves to evaluate the weight of their expertise. If they have impressive projects, their criticism means something.
there was a koan about the progression of someone's Java program from beginner to intermediate, to expert to master to enlightened. The code for the enlightened looked the same as the beginner.
These koans are useful too
https://prirai.github.io/books/unix-koans.html
Edit: In other words, say a developer that criticized wordpress, do you respect what they say more than the success and value of wordpress (of the wordpress developers)?
Mmm I've been programming for 30 years across a lot of languages. My code doesn't look the same as it did when I was a beginner. Sure has flopped around a lot though. Take comments:
- When I started, I used no comments (because I was ignorant)
- Then I put comments everywhere because someone told me it was good practice
- Then someone said comments are a sign your functions are too long and poorly named. So I went back to no comments for a bit. But I sort of hated the resulting code - functions were too small and it still wasn't "obvious" no matter how cleverly I named my functions.
- So then I had a few comments here and there, wherever my code wasn't "obvious".
- Then I tried literate programming, and my code was in small islands amidst an essay of comment. That was fun, but it didn't last.
These days I use comments for 2 purposes: to document my APIs, and to write little letters to my future self. Eg:
// This code looks wrong at first glance, but its actually correct.
// Here's why: ...> Edit: In other words, say a developer that criticized wordpress, do you respect what they say more than the success and value of wordpress?
Depends on the content of the criticism. There are many legitimate criticisms to be had about WordPress, and they can't all be dismissed with "it's popular, so it must be good".
An authoritative source of accomplishment or demonstration is more valuable than someone with no skin in the game, or who doesn't even contribute even if what they say is true.
Wordpress has had security issues, I didn't mean to say popular = good. What I meant is that I trust the wordpress developer's ability to know how to write software that works than someone who doesn't know how or has ever written successful software but knows how to avoid security issues. Don't let perfect be enemy of the good :-)
What would you prefer or listen to more? More developers capable of writing wordpress-successful products or more developers who can criticize but can't build anything.
You could listen to the wordpress developer's attitudes on software development but you wouldn't take their security advice. Maybe you would ignore the criticizer's criticism on how to build software.
That's true! Just calling something a best practice does not make it so. That also has to be argued for, by demonstrating the consequences of one practice over another.
> I trust the wordpress developer's ability to know how to write software that works than someone who doesn't know how or has ever written successful software but knows how to avoid security issues.
Criticism being easier to state than address does not invalidate the criticism. "Sure, this extension may be full of SQL injection bugs, but that's easier said than fixed, so it doesn't matter. I'm going to install it anyway."
I don't like the pride, superiority and smugness that comes from criticizing code and the assumption that they're the ones in the right because of their criticism, rather than the expertise of the one who wrote the code and solved the problem and did the work.
I've noticed people pay more attention to the stones that people throw rather than those throwing them.
Edit: In other words, criticism is cheap if you have no skin in the game.
I don't think it's a fallacy, it's like hosting a wedding and inviting a guest and then being criticised for the entertainment on offer. People pay attention to the criticism, not that the criticism is coming from a guest and that their criticism is not as valuable coming from the guest than the people paying for and tastes of those hosting the wedding.
Whether or not the code quality issues are true or not, their importance is dependent and weight of someone's criticism is not that high as everyone thinks.
There is a fashion for inverting it: "if (true == some_boolean)", which seems arse-over-elbow to me (I do know why people do that).
if (true = some_boolean)
While this will silently result in an assignment of true to the some_boolean variable, not intended the equality test if (some_boolean = true)
Editor hints and linting can help catch this as well, but those aren’t always available (or weren’t available in the past). I think I first saw this in Code Complete 2.Here’s some more explanation
https://softwareengineering.stackexchange.com/questions/7408...
Looks like the pattern is sometimes referred to as a Yoda expression.
This removes virtually any pop up or cookie banner or suedo ad on any popular website.
Yeah, that's the button I was looking for.