Questions tagged [shorthand]

355 questions
1399
votes
14 answers

Multiline string literal in C#

Is there an easy way to create a multiline string literal in C#? Here's what I have now: string query = "SELECT foo, bar" + " FROM table" + " WHERE id = 42"; I know PHP has <<
Chet
  • 21,375
  • 10
  • 40
  • 58
648
votes
7 answers

CSS transition shorthand with multiple properties?

I can't seem to find the correct syntax for the CSS transition shorthand with multiple properties. This doesn't do anything: .element { -webkit-transition: height .5s, opacity .5s .5s; -moz-transition: height .5s, opacity .5s .5s; …
Gregory Bolkenstijn
  • 10,003
  • 7
  • 36
  • 38
309
votes
8 answers

Omitting the second expression when using the if-else shorthand

Can I write the if else shorthand without the else? var x=1; x==2 ? dosomething() : doNothingButContinueCode(); I've noticed putting null for the else works (but I have no idea why or if that's a good idea). Edit: Some of you seem bemused why…
Nikki
  • 3,664
  • 2
  • 15
  • 14
266
votes
8 answers

$(document).ready shorthand

Is the following shorthand for $(document).ready? (function($){ //some code })(jQuery); I see this pattern used a lot, but I'm unable to find any reference to it. If it is shorthand for $(document).ready(), is there any particular reason it might…
Mark Brown
  • 12,026
  • 8
  • 27
  • 32
155
votes
3 answers

Difference in C# between different getter styles

I do sometimes see abbreviations in properties for the getter. E.g. those two types: public int Number { get; } = 0 public int Number => 0; Can someone please tell me if there are any differences between those two. How do they behave? Are both of…
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
111
votes
2 answers

PHP shorthand for isset()?

Is there a shorthand way to assign a variable to something if it doesn't exist in PHP? if(!isset($var) { $var = ""; } I'd like to do something like $var = $var | "";
brentonstrine
  • 21,694
  • 25
  • 74
  • 120
88
votes
6 answers

Which "if" construct is faster - statement or ternary operator?

There are two types of if statements in java - classic: if {} else {} and shorthand: exp ? value1 : value2. Is one faster than the other or are they the same? statement: int x; if (expression) { x = 1; } else { x = 2; } ternary operator: int x…
Rogach
  • 26,050
  • 21
  • 93
  • 172
68
votes
6 answers

What does "nit" mean in hacker-speak?

When someone writes "nit: removed whitespace" on a commit, what does "nit" mean? I've also seen it capitalized as if it were an abbreviation (i.e. NIT). For an example usage see this post: Of course there is a difference between a comment saying:…
kdauria
  • 6,300
  • 4
  • 34
  • 53
55
votes
4 answers

How do you declare x and y so that x+=y gives a compilation error and x=x+y not?

I ran into this question in an interview and couldn't come up with a solution. I know the vice versa can be done as shown in What does the "+=" operator do in Java? So the question was like below. ..... x = .....; ..... y = .....; x += y;…
knshn
  • 3,401
  • 1
  • 21
  • 22
50
votes
10 answers

JavaScript shorthand if statement, without the else portion

So I'm using a shorthand JavaScript if/else statement (I read somewhere they're called Ternary statements?) this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right" This works great, but what if later I want to use just a…
ahren
  • 16,803
  • 5
  • 50
  • 70
47
votes
7 answers

Shorthand if/else statement Javascript

I'm wondering if there's a shorter way to write this: var x = 1; if(y != undefined) x = y; I initially tried x = y || 1, but that didn't work. What's the correct way to go about this?
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
40
votes
2 answers

Other Ruby Map Shorthand Notation

I am aware of the shorthand for map that looks like: [1, 2, 3, 4].map(&:to_s) > ["1", "2", "3", "4"] I was told this is shorthand for: [1, 2, 3, 4].map{|i| i.to_s} This makes perfect sense. My question is this: It seems there should be an easier…
trh178
  • 11,228
  • 5
  • 28
  • 37
37
votes
6 answers

Try/catch oneliner available?

Just as you can convert the following: var t; if(foo == "bar") { t = "a"; } else { t = "b"; } into: t = foo == "bar" ? "a" : "b"; , I was wondering if there is a shorthand / oneline way to convert this: var t; try { t = someFunc(); }…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
36
votes
10 answers

Overview of PHP shorthand

I've been programming in PHP for years now, but I've never learned how to use any shorthand. I come across it from time to time in code and have a hard time reading it, so I'd like to learn the different shorthand that exists for the language so…
James Simpson
  • 13,488
  • 26
  • 83
  • 108
34
votes
5 answers

c# shorthand for if not null then assign value

Is there any shorthand in c# now that will cutdown the following code: var testVar1 = checkObject(); if (testVar1 != null) { testVar2 = testVar1; } In this situation only want to assign testVar2 if testVar1 is not null from the CheckObject()…
Matt
  • 3,305
  • 11
  • 54
  • 98
1
2 3
23 24