Normally referring to the conditional operator, represented by the characters ? and :, that form a basic conditional expression in several programming languages, also known as inline if. It is used as follows: (condition) ? (value if true) : (value if false).

learn more… | top users | synonyms

2
votes
2answers
39 views

Accessing value from either os.environ or argparse

I have the following code: ...
3
votes
2answers
52 views

Forming a dictionary where all values are only conditionally set

I have lots of dictionary keys with conditional values, ternary in this case: ...
3
votes
2answers
56 views

Composing terminal control sequences

I've seen everybody claims the nested ternary to be an evil even when an operation couldn't be written with switch-case; and here's even more, my code could be easily written with switch-case ...
1
vote
1answer
59 views

Faking a pass method when building an array

Ruby doesn't have a pass method to 'do nothing' the way Python does - if I want a ternary statement to do something on true, but nothing on false (as in the code below) is it bad practice in Ruby to ...
3
votes
3answers
91 views

Converting a state from hash into a JavaScript object

This code gets a state from a query string hash part, converts the hash part to a JavaScript object and then tries to get a view, ...
6
votes
3answers
3k views

Returning the lowest and highest number based on input

This simple console program prompts the users for a number and returns the lowest and highest numbers from their inputs. It is difficult to add more user inputs, though. How can I make this easier to ...
5
votes
2answers
305 views

Concise nested statements

I really like the ruby ? : syntax for if else statements. However, I'm not sure how to do the following with a nested ...
7
votes
3answers
484 views

Ternary extension method

I created the following HtmlHelper extension method for my Asp.Net MVC Razor views because the ternary syntax sucks when you need to use it intermixed with markup. Is there a better way to write this ...
14
votes
5answers
3k views

Reduce code complexity by applying ternary operator

I have a condition in my code where I need to put a lot of nested else if statements. However, I have managed to reduce some if ...
7
votes
2answers
267 views

Reasonable use of the comma operator?

I've just posted this solution in my review of @adrienlucca's question, which is to read three rows of six tab-separated doubles. ...
4
votes
3answers
175 views

Execute method on ternary operator result?

I have two List<T>s of the same T and a T object. Based on a condition, I want to add ...
2
votes
1answer
65 views

Property getter implementation

I always see something like this: - (NSMutableArray*)items { if (_items == nil) _items = [NSMutableArray array]; return _items; } Recently I ...
2
votes
1answer
2k views

Strip whitespace from strings in list that contains elements of various types

Let's say I have a list that contains strings and integers. The strings can contain leading and trailing whitespace that I'd like to remove. I can't just strip() ...
3
votes
2answers
148 views

An abundance of ternary operators

I'd like to find a way to do what this method does in a cleaner, less hacky looking way ...
8
votes
4answers
432 views

Should I use a ternary expression in this case?

Is a ternary expression ok in this case: ...
4
votes
3answers
2k views

Shorthand if conditional in PHP

(!empty($student->former_name) ? print $student->former_name : ''); I only want to print out the former name if it is not empty, nothing else. I imagine I ...
2
votes
2answers
1k views

Ternary Operator, check if Datetime is MinValue and set value to it. There is a elegant way to do it? [closed]

There is a better or more elegant way to do this ? ctroRegistro.DataHoraCriacao is Datetime. ...
41
votes
4answers
5k views

Ternary operation in Java - isn't this abuse?

I think the below code is difficult to understand. I also feel it abuses Java's ternary operator. ...
8
votes
4answers
472 views

Ternary operator and comments

I have a class that spawns threads to process data. I am working on instrumentation code. When the process is started, time = System.currentTimeMillis(); When it ...