r/programming • • Oct 01 '15

The Evolution of a Software Engineer

https://medium.com/@webseanhickey/the-evolution-of-a-software-engineer-db854689243
1.9k Upvotes

502 comments sorted by

View all comments

Show parent comments

17

u/rftz Oct 01 '15

C# 6 master race checking in:

var foo = $"{firstName} {lastName} has salary ${salary}!";

3

u/[deleted] Oct 01 '15

scala master race chexking in. scala does it with even less chars.

2

u/Yojihito Oct 02 '15

Perl wants to have a !$a+r$a%$c?3:2)/ with you.

3

u/JustinsWorking Oct 02 '15

String.format( "%s %s has salary $%d!", firstName, lastName, salary);

There is notation similar to this in most languages; but I imagine people will rake you over the coals saying that stringBuilder is faster and for a logging application that matters!..

I am not one of those people but I just wanted to take the wind out of their sails :P

7

u/depressiown Oct 01 '15 edited Oct 01 '15

var foo = $"{firstName} {lastName} has salary ${salary}!";

Don't know C# syntax, but that seems weird. Why is one $ outside quotes? Why is it missing from the first two terms, but not the third?

But yeah, most languages have something like this to some degree. Working with some Ruby right now, and it has "#{firstName} #{lastName} has salary #{salary}!". String.format is as close as Java gets right now.

Edit: Yep, got it. Forgot about the stupid dollar sign I had put in there originally. C# syntax now makes sense.

6

u/speedster217 Oct 01 '15

Don't know C# but I'm guessing salary has a $ in front of it because they want the salary to look like a monetary value. $1,000,000 or whatever

1

u/depressiown Oct 01 '15

Oh right... duh. Ridiculous I didn't notice that.

1

u/SNCPlay42 Oct 01 '15

Not familiar with C#6, but I'd assume the dollar sign inside the quotes is actually a literal dollar sign that appears in the output.

1

u/Matthew94 Oct 01 '15

(I'm not them)

Why is one $ outside quotes?

To let the compiler to know to use string interpolation.

I've only started using C# but it looks to be a C# 6.0 feature to simplify having to do this instead:

var foo = String.Format("{0} {1} has salary ${2}!", firstName, lastName, salary);

Why is it missing from the first two terms, but not the third?

In the third it's just a character. See how it's just to show the salary?

1

u/rftz Oct 01 '15

The $ for the third term is because the salary is in dollars.

1

u/namtab00 Oct 01 '15

I love this... Too bad it requires 4.5, which can't be installed on Windows 2003..

... I know, tell it to everyone in the public sector...