Why do most programming languages use comma before newline, instead of after newline?









up vote
0
down vote

favorite












To explain why I've come up with this question, I'll explain python3's commonly used multiline list/dictionary notations and what actual syntax definition looks like.



This is the most commonly used multiline list notation in python3:



list_variable = [
'value1',
'value2',
# The last comma after 'value2' is sometimes omitted.
]


And, to understand how this notation is recognized by parser, we need to look at grammar definition(link).



In the definition of python, list-like definitions are defined similar to this:



list_like: '[' [element (',' element)* [','] ] ']'


Let me explain this a little bit more, just in case you're not familiar with syntax definition or I'm a bad summarizer:



  1. '' means it is actually a character. For example, '[' and ']' are list's ending or beginning in pure character, which will be [ 'value1', 'value2', ] in actual code.


  2. element is their type's expected elements. for example, If it's a list, it will be a single variable. If it's a dictionary, it will be key ':' value.


  3. means all things inside that are optional.


  4. () means all things inside that are in one set. i.e. It all comes together.


  5. * means a syntax set preceding that may occur 0 or more times. In this example, (',' element) may be omitted, but it also may occur 3 times.


  6. I almost forgot to mention, that these rules are applied after whitespace managing is done.



Okay, so let's have a look at list's parsing example:




  1. [1]



    will be parsed into '[' element ']'




  2. [1, ]



    will be '[' element ',' ']'




  3. [1, 2]



    will be '[' element (',' element) ']'




  4. [1, 2, ]



    will be '[' element (',' element) ',' ']'



So, as you see, in the example 3 the comma is paired with 2, not 1.
Also, in the example the last comma is not paired with 2 - it actually is paired with nothing in terms of syntax definition.




