Post code you see daily that grinds your gears

post code you see daily that grinds your gears

Other urls found in this thread:

kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
crypto.stackexchange.com/a/10285
twitter.com/AnonBabble

What is your grief with that code? It doesn't check errors?

++i
i++
i+=1

seed = time()
while(1){
srand(seed)
rand()
}

using namespace std;

time returns a time_t, srand wants an unsigned int

What is your point?

And?

>What is your point?
it's time to rethink incrementation

current time with 1 second resolution and which 24 bits you can easily guess isn't proper source of entropy

>time returns a time_t, srand wants an unsigned int
The definition of time_t is implementation defined. It is also automatically truncated, so isn't really a problem.

>What is your grief with that code?
Not OP, but using seconds as seed is predictable and doesn't contain any inherent entropy. You can simply set the system clock and generate the same sequence.

You should rather use some hardware state for seed.

>isn't proper source of entropy
Who cares?

>wants randomness
>doesn't care about entropy
Explain

import java.net.*;

while (true) instead of for(;;) for infinite loops

if (x = y)...

What if it is supposed to be an assignment?

for (int i = 0; i < foo.size(); ++i)

Fuck it isn't the 80s. Heck I bet even in the 80s they had languages with reasonable iteration patterns.

... at least it gets compiled to the same thing

int n, m, i, j; // loop counters

It's enough for what I do with rand.

>he doesn't use the audio of his grandmother queefing into the mic as a source of entropy
Pathetic.

Yes, better to use an IteratorCompositeFacadeFactory to count from 1 to 10.

Everyone complaing about this is an idiot. Seeding rand with the time is fine. If the random number you want needs to be seeded non-deterministically, then you shouldn't be using rand() in the first place.

Better use some kind of range or stream.

Friendly reminder that this is valid GNU C++11
for (int i: {1 ... 10})
{
}

erlang and haskell are from late 80s so there were probably even list comprehensions

JESUS CHRIST PLEASE STOP

system(...)

Seriously, fuck off and use a library.

>i want my programming language to come prepacked with bloat

>syntax is bloat

>I program in asm, everything else is bloat

>double compile time because bloated syntax
>double run time cuz muh interpreted language
>not bloat

>double run time cuz muh interpreted language
Are you retarded?

This.

>{1 ... 10}
never seen that before, doesn't compile and no google results. care to elaborate?

sizeof(char)

cat file.txt | grep content

Maybe I want to gradually build an expression?

find . | grep foo
Fight me.

I agree with you.
It's hard to read and unnecessarily long.

Better alternatives:

foo.forEach((item, index) => {...})

repeat(()=> {...}, foo.size())

while(condition) {
code;
}


I see this and assume the person that wrote it is a fucking tryhard that's obnoxious to be around IRL. Put your fucking brackets on their own line. Whitespace means fuck all and you know it.

>read someone's code
>while(true)
>oh, he's using an infinite loop

>read someone's code
>for(;;)
>why the fuck would you do that?

$array = ['element-1',
'element-2',
'element-3',
'element-4'];


if ($someCondition) {
return $value;
} else {
return $anotherValue;
}

When you see code like this, you know the guy does not give a fuck.

if ((x = y))

if (((goldberg)))

if (((goldberg)))
abort();

less foo
/searchterm
/
/
/

Compilers recommend this.

if (((goldberg)))
shutdown();

ftfy

kek

if (((goldberg))) {
the_goyim_knows = true;
shutdown();
}

why?

if( goyim->know() )
{
it->shutdown();
}

for me it's people who put brackets on new line and write
}
else
{

and don't use auto-formatters or any relevant standard coding style
kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces

I mean still better than introducing magic numbers to your code - technically you could argue 1 being an acceptable magic number, but in this case you could also argue sizeof being there for readabilities sake. Also, sizeof is compiled statically so no reason to complain.

>he should be ashamed of himself being a "'"'decent'"'" programmer and kill himself imo

What's wrong with doing that?

nano foo
searchterm

>echo "some text" > some.file

>nano

Only students put opening brackets on their own line.

I see no problem with that as char is defined by standard to be AT LEAST one byte but there isn't a guarantee on the upper limit.
Some implementation might do 2 bytes because it's defaulted to Unicode instead of ASCII.

Linus will shot you on sight if you do something like
if (condition)
{
// ...
}
else
{
// ...
}

>I see no problem with that as char is defined by standard to be AT LEAST one byte but there isn't a guarantee on the upper limit.
That's plain wrong. char is defined to be the size of the smallest addressable unit, and sizeof will therefore always be 1.

>Some implementation might do 2 bytes because it's defaulted to Unicode instead of ASCII.
Nope, these (aka only Windows) use wchar_t for unicode-16. Sane systems that use UTF8, uses multi-byte strings for this.

HIV

/* i don't know why this works */
for (;;) {
//arbitrary library function
}

Literally nothing wrong with using nano.

>no condition provided
>runs unconditionally

Fucking kill yourself filthy new line bracket degenerate

Except that it's terrible, desu

Perfect example of the type of people responsible for the current state of security, folks!
>should rather use some hardware state for seed.
Don't reinvent the wheel, especially when it comes to security-sensitive stuff.
If your OS is sane, it should provide you with sufficiently good sources of entropy - e.g. dev/{u,}random (early VM/embedded boot disclaimer applies).
Sure, there's plenty of use cases where your use of randomness doesn't involve any security considerations.
But in anything that has even the slightest relation to security, the above code is unacceptable.
OTOH, a sane language should make the proper solution easy enough that most people don't resort to idiocy like this.

It's good enough for a quick edit.

>If your OS is sane, it should provide you with sufficiently good sources of entropy - e.g. dev/{u,}random
/dev/urandom literally uses hardware state, ya fucking doofus.

This is the only acceptable case

Linus would shit on anyone for anything. he's a grumpy cunt that's using one accomplishment from over 20 years ago to give him some elitist status.

I've seen worse: do { ... } while (true)

Not him but that is exactly why you should use it instead of re-implementing it. You're not going to implement it better.

>You should rather use some hardware state for seed.
>Everybody who uses a prng needs cryptographic grade randomness
Most of the time you use PRNGs you specifically NEED reproducibility. That's pretty much the whole point.

if (condition) {
return; // or break or whatever
}
else { // why the fuck do you need else when the previous condition if true goes out of scope?
}

>If your OS is sane, it should provide you with sufficiently good sources of entropy

t. c++ developer

I never said anything about re-implementing shit, you guys are both projecting your assumptions onto my post.

You *should* use hardware state for your seed, i.e. /dev/urandom

and Window/VS users

Generally every code that compromises readability for brevity or intentionally uses obscure notation to make your code look more sophisticated. Usually none of those things compile to faster code and it drives me mad.

>Generally every code that compromises readability for brevity or intentionally uses obscure notation to make your code look more sophisticated.
>literally describing C++

So much this. I'm a python dev and everyone thinks its the coolest thing since sunday be 'pythonic' which to them doesn't mean programming with the python data model it means just cramming language features in how they see fit. So a bunch of super cool(tm) generator expressions that define custom objects and shit like that.

At the end of the day its both less readable and severely under preforming a simple solution.

>/dev/urandom literally uses hardware state
Never claimed otherwise, get your reading comprehension checked m8.
See You should avoid implementing it yourself, since the majority of people have no clue of the possible weaknesses of most implementations. I mean, if the world's crypto experts took that long to figure out NSA's Dual_EC_DRBG shenanigans, how well do you think the average NIH sufferer will do?
Also, the folks behind dev/{u,}random at least weren't retarded enough to blindly trust Intel's black-box RDRAND, yet there's still noise that their approach (XOR'ing RDRAND output *after* calculating the rest of the entropy) might be vulnerable to attacks from the processor.

crypto.stackexchange.com/a/10285

do while is actually more intuitive

What does that do?

>seeing it's infinite loop after the body
not really

I wasn't just talking about infinite loops, but every while that has at least one run.

Finds "foo" in files in the current directory

[...] and all subdirectories

Very much agreed. It's way too common in python, especially since new, absolutely superfluous features are added all the time. Not too long ago I've seen something like this
a, *b, c = range(5)

which results in a being 0, c being 4 and b being [1,2,3]. If you know python can use the star expression like that, it's no big deal, but nobody can convince me that this is a readable way to perform that shit. Stuff like that just doesn't need to exist.

>never said anything about re-implementing shit
This thread might be used as an actual source of advice by some poor bastard. As such, let's take the worst case interpretation:
>[using srand(time) is retarded]
>You should rather use some hardware state for seed.
worst-case dev: "huh, let me google hardware source entropy. Huh, this says Intel's RDRAND is the safest best totally most iron-clad stuff. Guess I'll use that", or some other non-best-practice shit.
This is why it's important to consider the worst-case interpretation of what you say/write. 10 seconds of effort to elaborate "but don't NIH it, check best practices - e.g. use OS-provided entropy" might make the world a slightly less shitty place.

Because...?

So exact same as:
grep foo -R
correct?

Oh sorry, didn't read correctly.
Nope, file just prints the filenames disregarding the content.

>implied
but yes

old incrementation
int i = 0;
++i;

new incrementation
int i = 0;
write_int_to_pid_mem(getpid(), &i, read_single_val_from_pid_mem(getpid(), 4, &i)+1);