Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Ithaqua posted:

There's nothing confusing about it, it's just less readable, and the second you start stacking them it turns into a mess. I submit for your consideration: http://stackoverflow.com/questions/9101719/im-looking-for-a-free-tool-stand-alone-or-add-in-that-can-decompose-ternary-ex/

I think other people here are defending the practice of nesting a further conditional expression as the third operand (if-false-value) of a conditional expression, not embedding further conditional expressions as operands of a conditional expression in full generality. Although it would be possible to write reasonable code doing so, if you don't take it to an insane extreme, and you guide the reader using visual cues like indentation.

code:
var x = some_test() ?
            some_other_test() ?
                value_true_true :
                value_true_false :
            value_false;

shrughes posted:

It's because some people are afraid of functional programming.

I don't understand what this has to do with functional programming.

Adbot
ADBOT LOVES YOU

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Hammerite posted:

code:
var x = some_test() ?
            some_other_test() ?
                value_true_true :
                value_true_false :
            value_false;

See, I hate that. That takes me way longer to parse than if it were just written out with ifs and elses. I don't think it's a horror to do it that way, I just don't like it personally, and I'd probably change it if I ran across it in code I was working on.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Hammerite posted:

code:
var x = some_test() ?
            some_other_test() ?
                value_true_true :
                value_true_false :
            value_false;

That's different. I hate the nested case. The original case is not nested at all.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Suspicious Dish posted:

That's different. I hate the nested case. The original case is not nested at all.

I don't understand what you are talking about when you refer to "the nested case" and "the original case". What do you hate?

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Hammerite posted:

I don't understand what you are talking about when you refer to "the nested case" and "the original case". What do you hate?

See, we're having trouble even telling the different cases apart! :v:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Hammerite posted:

I don't understand what you are talking about when you refer to "the nested case" and "the original case". What do you hate?

I originally had a bigger example, but removed it because I thought you guys would just be annoyed by the noise.

JavaScript code:
function ternary() {
    return test_one ? value_one :
           test_two ? value_two :
           test_three ? value_three :
           value_default
}

function ifstmt() {
    if (test_one)
        return value_one
    else if (test_two)
        return value_two
    else if (test_three)
        return value_three
    else
        return value_default
}
JavaScript code:
function ternary() {
    return test_one ?
           test_two ?
               value_one_true_two_true :
               value_one_true_two_false :
           value_one_false

}

function ifstmt() {
    if (test_one)
        if (test_two)
            return value_one_true_two_true
        else
            return value_one_true_two_false
    else
        return value_one_false
}
Can you identify which one is the nested case, and which one is the original case?

darthbob88
Oct 13, 2011

YOSPOS

Hammerite posted:

I don't understand what you are talking about when you refer to "the nested case" and "the original case". What do you hate?
Edit: Removed for redundant, Suspicious Dish did it better.

darthbob88 fucked around with this message at 21:16 on Sep 24, 2012

raminasi
Jan 25, 2005

a last drink with no ice
I did this once :v:
C++ code:
bool surface_pair::contributes_to_envelope() const {
    return
        !is_self() &&
        !are_perpendicular() &&
        (!are_parallel() || // if they're parallel...
            (opposite_senses() && other_is_above_base() && areas_intersect())) &&
        (are_parallel() || // if they're not parallel...
            (other_in_correct_halfspace() && drape_hits_other_plane()));
}
I won't defend it.

McGlockenshire
Dec 16, 2005

GOLLOCKS!
Good, because you don't need to. It's perfectly readable and understandable, once you sit down and read it, and would be even easier to understand inside an IDE with paren matching. The descriptive function names are amazingly clear.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Suspicious Dish posted:

Can you identify which one is the nested case, and which one is the original case?

I am not sure what relevance your example has to mine, since the indentation pattern in your example is dissimilar.

Your example:
JavaScript code:
function ternary() {
    return test_one ?
           test_two ?
               value_one_true_two_true :
               value_one_true_two_false :
           value_one_false
}
My example:
JavaScript code:
var x = some_test() ?
            some_other_test() ?
                value_true_true :
                value_true_false :
            value_false;
In my example, the second and third operands of each conditional expression are each indented one step (four spaces) further than the first operand of the same expression. In your example, the indentation pattern is less consistent.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Because they're the exact same code, indentation different or not. (I meant to copy your indentation style; I copy/pasted the code and changed around a few variable names without thinking about indentation). Can you still not identify which one is nested?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

