I did not know { } are called braces. I called them curly bracket or flower bracket in my verbal communication until I started my career.
In the early days of my engineering college, I wondered why the braces are used and not the square bracket or parenthesis to open and close the class, function and a block of code.
I was practicing C, C++ and COBOL then. Though COBOL did not use braces, it used indent to start from specific column in a row. But, the philosophy of a programming language construct looked relevant in COBOL as in C and C++.
When I practiced Core Java, I could relate the programming constructs and find the similarities in C, C++, COBOL and Java. So with C#, JS, and Shell Scripting.
All this said, the question that remained in me is -- Why these programming languages use braces to open and close the class, function, loop & conditional blocks? Why this similarity?
When I studied Discrete Mathematical Structures in a semester, I could correlate the programming to it. This helped me to reason and interpret why the braces in a programming language.
Pic: Programming languages having braces to open a function. |
{ } and Set
In the Discrete Mathematical Structures, I studied Sets. Further, I learned how the Sets is used to classify and organize the elements in problem solving.
While I was solving a discrete math exercise, this struck to me -- Why the braces to represent a SET? This triggered my associative memory and sparked a question -- Why do I see the braces in C, C++, and C#?
Here is how I interpreted and reasoned my thoughts and questions.
- The SET is to represent the elements of common nature and characteristic which has a purpose. Most of the times it is deterministic.
- But, why the different programming languages use this braces to group a set of instructions? Why not something else to represent programming instructions with a common and deterministic purpose?
- The COBOL do not use braces, but, the philosophy and thought process of braces exist in it as starting from specific column.
- I observed Java and Shell scripting using the braces.
The point and thought to note here is, the two words -- SETS and Groups. I'm studying this since my primary school days. But, I had not realized how they are so much into the computer science!
Today, I observe most programming languages use the braces to group the set of instructions which are for a specific purpose. Be it a class, function, loop or a conditional block, they are within { }.
Between the { }
The instructions written between the braces has an objective. Be it class, function, condition or loop block. That is, I group this instruction as a set.
Why I call it as a set?
- The instructions between the braces have the one purpose.
- Is this SRP -- Single Responsibility Principle? I am yet to put this in a better word!
{ } and Programming Languages
A programming language is created [written] to overcome the limitations of other language in a context.
This blog post was in draft for almost 12 years. I pulled it out today and rephrased a bit, and I published. Feeling good!
ReplyDeleteWhen I read about Python, I observed,
ReplyDelete1. The Python do not use braces
2. Instead, the Python uses : and indent
I read, this is to improve the readability as in the written English.
And, # is used to comment a line in Python. It is said to improve the readability # is used to comment a line.
I remember reading this line and I cannot recall where I read it. It says, learn to read and write English, you will improve in programming with Python. I'm not sure how relevant is this, but I have read it.