Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Automatic Semicolon Insertion in C# #5355

Closed
mkosieradzki opened this issue Sep 19, 2015 · 15 comments
Closed

[Proposal] Automatic Semicolon Insertion in C# #5355

mkosieradzki opened this issue Sep 19, 2015 · 15 comments
Labels

Comments

@mkosieradzki
Copy link
Contributor

This might be a bit controversial :). But is there any good reason why there is no ASI in C#? I am curious if rules for ECMA Script http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf chapter 11.9 could also work in C#?

From practical point of view code without semicolons in Javascript/Typescript looks leaner and it is possible to write thousands of lines of code without using single semicolon (not counting for (;;) )

@HaloFour
Copy link

  1. There is no value in it but a ton of effort would be required.
  2. The language was not designed for it thus this change would introduce ambiguities.
  3. C# shouldn't borrow bad ideas from horribly designed languages (to be fair, very quickly hacked together languages due to stupid management-imposed deadlines, but the end result is the same).

To quote what was already said on this subject from CodePlex:

Let’s put aside the discussion on whether we want to do this for a moment. Obviously there is a long discussion to be had there.

Suppose we were to do this:

C# was not designed to have the semicolon delimiter be optional, so unlike Visual Basic or Python there would be cases where this introduces ambiguity in the code's meaning. The correct thing to do here would be to have the compiler identify ambiguous cases and show a warning. Unfortunately nearly everyone compiles with warnings as errors, so this would essentially be a breaking change. It’s not that C# never introduces breaking changes, but when we do the break needs to be mitigated as much as possible and the potential benefit needs to be huge relative to the size of the break.

My point is: even if we all collectively agreed to make the semicolon delimiter optional, the risk of breaking existing code or the expense of mitigating that risk is significant enough to cause us to rethink things. That doesn't mean it can't be done, just that doing it is going to be expensive. In my opinion, even if we had consensus and everyone wanted to do this, the bang-for-your-buck would not be significant enough compared to other potential features.

@dsaf
Copy link

dsaf commented Sep 19, 2015

How can this be a good idea (11.9.2):

image

@mkosieradzki
Copy link
Contributor Author

@HaloFour Can you please link discussion from codeplex? I am unable to find it by its fragments. But it is definitely worth to be able to track previous discussion on the same issue.
@dsaf scenario you have mentioned:return a + b is really awful in javascript. It will not be the case in C#, because either function is void and return a + b; makes no sense or function is non-void and return; makes no sense

Second case is perfectly reasonable.

Also: I definitely agree that breaking reverse compatibility for such a minor improvement is a horrible idea, but if one day someone decides to make some other breaking changes this one might be worth considering.

@HaloFour
Copy link

Remove requirement for semicolon (;) at end of line
Implicit Semicolon?

It's also come up a couple of times embedded in other issues/discussions.

You can't make the semicolon implicit without adding some form of significant whitespace, which will change the semantics of existing code. The following would become ambiguous:

for (int i = 0; i < 10; i++)
a += b;

In my opinion it doesn't make sense to change the syntax of C# to match that of another language just for the sake of doing so. Adoption of features from other languages is one thing, but making C# smell like JavaScript, Scala or Python syntactically adds nothing to C#.

@dsaf
Copy link

dsaf commented Sep 19, 2015

Also search for "semicolon" in the mega-thread #2136.

@mkosieradzki
Copy link
Contributor Author

@HaloFour @dsaf Thanks guys for referencing those threads.

@iam3yal
Copy link

iam3yal commented Sep 21, 2015

I'm not using semicolons in JavaScript anymore because the code looks much cleaner but I really don't want to see C# look like JavaScript, in C# these semicolons are consistent you either add them or you don't, I don't like the option in-between these two extremes very much, just my opinion. :)

@pawchen
Copy link
Contributor

pawchen commented Sep 21, 2015

One day I wrote the following code in javascript

return
{
  a: 0
}

And I was toasted.

No, please.

@paulomorgado
Copy link

Coding style matters because it reflects the best practices for each language.

In JavaScript, one should write code with opening curly braces on the line of the statement blog belongs to:

return {
    a: 0
};

That solves @DiryBoy's problem. But I wouldn't touch @eyalsk's JavaScrpt code even with a 10 foot pole.

@dsaf
Copy link

dsaf commented Sep 21, 2015

@paulomorgado
Coding style reflects the personal preferences of language creators :). This is how C# handles it:

https://www.jetbrains.com/resharper/webhelp60/Reference__Options__Languages__CSharp__Formatting_Style__Braces_Layout.html

http://stackoverflow.com/questions/3048800/how-can-i-set-visual-studio-to-use-kr-style-bracketing

IMHO that's not @DiryBoy 's problem, it's a bug in the language design. The language should decide whether it has significant whitespace or not.

@iam3yal
Copy link

iam3yal commented Sep 21, 2015

@paulomorgado so don't use NPM please because they are doing that too! why semicolons are so precious to you? my code is well covered with tests so how come semicolons make such a difference to you? it's really interesting to know the reason.

@dsaf
Copy link

dsaf commented Sep 21, 2015

@eyalsk Certain unit tests are merely a workaround for a lacking type system (even in C#). The more can be handed over to static analysis the better.

@iam3yal
Copy link

iam3yal commented Sep 21, 2015

@dsaf Yeah, I was one of the first people to use CodeContracts when it was published, I know about that but @paulomorgado was speaking about my JavaScript code due to lack of semicolons.

If you understand ASI and you make sure your code is properly linted and you do your tests properly there's no need for semicolons, again, when we're speaking about JavaScript.

@JoergWMittag
Copy link

Iff you want to do this, Scala's Semicolon Inference might be a lot better role model than ECMAScript's Automatic Semicolon Insertion.

However, please note:

Even though

  • Scala's syntax is simpler than ECMAScript's (and also C#'s)
  • Scala was designed from the get-go for Semicolon Inference
  • Scala got to learn from ECMAScript
  • Scala was much more carefully designed

there are still cases where Semicolon Inference can surprise you, if you don't follow the official Scala style guide.

@ltlombardi
Copy link

Swift does this very well for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants