Questions tagged [syntax]

Syntax refers to the set of rules that define how to write a correctly structured program in a language. It explicitly does not deal with the program's meaning or interpretation.

The syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. The syntax of a language defines its surface form. Text-based programming languages are based on sequences of characters, while visual programming languages are based on the spatial layout and connections between symbols (which may be textual or graphical).

The syntax of a language describes the form of a valid program, but does not provide any information about the meaning of the program or the results of executing that program. The meaning given to a combination of symbols is handled by semantics (either formal or hard-coded in a reference implementation). Not all syntactically correct programs are semantically correct.

From Wikipedia

193 questions
311
votes
16 answers

Why are shortcuts like x += y considered good practice?

I have no idea what these are actually called, but I see them all the time. The Python implementation is something like: x += 5 as a shorthand notation for x = x + 5. But why is this considered good practice? I've run across it in nearly every book…
Fomite
  • 2,616
  • 6
  • 18
  • 20
259
votes
21 answers

Are `break` and `continue` bad programming practices?

My boss keeps mentioning nonchalantly that bad programmers use break and continue in loops. I use them all the time because they make sense; let me show you the inspiration: function verify(object) { if (object->value < 0) return false; if…
160
votes
1 answer

Why there is no markdown for underline?

I am wondering why there is no markdown syntax for underline? I know that basic html tags can be embedded to achieve this but I am trying to understand why underline got omitted when bold and italics exists
ganessh
  • 1,719
  • 2
  • 10
  • 5
147
votes
15 answers

Why did memory-managed languages like Java, Javascript, and C# retain the `new` keyword?

The new keyword in languages like Java, Javascript, and C# creates a new instance of a class. This syntax seems to have been inherited from C++, where new is used specifically to allocate a new instance of a class on the heap, and return a pointer…
Channel72
  • 2,475
  • 5
  • 27
  • 28
136
votes
14 answers

Why are statements in many programming languages terminated by semicolons?

Is there a reason that a semi-colon was chosen as a line terminator instead of a different symbol? I want to know the history behind this decision, and hope the answers will lead to insights that may influence future decisions.
A Coder
  • 1,343
  • 2
  • 9
  • 17
100
votes
3 answers

Why do programming languages, especially C, use curly braces and not square ones?

The definition of "C-Style language" can practically be simplified down to "uses curly braces ({})." Why do we use that particular character (and why not something more reasonable, like [], which doesn't require the shift key at least on US…
SomeKittens
  • 4,220
  • 6
  • 31
  • 38
96
votes
5 answers

Why store a function inside a python dictionary?

I'm a python beginner, and I just learned a technique involving dictionaries and functions. The syntax is easy and it seems like a trivial thing, but my python senses are tingling. Something tells me this is a deep and very pythonic concept and I'm…
mdeutschmtl
  • 1,079
  • 1
  • 8
  • 6
95
votes
13 answers

What's the difference between syntax and semantics?

I've always thought that referring to the syntax of a language was the same as referring to the semantics of a language. But I've been informed that apparently that's not the case. What's the difference?
gsgx
  • 1,767
  • 2
  • 16
  • 17
83
votes
6 answers

Why does the type go after the variable name in modern programming languages?

Why is it that in nearly all modern programming languages (Go, Rust, Kotlin, Swift, Scala, Nim, even Python last version) types always come after the variable name in the variable declaration, and not before? Why x: int = 42 and not int x = 42? Is…
Andre Polykanine
  • 957
  • 1
  • 6
  • 6
79
votes
8 answers

A practical use of "yield" keyword in C#

After almost 4 years of experience, I haven't seen a code where yield keyword is used. Can somebody show me a practical usage (along explanation) of this keyword, and if so, aren't there other ways easier to fullfill what it can do?
Saeed Neamati
  • 18,142
  • 23
  • 87
  • 125
73
votes
5 answers

Why do C# developers newline opening brackets?

I've spent most of the last several years working mainly with C# and SQL. Every programmer I've worked with over that time was in the habit of placing the opening brace of a function or control flow statement on a new line. So ... public void…
Bob Tway
  • 3,606
  • 3
  • 21
  • 26
72
votes
6 answers

Why is the Select before the From in an SQL query?

This is something that bothered me a lot at school. Five years ago, when I learned SQL, I always wondered why we first specify the fields we want and then where we want them from. According to my idea, we should write: From Employee e Select…
Cyril Gandon
  • 1,296
  • 1
  • 11
  • 17
71
votes
3 answers

Is there a performance benefit to using the method reference syntax instead of lambda syntax in Java 8?

Do method references skip the overhead of the lambda wrapper? Might they in the future? According to the Java Tutorial on Method References: Sometimes... a lambda expression does nothing but call an existing method. In those cases, it's often…
GlenPeterson
  • 14,890
  • 6
  • 47
  • 75
70
votes
2 answers

Why do bitwise operators have lower priority than comparisons?

Could someone explain the rationale, why in a bunch of most popular languages (see note below) comparison operators (==, !=, <, >, <=, >=) have higher priority than bitwise operators (&, |, ^, ~)? I don't think I've ever encountered a use where this…
69
votes
5 answers

What's the difference between implementing an Interface explicitly or implicitly?

In Visual Studio I can right-click on an interface and choose to Implement Interface, or Implement Interface Explicitly. public class Test : ITest { public string Id // Generated by Implement Interface { get { throw new…
Rachel
  • 23,979
  • 16
  • 91
  • 159
1
2 3
12 13