C# Object Initializer Syntax
-
X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| X| &
Right.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
"I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns. -
Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";
Initializing is for sissies. Real programmers do not initialize.
-
Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";
-
Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";
Sadly I've been corrupted by javascript and I am currently finding this the most readable (and I do this for SQL as well for INSERT INTO column lists)
StudentName student2 = new StudentName {
FirstName = "Craig",
LastName = "Playstead"
};I know, I've gone rabid and need to be put down out of humane concern. :sigh:
-
Give it another iteration of new language features and we'll probably have the even shorter C++ version. At times I wish they went faster but at least MS is steadily chipping away at language verbosity in C#; unlike Sun/whOracle who seem to revel in Java's bloatyness. :doh:
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
I would Java.Lang.Objection.RaiseSpecificObjection.Object to that slam on Java being wordy! :-)
-
I always use your second style here, all function calls are broken up with one param per line, all neatly lined up. (I am so anal about code tidyness :) )
We actually align the = signs as well. It makes it MUCH easier to read the 2 columns of assignment! (so you shouldn't feel overly anal about it)
-
Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";
I always hated that style of indent = { indent. When I learned C it was:
void main()
{
IndentedCodeHere;
}I cringed when I saw:
void main()
{
CodeHere;
}as if that made it easier to read. Then JavaScript came in
function main() {
Codehere;
}Which I can live with, and I get. But it just seems wrong. It is VERY hard to match the braces visually, but the editors do it for me... But indentation matters.
-
Just a personal preference, but I've come to dislike the look of this syntax. I think it's the indentation. StudentName student2 = new StudentName { FirstName = "Craig", LastName = "Playstead", }; vs StudentName student2 = new StudentName(); student2.FirstName = "Craig"; student2.LastName = "Playstead";
-
Am I not seeing what you're seeing? I don't see ANY indentation there. FWIW, I prefer this indentation:
var student2 = new StudentName
{
FirstName = "Craig",
LastName = "Playstead",
};Just a personal thing but I hate code visually floating out to the right. I get that it lines up with the "new" but I find that disturbing, like I need to go in and fix it OCD style. I see the same thing with some default SQL formatters (like from RedGate). First thing I go to customize.
-
Sadly I've been corrupted by javascript and I am currently finding this the most readable (and I do this for SQL as well for INSERT INTO column lists)
StudentName student2 = new StudentName {
FirstName = "Craig",
LastName = "Playstead"
};I know, I've gone rabid and need to be put down out of humane concern. :sigh:
-
StudentName student2 = new StudentName
{
FirstName = "Craig"
,LastName = "Playstead"
};Nothing at all wrong with that, and requires less typing than your preferred method. Of course, the ultimate in "less typing" is a constructor with a parameter for each property:
StudentName student2 = new StudentName("Craig", "Playstead");
In the end, your preference doesn't matter within the context of your employer's coding standards, and no matter which way you go, they all do essentially the same thing.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013It's not quite the same, in the matter of debugging. If initializing gets a little more complex, the former syntax do not allow stepping to find which assignment is throwing the exception. It's not the end of the world, but it slows you down, so I prefer the latter. It doesn't even require more typing as you can type several lines at the same time in the editor.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Give it another iteration of new language features and we'll probably have the even shorter C++ version. At times I wish they went faster but at least MS is steadily chipping away at language verbosity in C#; unlike Sun/whOracle who seem to revel in Java's bloatyness. :doh:
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
I just love it. Most of the terseness is easy to read as well, except those LINQ monsters that ReSharper turns your loops into. Even a few of them are easier to read.
Do what thou wilt shall be the whole of the Law. - Liber AL vel Legis 1:40, Aleister Crowley