DEV Community

Ali Aryani
Ali Aryani

Posted on

Semicolon-First: A Psychotic Yet Interesting Way of Writing Code!

How the Idea Came to Me

When I was 9, I was dreaming of a programming language in which every line starts with a semicolon. Weird huh? It gets more weird when you realise back then I did not know anything about programming languages!

Three years ago when I was learning a little bit of JavaScript, I read a note about comma-first writing style. If you don't know what it is, look at the following example:

normal_way = [
   'item1',
   'item2',
   'item3'
]
# now let's write it with comma-first
comma_first_way = [
    'item1'
    , 'item2'
    , 'item3'
]
Enter fullscreen mode Exit fullscreen mode

As you see in the example above, in comma-first, instead of ending a line with a comma, we start the next line with a comma!
There's nothing wrong with this way of coding, it's your personal choice if you want to use comma-first.

Anyways, What is semicolon-first and what does it have to do with what I've said so far?

Semicolon-First

It's creative, it's lovely, it's a mess!

A month ago I thought to myself: "Let's put this childhood dream into practice, the compiler won't care!".
Writing code in this new way seemed easy at first, but it was a bit tricky to implement.
To start, look at the following C++ example, which is normal:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "What's your name? => ";
    cin >> name;
    cout << "Hello " << name << "!" << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

After trying different ways, I finally invented the semicolon-first style:

#include <iostream>
#include <string>
using namespace std

; int main() {
    string name
    ; cout << "What's your name? => "
    ; cin >> name
    ; cout << "Hello " << name << "!" << endl

    ; return 0 ;
}
Enter fullscreen mode Exit fullscreen mode
  • except the first line of each block, every other line starts with a semicolon
  • the last line starts and ends with a semicolon ( spaces should be there to keep the balance )

__Question is, should I use it? Is it better than the traditional way? Is it the future? Let's see.

Semicolon-First Pros and Cons

Why is it good ?

  • By using semicolon first you will never forget to put a semicolon at the end of a statement, because it comes first!
  • Commenting in this style is somehow easier. You can break a statement into small chunks and write a comment about each one. look at the following example:
#include <iostream>
using namespace std

; int main() {
    int age 
    ; cout << "How old are you? " // just asking
       << endl // adding new line character
    ; cin >> age // get the age 
    ; cout << "So you're " << age // fore-telling the obvious!
        << endl << "Hmm..." // thinking
        << endl << "interesting!" << endl
    ; return 0 ;
}
Enter fullscreen mode Exit fullscreen mode

I shall mention that you can do this in the traditional way, too. So it's not a big deal.

Why is it bad?

  • World of programming does not like a rule breaker like this!
  • There is a chance of getting roasted for using this style!
  • Other programmers might see your code as confusing.
  • Code formatting tools might change to the traditional way anyways!
  • Others will have problems contributing in your projects

Conclusion

Writing this way is a personal choice, no one can force you to do what you don't like. If you think this style is better, use it.
I personally do not use this style anymore, since I can't risk my code being unclean. But if the world decides to use semicolon first, so will I!
I will be happy to hear you comments and ideas on this.

Create. Love. Improve.

  • A. Aryani

Top comments (0)