r/badcode • u/vladvlad002 • Mar 07 '21
c++ So this is the source code of the official solution of a problem from the National Olympiad
87
u/CeldonShooper Mar 07 '21
Let's use the C++ compiler because we have polymorphism, STL and multiple inheritance then. Wait...
0
Mar 07 '21
[deleted]
18
u/isunktheship Mar 08 '21
"The C Programming Language" book?
Doesn't hurt to start there, gives you a great appreciation for things!
11
u/alexppetrov Mar 08 '21
At my last uni the first semester we had c++ course that didn't even mention stl, vectors, etc and the next semester it was implied we had to have the knowledge not only of that, but also using the <algorithms> library and python knowledge. It wasn't the worst thing in the world, it was manageable to learn those basics for another week, but for other reasons I had to change uni and i am glad that i did, because starting to work with vectors rather than pointer arrays and using smart pointers instead of raw ones - it's a game changer for beginners. Of course they taught us the pointer arrays and raw pointers later to show us that those exist and where to use them, but still that opened my eyes towards so much more of c++ that I haven't seen
1
u/cheese4432 Mar 09 '21
smart pointers? That's new to me. At my school we learned about STL data structures mainly by programming our own versions of them in data structures.
2
u/tendstofortytwo Mar 09 '21
Smart pointers are amazing - learn once about unique_ptr and shared_ptr, and never manage memory again.
1
u/CDno_Mlqko Mar 08 '21
This morning I had a class on data structures
In python
With fixed array sizes for queue, stack, priority queue
priority queue with linear push
I feel your pain
1
Mar 09 '21
Exactly what I had. The only std library objects used in the course are
std::cinandstd::cout, well plus this manipulatorstd::endl. Oops I meanusing namespace std; coutbecause obviously people think it is a good idea to use the dump everything in the std namespace.
74
184
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 07 '21
Image Transcription: Code
#include <bits/stdc++.h>
using namespace std;ifstream f("permutare.in");ofstream g("permutare.out");
const unsigned oo=2000000001;unsigned i,k,n,m,c,u,v,p,q,x[2010],D[1000][1000];
int main(){
for(D[0][0]=1,i=1;i<1000;i++)for(D[i][0]=1,k=1;k<=i;k++)D[i][k]=min(D[i-1][k]+D[i][k-1],oo);
while(f>>c>>n){u=v=n,p=1,q=n+1,m=2*n;
if(c==1){for(f>>k,i=1;i<=m;i++)if(D[u][v-1]>=k)x[p++]=i,v--;else x[q++]=i,k-=D[u--][v-1];
for(i=1;i<=m;i++)g<<x[i]<<' ';g<<'\n';continue;}
for(i=1;i<=m;i++)f>>x[i];for(i=1,k=1;v>0;i++)if(x[p]==i)v--,p++;else k+=D[--u+1][v-1],q++;g<<k<<'\n';}
return 0;}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
92
61
24
Mar 08 '21
Honest question for this type is subreddit... does OCR not work well enough for this?
19
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 08 '21
Sadly, not currently. We have a bot that attempts posts it detects text in, but code is one of the places it fails most, for some reason. Even in plain text like a tweet or something, it isn't reliable enough to put out official transcriptions that haven't been check over.
Some people use its attempts as a basis for their transcriptions, going through and correcting its mistakes, but not everyone.
One day, hopefully, it'll be able to do most posts, but we aren't there yet.
10
Mar 08 '21
That’s odd. Code is the easiest thing to read. Monospace, grid aligned. What more would you want?
It might be because it’s not words though, every piece of code is different.
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 08 '21
Yeah, I would've thought the same thing. My theory was always that there was something lost in translation between the OCR and the Reddit comment it puts it into. It might be difficult to definitively track down the true problem, though, since we don't develop the OCR itself, I think we just use its API (I'm not entirely clear on the details of how we do that either, since I'm not on the dev team).
1
u/x5nT2H Mar 09 '21
Can you try tesseract with eng language? I use it for screenshots by my coworkers and it works surprisingly well
2
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 09 '21
So tesseract gave me:
#include <hit5/Stdc++.h> using namespace std;ifstr‘eam f("permutare.in");ofstr‘eam g("per‘mutar‘e.out“); const unsigned no:zeeeeeeeel;unsigned i,k,n,ll,c,u,v,p,q,x[2919],D[leee][1999]; int main(){ for(o[o][e]:1,i i<leee;jH)for-(D[i][e]:1,k:1;k<:i;k++)n[i][k]:min(D[iil][k]+D[i][k71],oo); uhi1e(f>>c>>n){ :n,p:1,q:n+1,n:z*n; if(c::1){for-(f>>k,' ;i<:ll;iH)if(D[u][v71]):k)x[p++]:i,v~;else x[qH]:i,kr:D[u~][v71]; for(i:1;i , H)g((x[i]((' ';g<<'\n‘;continue;} for(i:1;i<:ll;iH)f>>x[i];for-(i:1,k: v)e;i++)if(x[p] )V",p++;else kicn[~u+1][v71],qH;g<<k<<'\n';} return 0;}Which is a start -- I could go through and fix the errors, but I get much more bored doing that than when I'm actively typing it out from scratch, so I'll probably just stick to doing them manually. :P
7
10
4
67
u/artiface Mar 08 '21
If you've ever done code golf solutions, meaning solve this problem with least code, they often end up like this. Without knowing the actual problem I couldn't tell if this is bad code. It's certainly not readable or maintainable, but in the intended context it was the winner so there's that argument against it being bad.
42
u/graycode Mar 08 '21
not readable or maintainable
That makes it bad, full stop. Source code is for humans to read, not computers.
61
u/sebamestre Mar 08 '21
In a competitive programming contest, once you solve a problem, and it passes the automated tests, you will never need to read the code ever again, so there is no point in optimizing for readability.
What you do is optimize for write-ability: you try to keep your code as simple and concrete as possible, with as many implementation details visible at once, to make it easy to debug while the meaning of each one letter variable is still in your short term memory.
This makes writing highly compact implementations of tricky algorithms a lot easier and faster.
27
u/DutchBookOptions Mar 08 '21
This isn’t a submission, it’s an official solution.
25
u/sebamestre Mar 08 '21
Official solutions are written by organizers, and problem submitters, who are usually competitive programmers, and their main use is to validate the test cases, not education.
(Source: did compprog during high school, got somewhat involved with contest organizers for a while)
4
u/DutchBookOptions Mar 08 '21
If they’re used mostly to validate test cases then why release them to the public? They can validate without doing that.
21
u/sebamestre Mar 08 '21
Mainly for transparency, fairness, that kinda deal.
-7
u/DutchBookOptions Mar 08 '21
What does this do for transparency? Or fairness? Of course something should be able to solve the problem. Knowing that some code is a solution without knowing what the actual code is, does the same thing as seeing the code without knowing what it’s actually doing.
17
u/sebamestre Mar 08 '21
There could be a mistake in the official solution and, by extension, in the official tests. Letting contestants validate the solution after the fact helps make sure every one gets the right judgement.
-8
u/DutchBookOptions Mar 08 '21
How are they going to validate it if the code is unreadable yet passes the tests?
→ More replies (0)1
u/HagymaGyilkos Mar 09 '21 edited Mar 09 '21
I've never done comp programming, but if you look up some people doing it (competitively), you can see, they write the very same code. There are commonly used code blocks, and alorithm implementations, if you use them, you probably understand this code as well.
Edit: Nvm, I read the full comment thread. If there wasn't any semantic explanation, then this code clearly sucks, but I still think, if ther is a write-up about the algorithm, this is sufficient.
11
u/badadvice4all Mar 08 '21
That makes it bad, full stop. Source code is for humans to read, not computers.
Source code is for humans
to write.
Edit: and read?Edit 2: I think I nailed it : )
2
u/DutchBookOptions Mar 08 '21
Username checks out. This source code has a different purpose than most source code. This is an official solution, not a submission. It’s literally meant to be read by people and understood. That’s at least a good reason to format the damn thing, if not to name variables reasonably. Those are easy to do, and far from all that’s needed for making the code easy to understand.
1
u/badadvice4all Mar 08 '21
Eff it, someone down voted you, but I'll up vote you, my other comment was so pointless... Almost as if I were typing into an infinite web of strings and my comment will only ever be seen by 14 people in the next 10,000 years. Hehe.
-2
u/DutchBookOptions Mar 08 '21
It’s bad code. It’s an official solution, not a submitted solution. This should be at LEAST be formatted to make it readable for those studying the problems to be able to gain some value from the problems. Otherwise why even release official solutions? Obviously they want participants to be able to get some value from the solutions. Just fucking right click and format the code and the value one can gain from this is multiplied. Why not?
0
u/Aphrontic_Alchemist Mar 08 '21
Give it as an exercise for students. What's a better way to learn how to trace that trial by fire? Along with the solution, students will learn how to de-obfuscate code on their own.
3
u/loomynartylenny Mar 08 '21
Thing is, if you want to give this code as a de-obfuscation exercise for students, it also helps if you have the de-obfuscated version as well as a solution for the de-obfuscation exercise.
And, if someone's really stuck, if the solution can be provided/drip-fed, they can then use that to work out what all the obfuscations de-obfuscate to, giving them some understanding that they can apply to any further de-obfuscation exercises.
33
u/LB-- Mar 08 '21
I used to do competitive programming, it was what earned me a full ride scholarship in college. I was the only one who seemed to care about code quality. None of the other students or teachers/professors had any interests or care about the longevity of the code or the quality of it, it was all just about solving difficult algorithm problems that had already been solved decades ago and that you should really just be using a library for. That's not programming in the same sense as the real world, that's just solving algorithm problems and then writing any code that implements the solution within a max timespan without regard for quality. People who try to transition from that to actual programming jobs are going to have a very tough time adapting.
3
Mar 08 '21
Google found a correlation that supports your conclusion. Competitive programmers do have a hard time performing well as software engineers.
https://catonmat.net/programming-competitions-work-performance
10
17
u/Bobitsmagic Mar 08 '21
Well some of my friends went to the international Olympiad and their minds works differently i would say. When they have an algorithm in mind they know how everything needs to be done, then they just type out whats in their head so formatting doesn't matter cuz they only gonna read it once.
13
u/sebamestre Mar 08 '21
I went to IOI too, but I could never adapt to the (supposedly common) bad code formatting. Al my competitive code was properly indented and mostly well formatted, with spaces around operators, etc.
Surprisingly, after seeing some code by some top competitive programmers (tourist, errichto, etc), i learned that they, too, keep their code well formatted (although maybe in a peculiar style), even as they write it
5
u/DutchBookOptions Mar 08 '21
This is not a submission it’s an official solution meant to teach others. It should not be thought of in the same light as a submission
4
u/sebamestre Mar 08 '21
I agree that it shouldn't be thought of as the same, but only so far as to say that the standard for correctness should be higher.
1
u/DutchBookOptions Mar 08 '21
I’m sorry I’m having trouble interpreting your response. Would you mind rewording your reply?
3
u/sebamestre Mar 08 '21
I do agree that contestant submissions and official solutions do not have the same standing
I do not agree that official solutions are tool of education
I believe they should be correct, for the integrity of the contest, even more so than contestant submissions
7
u/sebamestre Mar 08 '21
For anyone wondering, as far as I can tell, the first part intializes D with binomial coefficients.
Then, in the loop, it does two types of operations.
Operation 1 sorts numbers 1..2n into two halves of an array, depending on a comparison between the input data, and some combinatorial numbers, then prints out the array at the end.
Operation 2 re-initalizes the array using the input data, and prints out a value computed from the new array.
7
u/skjall Mar 08 '21
I started formatting this sanely, but I lack the willpower/ time. This is just horrible. Unless obscurity/ loc is being tested, why not use go here, and let the auto-formatter make things nice without you having to bother with the formatting?
Anyway, here's what I have so far:
#include <bits/stdc++.h>
using namespace std;
ifstream f("permutare.in");
ofstream g("permutare.out");
const unsigned oo = 2000000001;
unsigned i, k, n, m, c, u, v, p, q, x[2010], D[1000][1000];
int main() {
for (D[0][0] = 1, i = 1; i < 1000; i++) {
for (D[i][0] = 1, k = 1; k <= i; k++) {
D[i][k] = min(D[i - 1][k] + D[i][k - 1], oo);
}
}
while (f >> c >> n) {
u = v = n;
p = 1;
q = n + 1;
m = 2 * n;
if (c == 1) {
for (f >> k, i = 1; i <= m; i++) {
if (D[u][v - 1] >= k) {
x[p++] = i, v--;
}
else {
x[q++] = i;
k -= D[u--][v - 1];
}
}
for (i = 1; i <= m; i++) {
g << x[i] << ' ';
g << '\n';
continue;
}
}
for (i = 1; i <= m; i++) f >> x[i];
for (i = 1, k = 1; v > 0; i++)
if (x[p] == i) v--, p++;
else k += D[--u + 1][v - 1], q++;
g << k << '\n';
}
return 0;
}6
4
u/zBrunoLEOz Mar 08 '21
Reason for why I didn't even think about going to the Olympiad. Besides the poor preparation in school and the difficulty of the exercises compared to what we do in class, I feel like it's just not worth it. Teaching everyone a mix of both C++ and C with no in-depth about stuff such as classes or pointers even (save classes for higher education!), it is just pain.
Personally I don't know much either but there's so much to learn and school unironically kinda puts me and quite a of my class behind. Should rather have free class to learn by myself, would've been off better.
3
3
Mar 08 '21
This is bad code in the context of programming, but good code in the context of competitive programming.
I hope that the official solution came with some kind of explanation...
3
u/hellbuck Mar 08 '21
Those variable names are more infuriating than the lack of newlines. Literally against one of the 10 commandments of coding.
2
u/tenfingerperson Mar 08 '21
Olympiads have very creative solutions ... hence they usually ace top company interviews
2
Mar 08 '21
This person writes code like he has to pay the compiler a dollar for every line he writes.
2
Mar 08 '21 edited Mar 08 '21
I've instantly realised this is the Romanian National Olympiad.
Relatable, since I also was in a local olympiad. Unfortunately, I had the smallest score smh. I kinda suck :/
Explaining the code context, I don't know about the line thing, but everyone in high school (with programming courses in Romania) is using variable names like that, to mention using namespace std and .in and .out file extensions.
2
2
4
u/remy_porter Mar 08 '21
The idea here is that even if the solution is released contestants can't actually "cheat" from it without having a pretty advanced understanding of C++. This program will compile and give valid results, and the goal is to compare the results of the contestant program against this "known good" program.
Just sorta feels like picking on purposefully obfuscated code is cheating a bit.
1
u/loomynartylenny Mar 08 '21 edited Mar 08 '21
Then again, how will people know that the 'known good' program is actually 'good' when the code is obfuscated beyond all 'known'ness?
Surely it would be best for them to also release a de-obfuscated solution as well, compiling and giving the same valid results as the obfuscated version, to prove the correctness of the obfuscated version?
And, if you want to get into specifics about people memorizing the code for the deobfuscated version if it leaks; well, if it leaks, that's a problem for the olympiad people to deal with, and they could just replace that leaked question with a different one.
1
u/remy_porter Mar 08 '21
Then again, how will people know that the 'known good' program is actually 'good' when the code is obfuscated beyond all 'known'ness?
Because the standard is the output, not the process to generate the output. The goal is to make a program which, given the same inputs, generates the same outputs as the obfuscated program. It is its own proof of correctness.
1
u/loomynartylenny Mar 08 '21
But there's no reason to obfuscate the solution program.
2
u/remy_porter Mar 08 '21
Sure there is. If someone turns in a program which looks or works anything like this then you know they cheated.
1
u/jank_lord Mar 08 '21
Wtf is the point of this? I'm trying to rationalize why this happening but I just can't.
3
u/cyber4dude Mar 08 '21
The point of this is to solve an algorithmic problem as fast as possible
1
u/jank_lord Mar 08 '21
Does putting a bunch of stuff in one line make the compiler optimize the code better? Maybe I just don't know how the compiler works. Like is there any benefit to coding things like this?
1
1
1
1
1
u/Error1001 Mar 08 '21
This is the Romanian Olympiad isn't it?
1
Mar 08 '21
Yes. You can realize that by the filenames used in the std::ifstream and std::ostream instances.
1
1
1
1
1
1
u/fugogugo Mar 09 '21
this must be a solution for a very specific problem
once something changed in the requirement everything breakdown completely
1
u/JohnDanV Mar 09 '21
For those interested: This was a problem in the "county state" of the National Informatics Olympiad (in 2017) in Romania for those in grades 11-12 of highschool (basically last two years of highschool). The problem statement can be found here (it's in Romanian, but it's basically some counting related to permutations which is solved with dynamic programming): statement
1
u/Kamil118 Mar 09 '21
So what we even have here...
So this first seta D[0][0] to 1, then sets D[1][0] to 1
then...
2: ? ? ?....
1: 1 ? ?...
0: 1 ? ?...
then it loops trough row 1, first setting D[1][1] to min(D[0][1] + D[1][0], oo)
D[0][1] is uninitialised, so at the wnd of the day, column 0 is full of 1s, row 1 is full of uninitialised data, and the entire rest of the matrix is filled with uninitialized data that's lower than 2 billion 1 and is increasing towards increasing indexes...
values are read form input to c and n as start values for loop until... file is empty? not sure how ifstream truthness works
so at the start:
c = input
n = input 2
u = input 2
v = input 2
p = 1
q = input 2 + 1
m = input 2 * 2
if (input 1 == 1)...
You know what, it's not worth my time. Nobody pays me to figure out how this works.
1
0
Mar 07 '21
[deleted]
147
u/applepumpkinspy Mar 08 '21
Clever is the enemy of good code
79
u/paranoid_giraffe Mar 08 '21
if the two are opposites then my code must be really really good
37
u/daperson1 Mar 08 '21
It's more that you should use as few clever tricks as possible. People frequently apply excess cleverness in ways that unnecessarily complicate things while adding no value.
I recently saw some very "clever" JavaScript code by some people who had read that "functional programming is good". They had no "for" or "while" loops anywhere. All iteration - including iteration of arrays to perform actions for their side effects - was done via
Object.keys(theArray).map(...)..... I digress.
17
u/stree1928 Mar 08 '21
I have no idea how it works in js but afaik in python, the map function is significantly faster than iterating over a list with a for loop. Shouldn’t be used for everything but it definitely helps in some places.
9
Mar 08 '21
In JS, using functions like .forEach will create a separate closure (so an object on heap) for every iteration and incur overhead for calling it. Using a regular loop is orders of magnitude faster. Of course, the overhead should still be trivial compared to anything you do in the loop, but still.
7
u/Ace-O-Matic Mar 09 '21
.map and .forEach are completely different functions from completely different specs, and .map is incredibly performant.
4
u/RonTheTiger Mar 09 '21
Especially when all those items in the map are done in parallel, something .forEach struggles to accomplish.
2
Mar 10 '21
Huh? Map is not parallel. You can achive parallelism by doing
const results = await Promise.all(array.map(asynMapFunction))but you can do the same with forEach.
1
u/AutoModerator Mar 10 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/davorzdralo, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
Mar 09 '21
It still has overhead compared to a plain loop. But like I said, this should be trivial compared to any work in the loop.
6
Mar 08 '21
Reminds me of a guy on 9gag. The post was a nice gif of eratosthene's prime number generation '. I let a comment with the code I wrote for it, i' several languages cause that's my favorite thing to write. 10-liners. And I state: the gif description is harder to code than my method. Here comes this guy with three lines, yes, three lines of functional Javascript (those map() things) that does it like in the gif. I spent hours reading those three lines with a mixture of awe and headaches. I felt dumb.
Better write dumb and easy-to-read than clever-ass code.
19
3
u/TorbenKoehn Mar 09 '21
Functional programming is nothing "clever", its a solid way of writing code, especially combined with immutability.
With React functional programming is an essential tool to handle states and render them in an efficient manner
I really suggest you try looking into some functional programming and get to know it, there's nothing crazy about it and functions like map, filter, reduce etc. were a thing way before functional programming was "cool".
2
u/daperson1 Mar 09 '21
Yes. The problem isn't FP, the problem is when people insist on using FP primitives when what they're doing is imperative (as in this example).
I'm getting lots of replies from people who seem to have read my comment as "FP is bad/complicated", which is very much not what I was aiming for.
The crazy in my example code is in two flavours:
Calling map() entirely for its side effects.
Using Object.keys() to generate a list of keys for an array, and then mapping that (instead of simply doing forEach/map on the original array).
:(
2
u/TorbenKoehn Mar 09 '21
Object.keysis a perfectly fine and valid way to iterate through an object (you can't iterate through objects by default in JS)It's flavor, some use
Object.entries(theObject).map(([key, value]) => ...), others useObject.keys(theObject).map(key => ...theObject[key]...). Both are perfectly fine. Calling Object.keys on an array (if that happened) however...is weird...but then again, it works (lengthis not an enumerable property)Based on your comment, it's hard to see what you understand what a "side effect" is.
map()in JS has no side-effects. In fact, a very good property is that it doesn't have side-effects and always returns a new array.I understand there are people that take functional programming patterns a bit too far and I agree, but that's not a problem of functional programming, it's a problem of lacking code review. But not having imperative control-structures and loops....that's perfectly fine. Recursion should be in the skillset of every programmer for sure.
1
u/Rikudou_Sage Mar 09 '21
Example of map side effect:
const entries = []; let something; entries.map(entry => something = entry);Meaning you don't actually do a mapping but use map to iterate over an array and produce a side effect.
I've seen it in the wild a few times.
2
u/TorbenKoehn Mar 09 '21
Yeah okay, I can go around and do a shitload of weird stuff in basically every language out there.
Some dudes not knowing how to use their tools is not an argument against the tools
-1
Mar 08 '21
Hey please don't say stuff like this. You're right about the clever tricks being generally bad. But saying "don't use common well supported features of the JavaScript language" without explaining the trade-offs is misleading to beginners. This guy has a good explanation: https://www.reddit.com/r/javascript/comments/57holi/comment/d8sg2b8
2
u/daperson1 Mar 09 '21
That isn't even almost what I said.
Honestly the replies I'm getting to this post are almost as hilarious as the original stupidity.
I am not saying "don't use map". Map is perfectly reasonable if you want to transform an array.
What these idiots did was something like this;
let foo = [1, 2, 3]; Object.keys(foo).map((i) => { console.log(foo[I]); });If they were just doing map() instead of foreach() it wouldve been gnarly. But this? Generating a list of integer indexes and then mapping that, for its side effects only? This is madness .
1
u/AutoModerator Mar 09 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/daperson1, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Ace-O-Matic Mar 09 '21 edited Mar 09 '21
The saddest thing about your comments is that your level of confidence is only achievable by your level of ignorance. The only thing wrong with your example, is using Object.keys on an array which literally doesn't do anything unless for some unholy reason you're mutating the array you're mapping.
2
u/daperson1 Mar 09 '21
It does do something: that's the problem. Instead of simply iterating the array (using any of the sane ways of doing that - a loop, a forEach, even a map-for-its-side-effects if you feel like it), this code creates a new array with the values
0, 1, 2, ..., iterates that, uses those values to index into the original array, and then discards the temporary.Simply mechanically refactoring all occurences of this pattern to either be a map() (in cases where it was truly creating a new array) or a for-of liminated about 5s of JS execution time when accessing a certain page of the React app this was found in.
2
u/Ace-O-Matic Mar 09 '21
It does do something
Yes, and the only reason to ever need to do that, is if you're mutating the original array while operating on it. Which is an anti-pattern.
Also using .map for "its-side-effects" leads to incredibly clean and readable code. It's not about being clever, it's about fitting a loop operation on a single line in a readable and maintable manner.
Although within a context of a React app, if you find yourself doing that, you've already fucked up. By the same notion, if replacing .maps with for loops leads to any noticeable performance improvement on the client-side, just go ahead and fire and rehire the entire front-end development team, because that's a symptom of a far larger architecture level issue.
1
u/sombrilla Mar 09 '21
I don’t know why you’re being downvoted, this is true, but also using the current indexed key value as the index for the array is wrong, since you’re already there. Also [I] will return undefined.
0
Mar 09 '21
You're clearly way too smart for me.
I wonder if this is the way you work with people on your team? How's that working out for you.
1
475
u/coderman64 Mar 07 '21
"Optimal solutions require minimal usage of the newline character"