Now the actual question is:
Is there more reasons to use comma before newline, except for the fact that most languages were using that commonly?
I think it is sensible to use comma after newline, for these reasons:



  1. It's more similar to actual syntax definition.


  2. It ensures that there will be always exact number of commas(number of elems - 1), and that helps when you're dealing with languages that hates additional leading comma, for example SQL.



  3. It also is git-friendly, like comma-before notation: If you add a new element to the list, git diff will show + , new_element only.



    In fact, in comma-before-newline notation if someone forgot to add a comma after old_element(Or because the language doesn't allow additional comma), someone who adds an element after that will see a git log like this:



- old_element
+ old_element,
+ new_element,









share|improve this question





















  • What if you want to add something to the front of the list?
    – user2357112
    Nov 11 at 6:11










  • @user2357112 [1n,2] => [0n,1n,2], and it might show misleading git log while comma before is [1,n2] => [0,n1,n2] and creates a cleaner git log. That's one point I didn't notice. Thank you.
    – ik1ne
    Nov 11 at 6:18










  • Between the opening and closing [ and ] brackets defining a sequence, newlines don't have much significance in Python—so I think your analysis doesn't apply. What you're talking about code layout and how it's done is hopefully by usually following some convention like PEP 8 - Style Guide for Python Code. It's most a just matter of taste and not so much about parsing efficiency. No doubt there might be some ways that would make certain modifications easier. Fine, do it, the guidelines are only suggestions.
    – martineau
    Nov 11 at 7:15














up vote
0
down vote

favorite












To explain why I've come up with this question, I'll explain python3's commonly used multiline list/dictionary notations and what actual syntax definition looks like.



This is the most commonly used multiline list notation in python3:



list_variable = [
'value1',
'value2',
# The last comma after 'value2' is sometimes omitted.
]


And, to understand how this notation is recognized by parser, we need to look at grammar definition(link).



In the definition of python, list-like definitions are defined similar to this:



list_like: '[' [element (',' element)* [','] ] ']'


Let me explain this a little bit more, just in case you're not familiar with syntax definition or I'm a bad summarizer:



  1. '' means it is actually a character. For example, '[' and ']' are list's ending or beginning in pure character, which will be [ 'value1', 'value2', ] in actual code.


  2. element is their type's expected elements. for example, If it's a list, it will be a single variable. If it's a dictionary, it will be key ':' value.


  3. means all things inside that are optional.


  4. () means all things inside that are in one set. i.e. It all comes together.


  5. * means a syntax set preceding that may occur 0 or more times. In this example, (',' element) may be omitted, but it also may occur 3 times.


  6. I almost forgot to mention, that these rules are applied after whitespace managing is done.



Okay, so let's have a look at list's parsing example:




  1. [1]



    will be parsed into '[' element ']'




  2. [1, ]



    will be '[' element ',' ']'




  3. [1, 2]



    will be '[' element (',' element) ']'




  4. [1, 2, ]



    will be '[' element (',' element) ',' ']'



So, as you see, in the example 3 the comma is paired with 2, not 1.
Also, in the example the last comma is not paired with 2 - it actually is paired with nothing in terms of syntax definition.




Now the actual question is:
Is there more reasons to use comma before newline, except for the fact that most languages were using that commonly?
I think it is sensible to use comma after newline, for these reasons:



  1. It's more similar to actual syntax definition.


  2. It ensures that there will be always exact number of commas(number of elems - 1), and that helps when you're dealing with languages that hates additional leading comma, for example SQL.



  3. It also is git-friendly, like comma-before notation: If you add a new element to the list, git diff will show + , new_element only.



    In fact, in comma-before-newline notation if someone forgot to add a comma after old_element(Or because the language doesn't allow additional comma), someone who adds an element after that will see a git log like this:



- old_element
+ old_element,
+ new_element,









share|improve this question





















  • What if you want to add something to the front of the list?
    – user2357112
    Nov 11 at 6:11










  • @user2357112 [1n,2] => [0n,1n,2], and it might show misleading git log while comma before is [1,n2] => [0,n1,n2] and creates a cleaner git log. That's one point I didn't notice. Thank you.
    – ik1ne
    Nov 11 at 6:18










  • Between the opening and closing [ and ] brackets defining a sequence, newlines don't have much significance in Python—so I think your analysis doesn't apply. What you're talking about code layout and how it's done is hopefully by usually following some convention like PEP 8 - Style Guide for Python Code. It's most a just matter of taste and not so much about parsing efficiency. No doubt there might be some ways that would make certain modifications easier. Fine, do it, the guidelines are only suggestions.
    – martineau
    Nov 11 at 7:15












up vote
0
down vote

favorite









up vote
0
down vote

favorite











To explain why I've come up with this question, I'll explain python3's commonly used multiline list/dictionary notations and what actual syntax definition looks like.



This is the most commonly used multiline list notation in python3:



list_variable = [
'value1',
'value2',
# The last comma after 'value2' is sometimes omitted.
]


And, to understand how this notation is recognized by parser, we need to look at grammar definition(link).



In the definition of python, list-like definitions are defined similar to this:



list_like: '[' [element (',' element)* [','] ] ']'


Let me explain this a little bit more, just in case you're not familiar with syntax definition or I'm a bad summarizer:



  1. '' means it is actually a character. For example, '[' and ']' are list's ending or beginning in pure character, which will be [ 'value1', 'value2', ] in actual code.


  2. element is their type's expected elements. for example, If it's a list, it will be a single variable. If it's a dictionary, it will be key ':' value.


  3. means all things inside that are optional.


  4. () means all things inside that are in one set. i.e. It all comes together.


  5. * means a syntax set preceding that may occur 0 or more times. In this example, (',' element) may be omitted, but it also may occur 3 times.


  6. I almost forgot to mention, that these rules are applied after whitespace managing is done.



Okay, so let's have a look at list's parsing example:




  1. [1]



    will be parsed into '[' element ']'




  2. [1, ]



    will be '[' element ',' ']'




  3. [1, 2]



    will be '[' element (',' element) ']'




  4. [1, 2, ]



    will be '[' element (',' element) ',' ']'



So, as you see, in the example 3 the comma is paired with 2, not 1.
Also, in the example the last comma is not paired with 2 - it actually is paired with nothing in terms of syntax definition.




Now the actual question is:
Is there more reasons to use comma before newline, except for the fact that most languages were using that commonly?
I think it is sensible to use comma after newline, for these reasons:



  1. It's more similar to actual syntax definition.


  2. It ensures that there will be always exact number of commas(number of elems - 1), and that helps when you're dealing with languages that hates additional leading comma, for example SQL.



  3. It also is git-friendly, like comma-before notation: If you add a new element to the list, git diff will show + , new_element only.



    In fact, in comma-before-newline notation if someone forgot to add a comma after old_element(Or because the language doesn't allow additional comma), someone who adds an element after that will see a git log like this:



- old_element
+ old_element,
+ new_element,









share|improve this question













To explain why I've come up with this question, I'll explain python3's commonly used multiline list/dictionary notations and what actual syntax definition looks like.



This is the most commonly used multiline list notation in python3:



list_variable = [
'value1',
'value2',
# The last comma after 'value2' is sometimes omitted.
]


And, to understand how this notation is recognized by parser, we need to look at grammar definition(link).



In the definition of python, list-like definitions are defined similar to this:



list_like: '[' [element (',' element)* [','] ] ']'


Let me explain this a little bit more, just in case you're not familiar with syntax definition or I'm a bad summarizer:



  1. '' means it is actually a character. For example, '[' and ']' are list's ending or beginning in pure character, which will be [ 'value1', 'value2', ] in actual code.


  2. element is their type's expected elements. for example, If it's a list, it will be a single variable. If it's a dictionary, it will be key ':' value.


  3. means all things inside that are optional.


  4. () means all things inside that are in one set. i.e. It all comes together.


  5. * means a syntax set preceding that may occur 0 or more times. In this example, (',' element) may be omitted, but it also may occur 3 times.


  6. I almost forgot to mention, that these rules are applied after whitespace managing is done.



Okay, so let's have a look at list's parsing example:




  1. [1]



    will be parsed into '[' element ']'




  2. [1, ]



    will be '[' element ',' ']'




  3. [1, 2]



    will be '[' element (',' element) ']'




  4. [1, 2, ]



    will be '[' element (',' element) ',' ']'



So, as you see, in the example 3 the comma is paired with 2, not 1.
Also, in the example the last comma is not paired with 2 - it actually is paired with nothing in terms of syntax definition.




Now the actual question is:
Is there more reasons to use comma before newline, except for the fact that most languages were using that commonly?
I think it is sensible to use comma after newline, for these reasons:



  1. It's more similar to actual syntax definition.


  2. It ensures that there will be always exact number of commas(number of elems - 1), and that helps when you're dealing with languages that hates additional leading comma, for example SQL.



  3. It also is git-friendly, like comma-before notation: If you add a new element to the list, git diff will show + , new_element only.



    In fact, in comma-before-newline notation if someone forgot to add a comma after old_element(Or because the language doesn't allow additional comma), someone who adds an element after that will see a git log like this:



- old_element
+ old_element,
+ new_element,






python syntax programming-languages






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 5:58









ik1ne

766




766











  • What if you want to add something to the front of the list?
    – user2357112
    Nov 11 at 6:11










  • @user2357112 [1n,2] => [0n,1n,2], and it might show misleading git log while comma before is [1,n2] => [0,n1,n2] and creates a cleaner git log. That's one point I didn't notice. Thank you.
    – ik1ne
    Nov 11 at 6:18










  • Between the opening and closing [ and ] brackets defining a sequence, newlines don't have much significance in Python—so I think your analysis doesn't apply. What you're talking about code layout and how it's done is hopefully by usually following some convention like PEP 8 - Style Guide for Python Code. It's most a just matter of taste and not so much about parsing efficiency. No doubt there might be some ways that would make certain modifications easier. Fine, do it, the guidelines are only suggestions.
    – martineau
    Nov 11 at 7:15
















  • What if you want to add something to the front of the list?
    – user2357112
    Nov 11 at 6:11










  • @user2357112 [1n,2] => [0n,1n,2], and it might show misleading git log while comma before is [1,n2] => [0,n1,n2] and creates a cleaner git log. That's one point I didn't notice. Thank you.
    – ik1ne
    Nov 11 at 6:18










  • Between the opening and closing [ and ] brackets defining a sequence, newlines don't have much significance in Python—so I think your analysis doesn't apply. What you're talking about code layout and how it's done is hopefully by usually following some convention like PEP 8 - Style Guide for Python Code. It's most a just matter of taste and not so much about parsing efficiency. No doubt there might be some ways that would make certain modifications easier. Fine, do it, the guidelines are only suggestions.
    – martineau
    Nov 11 at 7:15















What if you want to add something to the front of the list?
– user2357112
Nov 11 at 6:11




What if you want to add something to the front of the list?
– user2357112
Nov 11 at 6:11












@user2357112 [1n,2] => [0n,1n,2], and it might show misleading git log while comma before is [1,n2] => [0,n1,n2] and creates a cleaner git log. That's one point I didn't notice. Thank you.
– ik1ne
Nov 11 at 6:18




@user2357112 [1n,2] => [0n,1n,2], and it might show misleading git log while comma before is [1,n2] => [0,n1,n2] and creates a cleaner git log. That's one point I didn't notice. Thank you.
– ik1ne
Nov 11 at 6:18












Between the opening and closing [ and ] brackets defining a sequence, newlines don't have much significance in Python—so I think your analysis doesn't apply. What you're talking about code layout and how it's done is hopefully by usually following some convention like PEP 8 - Style Guide for Python Code. It's most a just matter of taste and not so much about parsing efficiency. No doubt there might be some ways that would make certain modifications easier. Fine, do it, the guidelines are only suggestions.
– martineau
Nov 11 at 7:15




Between the opening and closing [ and ] brackets defining a sequence, newlines don't have much significance in Python—so I think your analysis doesn't apply. What you're talking about code layout and how it's done is hopefully by usually following some convention like PEP 8 - Style Guide for Python Code. It's most a just matter of taste and not so much about parsing efficiency. No doubt there might be some ways that would make certain modifications easier. Fine, do it, the guidelines are only suggestions.
– martineau
Nov 11 at 7:15

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246245%2fwhy-do-most-programming-languages-use-comma-before-newline-instead-of-after-new%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246245%2fwhy-do-most-programming-languages-use-comma-before-newline-instead-of-after-new%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3