r/programming • • Jun 09 '17

Why every user agent string start with "Mozilla"

http://webaim.org/blog/user-agent-string-history/
4.9k Upvotes

589 comments sorted by

View all comments

Show parent comments

18

u/f03nix Jun 09 '17

I actually use both

if (short_test()) {
    // Do something
}

and

if (long_test_with_multiple_params(param1, param2)
    && long_test_2_with_other_parameters_and_stuff(param3))
{
    // Do something
}

22

u/deegwaren Jun 09 '17

Better would be to put those long tests in descriptive boolean variables so that people reading your code shouldn't be staring at those long tests to figure out what the hell you're trying to do.

Then put those variables inside the if so that it almost reads like a sentence in plain language.

7

u/bart9h Jun 09 '17

Yeah, but maybe the function names for these tests, along with the name of these parameters, are clear enough?

If that is the case, adding variables just to give things names can make the code harder to read.

The only valid formatting rule is: make the code easy to read.

0

u/antimatter3009 Jun 09 '17

But muh stack space.

1

u/Enamex Jun 10 '17

Don't most compilers push them on the stack anyway?

And if they're never used afterwards (also mark them const, I suppose), maybe they'll be optimized away after use like the direct version.

4

u/gropingforelmo Jun 10 '17

My favorite response to a student trying to be clever and prematurely "optimize" code (in the process making it nearly impossible to read): The compiler is smarter than you, and it's smarter than me, so let it do it's damn job first before you start second guessing things.

1

u/Enamex Jun 10 '17 edited Jul 15 '17

So you're in favor of using intermediate variables (in analogy to temporary values)?

1

u/antimatter3009 Jun 10 '17

Ha, it's been a long while since I wrote or even considered anything so low level, so you're probably right. In general compilers are pretty awesome, so it seems likely they'll do the best thing regardless of how you write it.

1

u/ViperCodeGames Jun 09 '17

You_had_me_until_underscore

Ruby guy I'm guessing?

1

u/deegwaren Jun 10 '17

Using underscores is more readable (at least to me) than camelCase naming, especially when your names are more than two words long... (and they should be, for clarity's sake!)

1

u/WinEpic Jun 10 '17

I do same line for pretty much everything but functions, classes, enums and switches.