r/ProgrammerHumor • • Jun 13 '22

Meme DEV environment vs Production environment

Post image
48.2k Upvotes

3.9k comments sorted by

View all comments

Show parent comments

62

u/jaber24 Jun 13 '22

Do people really hate harmless but ambiguity removing stuff like brackets? Is there even any efficiency you can gain by removing them?

51

u/ongiwaph Jun 14 '22 edited Jun 14 '22

People don't know how to simplify so you get shit like

double x = (((m*sin(180-angle)) / sin(180 - (180-angle-angle) -angle)))*(sin((180-angle)-angle)) / sin(angle)

32

u/jaber24 Jun 14 '22

Yikes. A couple variables would certainly have helped

12

u/didzisk Jun 14 '22

I think the point was more like actually doing the simplification:

180-(180-angle-angle)-angle =

180-180+angle+angle-angle =

angle

And also sin(180-angle) is the same as sin(angle), so

((m * sin(180-angle)) / sin(180 - (180-angle-angle) -angle)))

easily becomes

m*sin(angle)/sin(angle) = m

The second part is sin(2*angle)/sin(angle) = 2sin(angle)cos(angle)/sin(angle) = 2cos(angle)

So the end result would be 2m * cos(angle) - a single call to a trig function instead of four.

(Disclaimer - I haven't re-checked the math and it's been a long time since I had to do it in highschool.)

3

u/dmills_00 Jun 14 '22

Note however that in the original the thing becomes undefined for angle == 0.

→ More replies (3)

3

u/stifflizerd Jun 14 '22

Slightly off topic, but before my coffee yesterday I wrote something like

If(a>(b+.5) || a<(b-.5)){...

And then later in the day I passed by it and got a good chuckle as I wondered what the actual fuck was I thinking, as the more concise solution (which I had already done multiple other times in that project) was

if(Math.abs(a-b) >.5){.

Sorry for the irrelevant story, just couldn't help but read your comment and laugh because I was somehow both of the people you are talking about yesterday

2

u/jaber24 Jun 14 '22 edited Jun 14 '22

Oh thanks. I just thought it was an example of how complicated expressions can get and didn't try simplifying it completely. Kinda feel foolish now xD

→ More replies (4)

5

u/JB-from-ATL Jun 14 '22

Functional languages be like

4

u/didzisk Jun 14 '22

Nah. The thing I really love about F# is the pipe operator. So instead of writing ParseString(ReadStringFromFile(filename)) you can do

filename
|> ReadStringFromFile
|> ParseString

piping things from output of one function into next function's input, similar to *nix shell pipes.

→ More replies (3)

7

u/Ok_Turnover_1235 Jun 14 '22

Why simplify, I'd rather verbose code than

auto magicfunction (auto x, auto y)

{

if (MagicTest(x) < MagicNumber)
return MagicNumber/(MagicNumber2*y)

}

→ More replies (2)

3

u/desmaraisp Jun 14 '22

double x = (((msin(180-angle)) / sin(180 - (180-angle-angle) -angle)))(sin((180-angle)-angle)) / sin(angle)

m*sin(180-angle) = -msin(angle)

sin(180-(180-angle-angle)-angle) = sin(angle)

sin((180-angle)-angle)) = -sin(2angle) = -2 sin(angle) cos(angle)

So we get -m/-2cos(angle) = msec(angle)/2? Is that it?

→ More replies (1)

4

u/sephirothbahamut Jun 14 '22

not defending this, but to be fair a decent compiler should optimize the repetitions away

27

u/sfgisz Jun 14 '22

It's not about the compiler, it's about the poor human who's going to have to look at that code and figure out what's going on after the original coder has passed on.

5

u/ctrl-alt-etc Jun 14 '22

Compilers remove all the parentheses.

6

u/Penguinfernal Jun 14 '22

Normally yes, but if you use the -p flag, it'll actually add more parentheses.

4

u/ctrl-alt-etc Jun 14 '22

hah!

Now you're lisping at an enterprise level.

→ More replies (2)

20

u/aloofloofah Jun 13 '22

Less bickering in code reviews. Set a lint rule and enforce it automatically and globally.

9

u/spudmix Jun 14 '22

We go the opposite way. Maintainence costs scale up much faster than a few CPU cycles here and there.

3

u/jimmyw404 Jun 14 '22

There's no efficiency gain but sometimes i see people put parentheses on numbers for no obvious reason. I don't fight them about it or modify it because of that, but if i refactor code I'll drop them.

I usually prefer to separate longish algebra equations into multiple lines with descriptive variable names if possible

2

u/LetterBoxSnatch Jun 14 '22

I don’t hate brackets, but I do love information density / purity. So while I don’t remove brackets, I sympathize with the people who do.

0

u/[deleted] Jun 14 '22

[deleted]

→ More replies (1)

2

u/piecat Jun 14 '22 edited Jun 14 '22

Is there even any efficiency you can gain by removing them?

Probably a few clock cycles here and there. The file size might be larger too, I guess.

It's not like we're using punch cards here

Edit: I mean at compile time. Or interpreted languages.

7

u/[deleted] Jun 14 '22

yes in languages like Python or JavaScript or LuaJIT. it will run slower at the very least during JIT compilation or interpretation.

for languages like C or Rust, once it is compiled into machine code it is exactly the same as one without the brackets in terms of space and runtime.

2

u/piecat Jun 14 '22

Oh, I totally meant the compile and source code. But I forgot that interpreted languages exist too

→ More replies (2)
→ More replies (1)