McGlockenshire posted:

Good, because you don't need to. It's perfectly readable and understandable, once you sit down and read it, and would be even easier to understand inside an IDE with paren matching. The descriptive function names are amazingly clear.

C code:
(!are_parallel() || // if they're parallel...
umm....

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

GrumpyDoctor posted:

I did this once :v:
C++ code:
bool surface_pair::contributes_to_envelope() const {
    return
        !is_self() &&
        !are_perpendicular() &&
        (!are_parallel() || // if they're parallel...
            (opposite_senses() && other_is_above_base() && areas_intersect())) &&
        (are_parallel() || // if they're not parallel...
            (other_in_correct_halfspace() && drape_hits_other_plane()));
}
I won't defend it.

You are an amateur in the business of writing bad code, my friend, and I am a professional.

PHP code:
if ( ( $i == $GAME['tileindustries'][$j] or
       ( $GAME['tileindustries'][$j] == 5 and
         ( !$i or
           $i == 1
           )
         ) or
       ( $GAME['tileindustries'][$j] == 6 and
         ( !$i or
           $i == 3
           )
         ) or
       ( $GAME['tileindustries'][$j] == 7 and
         ( $i == 2 or
           $i == 3
           )
         )
       ) and
     ( ( $card <= $GAME['TopLocationCard'] and
         $carddetail == $GAME['spacetowns'][$j]
         ) or
       ( $card > $GAME['TopLocationCard'] and
         ( $GAME['HasLinkedToTown'][$GAME['PlayerToMove']][$GAME['spacetowns'][$j]] or
           $GAME['HasBuiltInTown'][$GAME['PlayerToMove']][$GAME['spacetowns'][$j]] or
           $canbuildhereusingVCs
           )
         ) or
       $card == 99 or
       !$GAME['HasBuilt'][$GAME['PlayerToMove']]
       ) and
     ( $GAME['SpaceStatus'][$j] == 9 or
       ( $GAME['SpaceTile'][$j] == $i and
         $GAME['TechLevels'][$j] < $TheTechLevel and
         ( $GAME['SpaceStatus'][$j] == 8 or
           $GAME['SpaceStatus'][$j] == $GAME['PlayerToMove'] or
           ( $i == 1 and
             !$GAME['CoalInLancs'] and
             ( ( $GAME['CurrentPlayers'] == 2 and
                 $GAME['CoalDemand'] == 6
                 ) or
               ( $GAME['CurrentPlayers'] > 2 and
                 $GAME['CoalDemand'] == 8
                 )
               )
             ) or
           ( $i == 2 and
             !$GAME['IronInLancs'] and
             ( ( $GAME['CurrentPlayers'] == 2 and
                 $GAME['IronDemand'] == 6
                 ) or
               ( $GAME['CurrentPlayers'] > 2 and
                 $GAME['IronDemand'] == 8
                 )
               )
             )
           )
         )
       ) and
     ( $GAME['SpaceAlwaysExists'][$j] or
       ( $GAME['ModularBoardParts'] & $GAME['SpaceExistenceArray'][$j] )
       )
     ) {
    ...
}

Suspicious Dish posted:

Because they're the exact same code, indentation different or not. (I meant to copy your indentation style; I copy/pasted the code and changed around a few variable names without thinking about indentation). Can you still not identify which one is nested?

I thought that your argument was that it was hard to tell them apart. I think I'm still not understanding your point. For me, it is ok to nest conditional expressions up to a very limited level of complexity, but the reader should be helped by formatting the expression to make its structure easy to perceive.

seiken
Feb 7, 2005

hah ha ha

Suspicious Dish posted:

C code:
(!are_parallel() || // if they're parallel...
umm....

The comment is perfectly correct but yes, this weird idiosyncrasy is probably the one reason it might qualify as a horror (still pretty mild, though).

nielsm
Jun 1, 2009



Hammerite posted:

You are an amateur in the business of writing bad code, my friend, and I am a professional.
:gonk:

Hammerite posted:

I thought that your argument was that it was hard to tell them apart. I think I'm still not understanding your point. For me, it is ok to nest conditional expressions up to a very limited level of complexity, but the reader should be helped by formatting the expression to make its structure easy to perceive.

A nested ternary expression as the second operand to a ternary removes the immediate consequence of a false condition much further from the condition, while when you only nest on the third operand the immediate consequences of both possibilities are easily visible.

I would certainly prefer reading this (change indentation to your preference):
C++ code:
return !x ? a : y ? b : c;
to this (again, change indentation to your preference):
C++ code:
return x ? y ? b : c : a;

That Turkey Story
Mar 30, 2003

Hammerite posted:

I don't understand what this has to do with functional programming.

The alternatives that people provide imply some form of mutation, whereas the ternary form is a non-mutating operation. It's the reason why you often use the ternary operator to initialize a const object or when doing branching at compile-time in C or C++.

Centripetal Horse
Nov 22, 2009

Fuck money, get GBS

This could have bought you a half a tank of gas, lmfao -
Love, gromdul

qntm posted:

This is only a horror if you try it in PHP.

Maybe this one counts as a horror:

ourDiv.removeClass().addClass(((ourIndex%3+1) % 3 ? ((ourIndex%3+1) % 2 ? "chartOptionsLeft":"chartOptionsCenter") : "chartOptionsRight")).attr("id", "chartOption" + ourIndex).attr('name', ourIndex);

I'm pretty much straight-up abusing ?:, here, because it's fun to use. I could have just used a loop counter (or a dozen other methods) to determine whether the item should be left, center, or right, but what fun is that? I also loaded it down with mod operators just because.

raminasi
Jan 25, 2005

a last drink with no ice

Suspicious Dish posted:

C code:
(!are_parallel() || // if they're parallel...
umm....

There's no logical implication operator in C++, but fortunately you can emulate one!

Zombywuf
Mar 29, 2008

GrumpyDoctor posted:

There's no logical implication operator in C++, but fortunately you can emulate one!

I've used this idiom a lot in SQL. I'm glad I'm not the only one who wants implies to be part of the standard set of logical operations.

PrBacterio
Jul 19, 2000

Zombywuf posted:

I've used this idiom a lot in SQL. I'm glad I'm not the only one who wants implies to be part of the standard set of logical operations.
Now you've made me think about if it would be possible to overload the '->' operator in C++ to function as a logical implication operator :gonk:

EDIT:

Zombywuf posted:

No. It only takes its lhs as an argument.
That's why I said I was only *thinking* about it, instead of just doing it :) I agree that it does seem impossible at first glance, but with some of the really weird and out there contortions that people have managed to construct out of C++ I wouldn't be prepared to deny the possibility out of hand. Which is why I offered it up as a horror in this thread with a :gonk: to go along with it ...

PrBacterio fucked around with this message at 00:52 on Sep 25, 2012

Zombywuf
Mar 29, 2008

PrBacterio posted:

Now you've made me think about if it would be possible to overload the '->' operator in C++ to function as a logical implication operator :gonk:

No. It only takes its lhs as an argument.

That Turkey Story
Mar 30, 2003

Zombywuf posted:

No. It only takes its lhs as an argument.

->* is a binary operator :}

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
=>

I forget, is that actually an operator? Or are we stuck with >=

That Turkey Story
Mar 30, 2003

Suspicious Dish posted:

=>

I forget, is that actually an operator? Or are we stuck with >=

No, it's not. But, you could always do <= and have the implication go right to left instead of left to right.

PrBacterio
Jul 19, 2000

Suspicious Dish posted:

=>

I forget, is that actually an operator? Or are we stuck with >=
No it's not, but as I said, with some of the weird poo poo people have come up with in C++ I still wouldn't discount the possibility of someone, somehow making it work.

EDIT: More realistically speaking though, it probably would be easiest to just use /implies/ as the operator.

PrBacterio fucked around with this message at 01:17 on Sep 25, 2012

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Suspicious Dish posted:

=>

I forget, is that actually an operator? Or are we stuck with >=

=> isn't an operator.

edit: oh, you were talking specifically about c/++. This is a pointless post.

Zombywuf
Mar 29, 2008

Oh yeah, you can't overload anything that only takes primitive types as arguments (i.e. bool) as well.

PrBacterio
Jul 19, 2000
C++ code:
#include<iostream>

namespace SlashOperators
{

class ImpliesOperator
{

public:
	class OperatorApplication
	{
		const bool lhs;
	public:
		OperatorApplication(bool _lhs): lhs(_lhs) {}
		inline bool operator|(bool rhs)
		{
			return !lhs || rhs;
		}
	};

	ImpliesOperator() {}
};

ImpliesOperator implies;

inline ImpliesOperator::OperatorApplication operator|(bool lhs, ImpliesOperator rhs)
{
	return ImpliesOperator::OperatorApplication(lhs);
}

}

using SlashOperators::implies;

#define implies |implies|

int main()
{
	int x, y, z;
	std::cin >> x >> y >> z;
	if(x == y implies y == z)
	{
		std::cout << "x != y or y == z" << std::endl;
	}
	else
	{
		std::cout << "x == y and y != z" << std::endl;
	}
	return 0;
}
The implies operator, with the same precedence and associativity as the | or operator. You're welcome :colbert:

Floor is lava
May 14, 2007

Fallen Rib
Whoops. Swing and a miss.

Floor is lava fucked around with this message at 17:36 on Sep 25, 2012

Zamujasa
Oct 27, 2010



Bread Liar
code:
/* 50 lines into a <style> tag */
   ...
   position: absolute;
   top: 240px;
   left: 320px;
   width: 15px;
}
<?}}?>


      </style>
   </head>
   <body>
<script ...
It's like a little PHP UFO! :3:

That Turkey Story
Mar 30, 2003

Zombywuf posted:

Oh yeah, you can't overload anything that only takes primitive types as arguments (i.e. bool) as well.

Yeah, but you can get around this by just wrapping your arguments.

_( implication_goes_here ) <= some_bool && some_other_bool

You'd want the implication_goes_here part to be a function object, though, so that you get lazy evaluation.

Zombywuf
Mar 29, 2008

That Turkey Story posted:

Yeah, but you can get around this by just wrapping your arguments.

_( implication_goes_here ) <= some_bool && some_other_bool

You'd want the implication_goes_here part to be a function object, though, so that you get lazy evaluation.

Think I prefer PrBacterio's solution.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Zombywuf posted:

Think I prefer PrBacterio's solution.

I think I prefer neither of them, and you should too.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
When a coding error becomes a coding horror: Added a line to my PHP to spawn a new instance of itself, due to inherited weirdness. But it wasn't dying right and kept spawning new processes.

In my attempt at killing them, I ran an untested command to kill all of my processes. Except I hosed it up and apparently killed everything *except* my processes, which meant I killed our production server. :saddowns:

edit: and yes I know the real horror is doing development work on the production machine.

Golbez fucked around with this message at 20:27 on Sep 25, 2012

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

