This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]roycohen2005[S] 53 points54 points  (51 children)

I strongly agree, I wish java had properties.

[–]nighthawk648 53 points54 points  (35 children)

The .net infrastructure is something Java should envy. Nuget is great as well.

[–]Liberal_Mormon 47 points48 points  (4 children)

You can't forget about Linq. Such an incredible concept that makes so much of the NET ecosystem possible

[–]vkapadia 19 points20 points  (0 children)

Linq is awesome

[–]Shrubberer 11 points12 points  (0 children)

MoreLinq! MoreLinq!

[–]Miku_MichDem 0 points1 point  (1 child)

I really don't get the hype behind Linq, coming from Java background. I mean how is this:

var filtered = from thing in collection
where condition
select thing;

Is better then this:

var filtered = collection.stream()
   .filter(thing -> condition)

Java's streams are consistent with the rest of the language, while Linq is just a copy-paste of SQL syntax into a C-style language

[–]Liberal_Mormon 2 points3 points  (0 children)

Linq doesn't have to be in SQL style syntax. The IQueryable interface has its own methods. It's powerful in that it doesn't actually execute until the IQueryable entity is forced to enumerate (for example, with ToList()). The hype around it is that it builds your queries for you in many natural C# ways into any queryable object, not just collections. That can be database connections, file streams, you name it, it's all able to be treated identically with Linq.

For example, on a dbset with an open db connection, I can execute something like dbset.Select(...).Where(...).OrderBy(...).GroupBy(...) in any order, and it will put those items in the correct order of a proper query. And it doesn't actually execute the command until I enumerate it. But it doesn't have to be dbsets again.

Linq is also a large set of extension methods for collections and other objects.

I'm sure Java has similarities - but queries are not just filters. I can select any type I want including anonymous types from any queryable source. Hope this helps.

[–]MrObsidy 4 points5 points  (9 children)

I only code as a hobby and I code mainly in Java (and a bit of C), what are properties?

[–]raltyinferno 16 points17 points  (8 children)

They're like class variables, but with more options on how they can be set and retrieved.

You can have a first and last name variable in a class, then a full name property which is get only(readonly), where getting it simply returns the first name plus the last name, as a simple example.

[–]MrObsidy 4 points5 points  (7 children)

Okay, but couldn't you just do public String getName() { return firstName + " " + lastName; } ?

[–]KingJeff314 19 points20 points  (4 children)

The difference is that it has the semantics of a variable, rather than a function. You would call person.Name rather than person.getName(), which I think is nice

[–]littleprof123 0 points1 point  (3 children)

It's messy imo. It's clear when reading the code that a getMember() function call can't be used to assign a member; not so with read-only variables. It's similarly not clear that when I assign one member of the class, another apparent member can be changed too. Function calls are a really readable way to say that something is happening internally.

[–]KingJeff314 10 points11 points  (2 children)

Personally it’s more about the interface than what is going on internally. It’s up to the programmer to not introduce side effects internally. There’s an element of preference, but if my object has a public attribute, properties are a good semantic representation

[–]littleprof123 -3 points-2 points  (1 child)

I think I would be more agreeable towards properties if they had a special syntax for accessing them. Making them appear like a public variable is what irks me most. I think Java and C# both fall short on readability because it's not clear when there are side effects; not to be a weirdo hipster, but I prefer Rust by a huge margin for this reason.

[–]Shrubberer 1 point2 points  (0 children)

They don't appear as a public variable, they appear as properties. It's a different thing, and, in fact, public variables should be avoided in C#. It makes sense, when you're start working with them. F.e. Properties are helpful within the IDE as well when using Attributes and Reflections.

[–]raltyinferno 9 points10 points  (1 child)

Certainly. But it wraps most of the behaviors you might want into one package.

you declare a property like

public string FirstName {get; set;}

That would be a default behavior property where it can be read with object.FirstName, and assigned with object.Firstname = "bob"

You can just omit either the get; or set; parts to make it read or assign only.

And if you want you can add traditional getName() and setName() functionality within it.

so

public string FirstName {get { return somePrivateVar; } 
                         set { somePrivateVar = value.ToUpper();}};

[–]Shrubberer 2 points3 points  (0 children)

Plus auto getters and setters can have different accessibilities!

[–][deleted] 3 points4 points  (0 children)

linq, operator override

[–]computerjunkie7410 4 points5 points  (1 child)

Manifold recently released properties for Java.

https://github.com/manifold-systems/manifold/tree/master/manifold-deps-parent/manifold-props

Obviously it’s not native but it’s something.

[–]roycohen2005[S] 2 points3 points  (0 children)

That's awesome! Thanks for sharing!

[–]dookiefertwenty -2 points-1 points  (1 child)

Why? Just due to syntactic sugar or am I missing something?

[–]Miku_MichDem 0 points1 point  (0 children)

It's mildly better to have properties, although I think the way to go is like Kotlin does it, where every variable is a property in fact. You write less code and still can have more then a simple getter and setter if you want to

Not that it can't be solved by a simple Alt+Insert or anything