Ahh, don't you just love a good ternary abuse? :) Consider the following expression:
true ? true : true ? false : false
For those of you who are now utterly perplexed, I can tell you that this evaluates to true. In other words, it's equivalent to this:
true ? true : (true ? false : false)
But is this reliable? Can I be certain that...
I came across the following line in a JS function (it was an RGB to HSB color converter, if you must know)
hsb.s = max != 0 ? 255 * delta / max : 0;
I'm wondering if someone can explain what the "?" and the ":" mean in this context.
...
I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator.
h.className += h.className ? ' error' : 'error'
The way i think this code works is as following.
h.className = h.className + h.className ? ' error' : 'error'
But that isn't correct because that gives a error in my consol...
Hello guys, i tried something like this:
boolean funkyBoolean = true;
int array[] = funkyBoolean ? {1,2,3} : {4,5,6};
But this code won't even compile.
Is there any explanation for this? isn't funkyBoolean ? {1,2,3} : {4,5,6} a valid expression?
thank's in advance!
...
Is this an acceptable coding practice?
public class MessageFormat {
private static final Color DEFAULT_COLOR = Color.RED;
private Color messageColor = DEFAULT_COLOR;
public MessageFormat(Person person) {
Color color = person.getPreferredColor();
messageColor = (color != null) ? color : messageColor; // this...
def val():
var = float(raw_input("Age:"))
status = ("Working","Retired")[var>65]
print "You should be:",status
I was looking on how the ternary operator is implemented in python. I found the code mentioned above as a substitute (from what i have read, found that the ternary operator does not exist, I will be happy to know m...
Is it possible to rewrite this to be shorter somehow?
if (isset($_POST['pic_action'])){
$pic_action=$_POST['pic_action'];
}
else {
$pic_action=0;
}
I have seen it somewhere but forgot... :/
BTW, please explain your code also if you like!
Thanks
...
I saw this today in some PHP code.
$items = $items ?: $this->_handle->result('next', $this->_result, $this);
What is the ?: doing? Is it a Ternary operator without a return true value? A PHP 5.3 thing?
I tried some test code but got syntax errors.
...
i'm new to php. I came across this syntax in wordpress. Can anyone explain to me the last line of that code?
$page = $_SERVER['REQUEST_URI'];
$page = str_replace("/","",$page);
$page = str_replace(".php","",$page);
**$page = $page ? $page : 'default'**
the last line(bolded).
thanks
...
Possible Duplicate:
What is the PHP ? : operator called and what does it do?
From http://twitto.org/
<?PHP
require __DIR__.'/c.php';
if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; }))
throw new Exception('Error');
$c();
?>
Twitto uses several new features available as of PHP 5.3:
The DIR constant
The ...
Is there a ruby idiom for "If do-this," and "do-this" just as a simple command?
for example, I'm currently doing
object.method? a.action : nil
to leave the else clause empty, but I feel like there's probably a more idiomatic way of doing this that doesn't involve having to specify a nil at the end. (and alternatively, I feel like ta...
I want to create a ternary operator for a < b < c which is a < b && b < c. or any other option you can think of that a < b > c and so on... I am a fan of my own shortform and I have wanted to create that since I learned programming in high school.
How?
...
So I'm not going for maintainability or elegance here.. looking for a way to cut down on the total tokens in a method just for fun. The method is comprised of a long nested if-else construct and I've found that (I think) the way to do it with the fewest tokens is the ternary operator. Essentially, I translate this:
String method(param) ...
I was looking at some code with a huge switch statement and an if-else statement on each case and instantly felt the urge to optimize. As a good developer always should do I set out to get some hard timing facts and started with three variants:
The original code looks like this:
public static bool SwitchIfElse(Key inKey, out char key,...
Here's a test for fun with the ternary operator:
public int so( final int a ) {
int r = (int) System.currentTimeMillis();
r += a == 2 ? 1 : 0;
return r;
}
Here's the bytecode produced:
public int doIt(int);
Code:
0: invokestatic #2; //Method java/lang/System.currentTimeMillis:()J
3: l2i
4: istore_2
...
Do you find the following C# code legible?
private bool CanExecuteAdd(string parameter) {
return
this.Script == null ? false
: parameter == "Step" ? true
: parameter == "Element" ? this.ElementSelectedInLibrary != null && this.SelectedStep != null
: parameter == "Choice" ? this.SelectedElement != null...
Those of us who've worked in VB/VB.NET have seen code similar to this abomination:
Dim name As String = IIf(obj Is Nothing, "", obj.Name)
I say "abomination" for three simple reasons:
IIf is a function, all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This...
Hi all,
Just out of curiosity, asking this
Like the expression one below
a = (condition) ? x : y; // two outputs
why can't we have an operator for enums?
say,
myValue = f ??? fnApple() : fnMango() : fnOrange(); // no. of outputs specified in the enum definition
instead of switch statements (even though refactoring is possible)...
I recently refactored code like this (MyClass to MyClassR).
#include <iostream>
class SomeMember
{
public:
double m_value;
SomeMember() : m_value(0) {}
SomeMember(int a) : m_value(a) {}
SomeMember(int a, int b)
: m_value(static_cast<double>(a) / 3.14159 +
static_cast<double>(b) / 2.71828)
{}
};
class MyClass
...
can every if...then...else statement be converted into an equivalent statement using only ?:
...