Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Here's a good one!

Code:
protected void Button5_Click(object sender, EventArgs e)
{
        try
        {
            throw new Exception();
        }
        catch (Exception ex)
        {
               
        }
}
merthsoft wrote:
Here's a good one!

Code:
protected void Button5_Click(object sender, EventArgs e)
{
        try
        {
            throw new Exception();
        }
        catch (Exception ex)
        {
               
        }
}
Hahahahaha wut? That's strange.
merthsoft wrote:
Here's a good one!

Code:
protected void Button5_Click(object sender, EventArgs e)
{
        try
        {
            throw new Exception();
        }
        catch (Exception ex)
        {
               
        }
}


That makes total sense, it's making sure that the runtimes exception handling works Razz

Likewise, don't forget to check that true == true and false == false and false != true and true != false, you've got to make sure that reality is still logical.
A framework for a website I've been assigned to rewrite had roughly the following code run every time a form was submitted:


Code:
for (int i = 0; i < container.size(); i++)
{
    if (container.contains(container.get(i)) == false)
        throw new Exception();
}


The container was a proprietary object that held every field from the form, so on the site's larger forms, the object's contains function would basically be checked dozens of times to make sure it was telling the truth Confused
magicdanw wrote:
A framework for a website I've been assigned to rewrite had roughly the following code run every time a form was submitted:


Code:
for (int i = 0; i < container.size(); i++)
{
    if (container.contains(container.get(i)) == false)
        throw new Exception();
}


The container was a proprietary object that held every field from the form, so on the site's larger forms, the object's contains function would basically be checked dozens of times to make sure it was telling the truth Confused
Ouch, that's horrible. Sad I seriously hope you resolved that when you overhauled it. Smile
Kllrnohj wrote:
That makes total sense, it's making sure that the runtimes exception handling works Razz

Likewise, don't forget to check that true == true and false == false and false != true and true != false, you've got to make sure that reality is still logical.

Sanity checking is important. You can't over-sanity check.

magicdanw wrote:
The container was a proprietary object that held every field from the form, so on the site's larger forms, the object's contains function would basically be checked dozens of times to make sure it was telling the truth

That's awesome! Once again, you can't over-sanity check Wink.
elfprince13 wrote:
2 or 3 (or 4 Very Happy ) nested ternaries are fine if you format them properly.


Only if it's in Python:


Code:
x = 3 if (y == 1) else 2 if (y == -1) else 1


Not totally on topic, but check out these stack overflow threads:

http://stackoverflow.com/questions/tagged/hidden-features

There are some really cool features I've found I otherwise wouldn't have known about.
Kllrnohj wrote:
elfprince13 wrote:
2 or 3 (or 4 Very Happy ) nested ternaries are fine if you format them properly.


Only if it's in Python:


Code:
x = 3 if (y == 1) else 2 if (y == -1) else 1
[...]
I'd argue that that's one of the few times multiple ternaries are unacceptable. That's a super-awkward syntax when the rest of your language is ordered conditional -> statement instead of statement -> conditional.
KermMartian wrote:
I'd argue that that's one of the few times multiple ternaries are unacceptable. That's a super-awkward syntax when the rest of your language is ordered conditional -> statement instead of statement -> conditional.


I disagree, it's much clearer than a ternary, and is easily read and understood without needing to look anything up. It also reads like a typical sentence "We'll do this if something otherwise that". Compare to "We'll do if something this otherwise that" - totally awkward and weird.

?: ternaries, on the other hand, have no obvious meaning until you look up what it means. ?: is the only thing I can think of that is an operator that is actually a conditional in most languages. C# also has the ?? operator/conditional, but ?? is cool, so it gets a pass.
Right, because it's typically unskilled programmers that are looking at advanced code, and especially unskilled programmers that don't have a book or tutorial next to them, because they have no interest in reading someone else's code to learn the language. Please. If you don't understand ?:, you can easily Google "C ? operator" or "C++ ? operator". Third sentence of the Wiki article that is the first result:

Quote:
When not overloaded, for the operators &&, ||, ?: (the Ternary operator), and , (the comma operator), there is a sequence point after the evaluation of the first operand.
And of course, the Ternary operator text is linked to a handy-dandy article about the operator. Razz
Once you know how to use it, the ?: ternary is definitely more logical then Python's then/if ternaries.

Any programmer will be able to read "If THIS then THAT else OTHER" easier then "THAT if THIS else OTHER". That's just... awkward and untraditional conditional structure. If you take your original understanding of If/Then statements, it's very easy to learn ?: ternaries. Reversing the structure can make things a bit confusing (even if the keywords are the same as used in an if/then statement. Besides, how hard is it to memorize how to write a ?: ternary? Razz ). As a traditional programmer, it should be pretty easy to understand a ternary like this:
Quote:
x = (y==1) ? 3 : (y==-1) ? 2 : 1;
Rather then one like this:
Quote:
x = 3 if (y == 1) else 2 if (y == -1) else 1
After learning general syntax of common languages and deciding Python would be the next language I would learn, choosing then to learn about ternaries was a bad idea. At first I began to read it like "X equals 3. if Y is equal to 1, else.... wait what?" A complete inversion of the traditional conditional-statement structure should not be done for the sake of making it easy for an English teacher to grade it's readability.

You shouldn't sacrifice readability or tradition for something so unorthodox. "X equals 3 if Y equals 1, otherwise X equals 2 if Y equals negative 1, otherwise X equals 1" is much harder to read in code to a programmer, then "If Y equals 1, then X equals 3. Else if Y equals negative 1, then X equals 2. Else X equals 1".

That's why they call it "programming languages". It's not English. Smile

You can't exactly break up then/if ternaries very easily, either. At least, not in a way that can be easily readable (as it relates to other conditionals.

For instance, the then/if ternary above could be structured like this, possibly:

Code:
     x = 3
if(y == 1)
     else 2
if(y == -1)
     else 1
Whereas a ?: ternary can be expressed like so:
Code:
$x = ($y==1) ? 3 :
     ($y==-1) ? 2 : 1;
Or even
Code:
if($y==1)
   $x = 3;
elseif($y==-1)
   $x = 2;
else
   $x = 1;
The last two can be read the same way. The first one cannot be written like the last without changing the way it's read. Which, IMHO, makes the first one less readable.

I, personally, hadn't discovered ternaries until I tried to learn Python. I decided to instead check out the PHP documentation for ternaries (because PHP has great documentation). I assumed PHP did ternaries the same, but as soon as I discovered ?: ternaries I got it in an instance.

It's not to say then/if ternaries are dreadful, but IMHO, they're less readable (and writable) then ?: ternaries Razz

This is a mindless debate, as we all know none of us are going to change our minds on the subject, but I just wanted to throw in my two cents Smile

A programmer's technique doesn't always change just because another's is different. Preference is preference (until it begins sacrificing execution times, etc..., but that's not in this debate. Especially when discussing ternary syntaxes in different languages).
?: is a ternary operator - it has three operands, a?b:c, unlike binary operators which have two (a*b) or unary operators which have one (-a). ?: is often referred to as the conditional operator.

Admittedly C-style languages only offer one ternary operator, which is why I guess calling ?: "the" ternary operator has stuck.

Bear in mind that chaining conditional operators is a bad plan in PHP as they have a different associativity to most other languages.


Code:
<?php
$value = 2;
echo $value < 0 ? 'Negative' : value > 0 ? 'Positive' : 'Zero'; # Outputs 'Zero'.
?>


Code:
// C#
var value = 2;
Console.WriteLine(value < 0 ? "Negative" : value > 0 ? "Positive" : "Zero"); // Outputs "Positive".

I suppose one response to "Silly Programming Things You've Come Across" would of course be PHP itself.

Code:
<?php
$a = 'true'; $b = 0;
if (($a == true) && ($b == false) && ($a == $b)) {
    echo 'PHP is silly.';
}
?>
KermMartian wrote:
Right, because it's typically unskilled programmers that are looking at advanced code, and especially unskilled programmers that don't have a book or tutorial next to them, because they have no interest in reading someone else's code to learn the language. Please. If you don't understand ?:, you can easily Google "C ? operator" or "C++ ? operator". Third sentence of the Wiki article that is the first result:

Quote:
When not overloaded, for the operators &&, ||, ?: (the Ternary operator), and , (the comma operator), there is a sequence point after the evaluation of the first operand.
And of course, the Ternary operator text is linked to a handy-dandy article about the operator. Razz


Likewise, if you are unable to understand Python ternaries then you are a complete moron who has no business using a computer.

See? That's how useless your response was. Razz

swivelgames wrote:
Once you know how to use it, the ?: ternary is definitely more logical then Python's then/if ternaries.


I disagree. Python's ternaries agree with other Python one-liners like array and dictionary generators:


Code:
x=(n for n in foo if bar(n)) # x is an iterator
x = [n for n in foo if bar(n)] # x is a list
x = dict((k,k+1) for k in range(10)) # x is a dict


Quote:
Any programmer will be able to read "If THIS then THAT else OTHER" easier then "THAT if THIS else OTHER". That's just... awkward and untraditional conditional structure.


Only because you are already familiar with the ?: ternary.

Quote:
Besides, how hard is it to memorize how to write a ?: ternary? Razz ).


That is a non-argument. I could just as easily say "Java? Seriously? How hard is it to just memorize JVM bytecode?"

Quote:
As a traditional programmer, it should be pretty easy to understand a ternary like this:
Quote:
x = (y==1) ? 3 : (y==-1) ? 2 : 1;
Rather then one like this:
Quote:
x = 3 if (y == 1) else 2 if (y == -1) else 1


True, but Python isn't trying to be a C replacement - never has, never will be. Thus, what a "traditional programmer" expects is entirely irrelevant.

Quote:
You shouldn't sacrifice readability or tradition for something so unorthodox. "X equals 3 if Y equals 1, otherwise X equals 2 if Y equals negative 1, otherwise X equals 1" is much harder to read in code to a programmer, then "If Y equals 1, then X equals 3. Else if Y equals negative 1, then X equals 2. Else X equals 1".


No it isn't, it isn't harder at all - it reads very naturally and logically.

Quote:
You can't exactly break up then/if ternaries very easily, either. At least, not in a way that can be easily readable (as it relates to other conditionals.


And I counter that that is a GOOD thing. Nested ternaries are *bad*. If you are splitting it over multiple lines, then you shouldn't be using a ternary in the first place.
Kllrnohj wrote:
Only because you are already familiar with the ?: ternary.
Negative. I've been working with then/if ternaries longer then I have ?: ternaries. I learned then/if before I learned ?: ternaries.

Kllrnohj wrote:
That is a non-argument. I could just as easily say "Java? Seriously? How hard is it to just memorize JVM bytecode?"
Agreed Razz

Kllrnohj wrote:
True, but Python isn't trying to be a C replacement - never has, never will be. Thus, what a "traditional programmer" expects is entirely irrelevant.
I guess the use of "traditional programmer" wasn't the right label to use. I actually went through my whole post and removed it from other places, but I guess I missed that one. What I meant to say was that because the more prominent languages I've learned share similar syntax with C and its variants, it's much easier to pick up ?: ternaries then it is to pick up then/if ternaries, in my experience. Wink

Kllrnohj wrote:
And I counter that that is a GOOD thing. Nested ternaries are *bad*. If you are splitting it over multiple lines, then you shouldn't be using a ternary in the first place.
The point was not to show a ternary broken up into multiple lines, as if it were a necessity, but to demonstrate their structure in a more easily readable format. Smile


Again, all arguments relating to my own experience. It's all about preference. Good Idea
Quote:
What I meant to say was that because the more prominent languages I've learned share similar syntax with C and its variants, it's much easier to pick up ?: ternaries then it is to pick up then/if ternaries, in my experience. Wink


Python isn't a C-like language Wink

Quote:
The point was not to show a ternary broken up into multiple lines, as if it were a necessity, but to demonstrate their structure in a more easily readable format. Smile


And if you have to restructure 1 statement across multiple lines of code to be readable, then that statement needs to be broken up.

Also, the purpose of Python's ternary is to promote a default value. Think of it as a replacement for:

Code:
x = 5
if y != 1:
    x = 2
benryves wrote:
I suppose one response to "Silly Programming Things You've Come Across" would of course be PHP itself.


Hahaha, Ben, so accurate.
Kllrnohj wrote:
Python isn't a C-like language Wink
Common knowledge. So, as a programmer with more experience in C-like languages, I find it harder to read then/if ternaries.

Kllrnohj wrote:
And if you have to restructure 1 statement across multiple lines of code to be readable, then that statement needs to be broken up.
You're missing the point entirely. It's perfectly readable within one line of code. The fact that I split it up into multiple lines has nothing to do with it's readability, I was simply dissecting it. Wink

The same would be if I showed nested functions on multiple lines.

Code:
$x = foo(bar($y));

$x = foo(
          bar(
               $y
          )
     );
The majority of the time, most code on multiple lines will be easier to read and comprehend. But if you look at the first line I posted, it's perfectly readable. Me posting a snippet up of a single statement on multiple lines does not mean it isn't readable in one line. That makes no sense. Confused

However, that's far from the point I was making. I was simply dissecting statements to demonstrate their structure.

Kllrnohj wrote:
Also, the purpose of Python's ternary is to promote a default value. Think of it as a replacement for:

Code:
x = 5
if y != 1:
    x = 2
Interesting. Good Idea Makes sense.
swivelgames wrote:
You're missing the point entirely. It's perfectly readable within one line of code. The fact that I split it up into multiple lines has nothing to do with it's readability, I was simply dissecting it. Wink

The same would be if I showed nested functions on multiple lines.
Code:
$x = foo(bar($y));

$x = foo(
          bar(
               $y
          )
     );
The majority of the time, most code on multiple lines will be easier to read and comprehend. But if you look at the first line I posted, it's perfectly readable. Me posting a snippet up of a single statement on multiple lines does not mean it isn't readable in one line. That makes no sense. Confused

However, that's far from the point I was making. I was simply dissecting statements to demonstrate their structure.


This is what you said:
Quote:
You can't exactly break up then/if ternaries very easily, either. At least, not in a way that can be easily readable (as it relates to other conditionals.


Your point WAS that you can break a ?: over multiple lines. If you want to make a different point, feel free. My counter to that point is that you should never break a ternary across multiple lines in the first place, as then you should no longer be using a ternary.
Kllrnohj wrote:
You're point WAS that you can break...


Off-topic, but shame.
I could totally see a case where you split a ternary containing a bunch of sub and sub-sub ternaries across several lines, and it would be considered both a good idea to use a ternary and to do the split. Razz
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 6
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement