Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (776)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  C#, did Microsoft just want to be different?  (Read 17726 times)
0 Members and 1 Guest are viewing this topic.
Offline atombrot
« Reply #30 - Posted 2014-11-26 15:45:33 »

I can give you a very basic example.

HashMap<SomeUglyType, AnotherUglyLongType> map = new HashMap<SomeUglyType, AnotherUglyLongType>();

Vs

var map = new HashMap<SomeUglyType, AnotherUglyLongType>();

Already an improvement without any information loss. Why do you have to repeat yourself in java? There is absolutely no gain in defining the type twice if the variable holding the value has the same type as the instance being created... still you have to process this information everytime you read the code. This is not a very big thing, but there are many small improvments like that and all those together tend to add up. And when having a very huge code base these small things can matter a huge deal.

 I worked on some stuff that might have been more complex than sorting sprites for rendering (management and forecasts for financial portfolios, simulating different investment strategies  for said portfolios etc ...). As soon as you have to do more than "check that the account is not below 0" it is crucial that the business logic is as clear and readable as possible.
Offline junkdog
« Reply #31 - Posted 2014-11-26 16:38:10 »

HashMap<SomeUglyType, AnotherUglyLongType> map = new HashMap<SomeUglyType, AnotherUglyLongType>();

Ofc, in java 7 and onwards you can shorten it somewhat:

1  
HashMap<SomeUglyType, AnotherUglyLongType> map = new HashMap<>();


Project Lombok has val, which essentially works like C#'s var.

artemis-odb: bugfixing and performance optimized fork of artemis ECS
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #32 - Posted 2014-11-26 16:51:20 »

@atombrot - seems you may have missed a few changes in Java over the last few years. The <> operator and lambdas in particular make things rather shorter than you might remember from 10 years ago.

As for var... I don't think it makes much of a useful contribution to the language given the <> operator exists and writing the types out explicitly helps the brain understand what is being written when it is read back. The missing <> operator is a major Achilles heel in the C# language actually, I found myself cursing the lack of it.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline atombrot
« Reply #33 - Posted 2014-11-26 17:11:08 »

Not entirely correct. You still have to type Map<>. But yes you are right, my java example was worse than it needed to be.

And I'm not advocating var for java. I'm saying that introducing var in C# has resulted in code that is easier to read. As I said. All the small improvements together make the whole package really good.

Yes, lambdas are nice and help improve java. But there is still a long way to go to be en par with c#
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #34 - Posted 2014-11-26 17:18:55 »

I'm still not seeing any significant advantages in C# over Java, syntax-wise. They're both roughly equivalent, expressively, and indeed as the OP seems to say, some things appear to be different "just because, ner ner ner". Or possibly to distance it from Java a little further in peoples' minds.

Cas Smiley

Offline atombrot
« Reply #35 - Posted 2014-11-26 18:17:22 »

Sure, if you limit this discussion to var, there are no big advantages vs java. If you look up the features I listed in my first post you gain a lot more additional fluff and also things that java simply does not support (better generics support/valuetypes, extension methods) Wink

It's not my (nor is it your) problem that you don't see the benefits. It's my gain that I see them and can use them to improve my productivity and the fun factor while coding. Just wanted to share some thoughts that are biased in in the other direction Wink
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #36 - Posted 2014-11-26 19:16:58 »

Well.. I'm not trying to limit the discussion to var. I would like someone like you to espouse the benefits of C# so we can see where it's better.

Cas Smiley

Offline Roquen

JGO Kernel


Medals: 518



« Reply #37 - Posted 2014-11-26 19:52:42 »

One could argue that C# has better support of hardware features and allowing you "step out of the box".  I'm not going to though.
Offline atombrot
« Reply #38 - Posted 2014-11-26 22:34:05 »

Well.. I'm not trying to limit the discussion to var. I would like someone like you to espouse the benefits of C# so we can see where it's better.

Look at the things I mentioned in my first post. I know that I cannot explain those concepts better than existing resources on the internet.

Some things:
  • Default and named parameters. I like convention over configuration. why do method overloads when you can have your values defaulted to sane values. => http://msdn.microsoft.com/en-us/library/vstudio/dd264739%28v=vs.100%29.aspx
  • Use "yield" for easy implementation of iterators => http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx
  • dynamic type in a statically typed language.  http://msdn.microsoft.com/en-us/library/vstudio/dd264736%28v=vs.100%29.aspx
  • The coalesce operator (??) => http://msdn.microsoft.com/en-us/library/ms173224.aspx
  • Generics information is not erased after compile. Reflection can access the information of the generic types.
  • You can use value types for generics.
  • Properties. I actually like them very much. I find working with properties nicer. objectA.Value = objectB.Value + objectC.Value as opposite to objectA.setValue(objectB.getValue() + objectC.getValue()). But I see that that is just a personal preference.
  • Operator overloading. A controversial one but can be definitely useful.
  • Nullable types (probably would not have much use in java, as you cannot define your own value types) => http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
  • Extension Methods:
    http://msdn.microsoft.com/en-us/library/bb383977.aspx[/url]
    A very neat way to extend existing API's without breaking encapsulation (XD, sorry, had to bring this example). This is extremely helpful if you want to add functionality to an API that you don't own or cannot change. The .NET library has some very handy extension methods that add functionality very similar to streams (java 8 ), without touching any of the underlying storage classes. A very useful tool.
  • The var operator:
    http://msdn.microsoft.com/en-us/library/bb383973.aspx
    This operator has been received with mixed reactions from the developer community, mainly because it can be abused. As far as I know it only has been introduced to allow anonymous types in the language. But they can be also very useful when having very large types or type definitions with many generics etc. I had the example with the hash map. I come across classes like BackendFormatterKeyChainOperatorImplemenation, sometimes you have even generated classes with much longer names ... That is not really a problem of the language, but the var operator helps to cope with the symptoms. I use it often, but only when it is clear what the resulting type is or when it doesn't matter.

Some of those features are mainly useful for my "boring business work", but I really miss things like extension methods, default and named parameters, ?? and var. I think there is a reason projects like lombok or xtend exist. Java has room for improvement and I really hope it will improve even further.
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #39 - Posted 2014-11-27 11:31:06 »

Yes, I like a lot of those things.

Because Java is missing them and needs it for performance:
  Value types

Especially for reducing a bit of boilerplate and increasing readability:
  Default and named parameters
  All exceptions are unchecked 

Things I'm definitely not keen upon:
  The coalesce operator. Co-opting ascii symbols to mean fancy new things is one of the worst aspects of many language, especially as they all like to mean subtly different things. I'm not even a great fan of ? : but I've used it for so many years before that I've gotten used to it.
  var. For various reasons but mostly because it hides stuff I like to know.
  Properties. Have a dot operator already. If I mysteriously need to munge values before getting and setting there's a perfectly good way to do it already - with a method.
  Nullable types. Daft idea. We have reference types to do that. Make a reference to a value type. Job done.
  Operator overloading. Again with the ascii symbols. I'm in favour of a version that allows normal method identifiers though.
  readonly and const (strangely Java has coped without either of these for its entire existence and we're all still alive)

Things I'm still not sure about:
  Extension methods. Seem like a great idea but I started to get an uneasy feeling about them after a while.

Stuff I know nothing about:
  Yield.
  Dynamic types.

Stuff I don't care about:
  Type erasure. The number of times I need to use reflection in ordinary programming is essentially zero unless I'm doing hax or not doing "ordinary programming"
 
Things that are in Java which are excellent ideas:
  the <> operator, which removes large tracts of crappy redundant typing
  static import, ditto
  lambda syntax, ditto, plus bells and whistles
  final, which works in a lovely consistent way
  for instead of foreach. Consistent use of keywords!

Things that could do with adding to Java other than those features mentioned that are nifty about C#:
  The elvis operator. I take back what I said about ascii Wink
  More type inference and implicit casting
  The ability to specify byte and short literals with suffixes a la longs (dunno if C# has these)
 
Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pjt33

« JGO Spiffy Duke »


Medals: 40
Projects: 4
Exp: 7 years



« Reply #40 - Posted 2014-11-27 12:13:22 »

Things I'm definitely not keen upon:
  The coalesce operator.

...

Things that could do with adding to Java other than those features mentioned that are nifty about C#:
  The elvis operator. I take back what I said about ascii Wink
The coalesce operator ?? and the elvis operator ?: are functionally the same.

Stuff I know nothing about:
  Yield.
Basically it's a limited form of coroutine. It is very useful for people who want to write implementations of Iterator; I haven't seen many other uses for it.

I think C#'s approach to lambdas is slightly better than Java's, although both of them would be improved (for the user - maybe not for the compiler implementer) by being more functional. Effectively they both require you to define a placeholder (respectively a delegate or a one-method interface) for the type. I would rather just be able to specify something of the form (String, String) => int as the signature.
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #41 - Posted 2014-11-27 13:02:06 »

The coalesce operator ?? and the elvis operator ?: are functionally the same.
Exactly Wink Sorry, irony not clearly specified!

I think C#'s approach to lambdas is slightly better than Java's, although both of them would be improved (for the user - maybe not for the compiler implementer) by being more functional. Effectively they both require you to define a placeholder (respectively a delegate or a one-method interface) for the type. I would rather just be able to specify something of the form (String, String) => int as the signature.

Ah, but then how would you write the receiving end, as it were? Gotta start from something in Java - that's just the nature of the beast. I can live with it.

You know what else would be nice? Blocks of declarations:

1  
2  
3  
4  
5  
6  
7  
public static final {
..vars here...
}

private {
...
}

That'd save a lot of boilerplate typing that you literally see everywhere in Java code.

Cas Smiley

Offline atombrot
« Reply #42 - Posted 2014-11-27 13:23:54 »

Quote
All exceptions are unchecked  
Forgot that. Not really a killer feature but I like that too.

Quote
Coalesce
As ? is already an operator I don't find it that evil. But I understand your concerns. Nice thing is that you don't need to use it.
And the definition what it does in c# is very clear.

Quote
For various reasons but mostly because it hides stuff I like to know.

This is as I said before... If you use it wrongly you can hide stuff. But there are still many places you can use to shorten the code without losing information. This is also a very controversial topic in the c#/.net
community, so you are absolutely not alone with your dislike for var. There are some stack overflow discussions about this topics... Probably might give
you some different perspectives on the topic:
http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp  
I need to check but I've read an interview with one of the language creators where he explains why they introduced var and what it should be used for. But not really sure where I read that. Google yielded no good results Wink

Quote
Nullable Types
Sure, you can do something similar in java. It is still nice not having to declare a reference type to hold a value type. Also I find it's notation (<datatype>? <name>) very nice to read, for example if you use it on a data model, entity or so. For example, when you're used to c#, you read "int? value" automatically as "nullable int value". Just a tiny improvement, but if it helps by not having to implement some wrapper classes, I'm happy to use it. (also things like entity framework use this notation to set db constraints).
Quote
Extension methods. Seem like a great idea but I started to get an uneasy feeling about them after a while.
I think this is because they change the way you program. In c# the introduction of extension methods lead to an increase in "functional" style libraries, code examples etc. classic c# (.net 1.1 - 2.0) is very similar to java 1.6/1.7. But since the 3.5 .net framework release a lot has changed. The code is less and less control code (for, if, switch) and more and more just "functions" and lambdas etc. Extension methods are used by many libraries to do very clean, decoupled libraries with nice tools that can be enabled by just including a namespace. Extension methods are now an integral part of the .net framework, and even if you don't create them directly, it is hard to not come around them. So it is good to know how they work (think you already know that), if you are required to use them.


Quote
Type erasure
It's not the end of the world, but actually it can be very handy to be able to instance a type based on the generics parameter. It makes some things (data generation/mocking, factories, IOC, dependency injection) simpler to do. I don't say that you can't do it with java, but with java you require some more boilerplate code to setup certain things. I don't come accross problems with this often, but when I'm usually annoyed about what I have to do to get it running with java. But now I usually try to avoid situation where I would need this info in java.

http://stackoverflow.com/questions/731452/create-instance-of-generic-type

Quote
Things that are in Java which are excellent ideas:   the <> operator, which removes large tracts of crappy redundant typing   static import, ditto lambda syntax, ditto, plus bells and whistles   final, which works in a lovely consistent way   for instead of foreach. Consistent use of keywords!

The <> is actually a nice addition. Would like var even more... But true, c# doesn't have an equivalent (because most cases are handled by var Wink ). Static import: This one is actually nice. But also, in many cases where you would use this, c# uses extension methods, which I find even niftier Wink Lambda Syntax: Don't really know the java lambda syntax. I like the one from c#.  final: Yes, final, readonly, etc. is a bit of a mess.  foreach: I disagree on this. For me this is semantically a different operation (and I don't like it if you have a keyword that does two different things, see final XD). I don't prefer one way or the other, but I don't see a problem with the foreach naming.

Quote
 The ability to specify byte and short literals with suffixes a la longs (dunno if C# has these)

Nope, doesn't look like it: http://stackoverflow.com/questions/5378036/literal-suffix-for-byte-in-net


The coalesce operator ?? and the elvis operator ?: are
functionally the same.

I'm not really sure if this is the same. Can you do this with the elvis
operator:
1  
return valueA ?? valueB ?? valueC ?? valueD ?? valueE ?? valueF ?? "defaultValue";

Stuff I know nothing about:   Yield.
Basically it's a limited form of coroutine. It is very useful for people who want to write implementations of Iterator; I haven't seen many other uses for it.

It is a compiler hack which let's you implement IEnumerable without having to implement anything (the compiler substitutes the required classes). This can be very useful if you are processing large streams of data or lists. Also, nothing java could not do, but it is nice that this is implemented as language feature.


Quote
dynamic
Dynamic is nice because it let's you create dynamic objects. Imagine doing a SELECT * FROM BLA and directly mapping the results to objects, without having to know the column names at run time. The micro orm dapper (which is developed by the stack overflow creators and as far as I know used as orm for stack exchange) makes heavy use of this feature.

Ah I forgot one feature I really love about c#. The using statement. C# has an interface IDisposoble. If you have an object that implements this interface, your can wrap your operations on this objects in an using statement. When the code exits the using block, all used resources are being disposed automatically.
how this looks like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
 
Table table = new Table();
using(var connection = new SqlConnection("."))
using(var command = new SqlCommand(connection))
using(var adapter = new SqlDataAdapter(command)) {    
   command.CommandText = "pr_LoadData";    
   command.CommandType = CommandType.StoredProcedure;
      adapter.fill(table);
}
return table;

In this example, connection,
command and adapter are all classes that have resources that need to be
disposed. This happens automatically, once "adapter.fill" has run.
Offline Spasi
« Reply #43 - Posted 2014-11-27 13:27:19 »

I would rather just be able to specify something of the form (String, String) => int as the signature.

This is the approach taken by Kotlin, if you'd like to try that in a JVM language.

According to this presentation by Brian Goetz, they started exploring lambdas with functional types and later redesigned it to the current functional interface convention. The reasons are solid and the solution surprisingly suitable for the Java ecosystem. Oracle did a fantastic job with Java 8 imho and I'm nothing short of excited for what's coming next (projects Valhalla & Panama).

The slow but steady progress of Java the language is a good thing. There are also tons of options, so many different JVM languages, if we're just talking about language features. Most have perfect interop, you can just use whatever you need to use to be productive and can always fallback to Java when you have to. Exploring different languages and programming approaches is also a good thing.
Offline Roquen

JGO Kernel


Medals: 518



« Reply #44 - Posted 2014-11-27 13:42:43 »

Type erasure sucks.  The only cool thing about type erasure is the hack to convert a checked exception into an unchecked one.  The real problem here (for me) is the "knowns" available to back-end compiler.
Offline Roquen

JGO Kernel


Medals: 518



« Reply #45 - Posted 2014-11-27 13:51:40 »

Actually, I'm lying.  Type erasure just sucks.  Consider type refinement.
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #46 - Posted 2014-11-27 14:58:34 »

Seems Valhalla awaits and is the end to all our woes, and includes endless feasting and maidens too.

Cas Smiley

Offline PaulCunningham

Junior Devvie


Medals: 2



« Reply #47 - Posted 2014-12-02 00:33:13 »

Some of the new features in C#6

http://www.dotnetcurry.com/showarticle.aspx?ID=1042
Offline princec

« JGO Spiffy Duke »


Medals: 1060
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #48 - Posted 2014-12-02 00:39:49 »

Getting increasingly messy, hm. Some good things in there... some facepalmy stuff.

Cas Smiley

Offline Mac70
« Reply #49 - Posted 2014-12-02 01:02:22 »

Most of this stuff seems like attempt to avoid boilerplate code... By adding unreadable code. Smiley
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #50 - Posted 2014-12-02 01:09:09 »

yet, operator overload beats it all.
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #51 - Posted 2014-12-03 05:04:28 »

I didn't meant to start a language vs language thing here, I was genuinely curious to these naming conventions and why C# had to be different from other OOP languages despite sharing the exact same principles.

Actually a project is not a soultion. A solution can hold multiple projects  Wink

Soooo...you mean a workspace like every other IDE has? :p

Actually, I don't think "workspace" is any kind of de-facto standard.  In IntelliJ Idea, for example, the equivalent of an Eclipse "workspace" is called a "project", and the equivalent of an Eclipse "project" is called a "module." 
Offline Roquen

JGO Kernel


Medals: 518



« Reply #52 - Posted 2014-12-05 08:43:49 »

http://blogs.msdn.com/b/dotnet/archive/2014/12/04/introducing-net-core.aspx
Offline atombrot
« Reply #53 - Posted 2014-12-05 08:55:12 »

Quote
NuGet as a first class delivery vehicle

Wohooo Cheesy
Offline GreenLantern101
« Reply #54 - Posted 2014-12-27 19:43:56 »

I've been using C# for three years, and I think the implicit operators are my favorite thing that Java still has to catch up on. And, that Winforms visual editor is kick butt.
Pages: 1 [2]
  ignore  |  Print  
 
 

 
hadezbladez (270 views)
2018-11-16 13:46:03

hadezbladez (147 views)
2018-11-16 13:41:33

hadezbladez (280 views)
2018-11-16 13:35:35

hadezbladez (63 views)
2018-11-16 13:32:03

EgonOlsen (2110 views)
2018-06-10 19:43:48

EgonOlsen (2142 views)
2018-06-10 19:43:44

EgonOlsen (1354 views)
2018-06-10 19:43:20

DesertCoockie (1942 views)
2018-05-13 18:23:11

nelsongames (1594 views)
2018-04-24 18:15:36

nelsongames (2241 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!