quote:

The range of character sequences expected should be strictly enforced by the javascript on the front end and the code on the backend. Any submission with unexpected characters or the < and > and ` characters should be rejected, before it could be used in any kind of database query. Don't send the submitted data back to the browser though, just report to the user that unexpected characters were detected. Odd character sequences like '1 AND 1', 'CREATE TABLE' and 'DROP TABLE' and 'SELECT *' should be filtered against. Multiple adjacent spaces should be collapsed into one before the filtering. Numbers should be checked to make sure they are in the range expected. On some of the sites I've made, if the backend code detects any kind of SQL injection attack, it logs the ip address and automatically blocks that ip address for a few hours, in case its a script kiddy attack that is going to send a billion requests in an hour trying all kinds of attacks.

And any passwords sent should be sent through the https protocol, if you are concerned about security and people are paying money for the service. You don't really need to pay the fee for a certificate, but most browsers will pop up a warning.

And I think that mysql_real_escape_string is deprecated. I think its just mysql_escape_string now.

Optimus Prime Ribs
Jul 25, 2007

quote:

And I think that mysql_real_escape_string is deprecated. I think its just mysql_escape_string now.

I hope, for your sake, that you don't work with the person who said that. :ughh:

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
Poe's Law definitely seems to apply to that.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Sounds quite straightforward really. All you have to do is filter against odd character sequences.

Zamujasa
Oct 27, 2010



Bread Liar

Hammerite posted:

Stuff about filtering

My "site" actually does some filtering against typical script-kiddie attacks (usually obvious identifiers for them), and adds the requester to a block list that lasts for a few hours, just to make those scripts not actually cause much load on the server while they fire their 10-per-second requests off.

The rest of it is just pure bullshit though. Proper escaping (or just using a real database object and avoid manual queries as much as is feasible will prevent that poo poo.

Adbot
ADBOT LOVES YOU

Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD
Whenever I'm facing a registration form that has a bunch of arbitrary tax code validations for the password written on the side, I have visions of a garden variety IT know-it-all going into soapbox mode with more or less those same words.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply