Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks - curly braces/no curly braces? [closed]

Tags:

php

block

When I started developing, I followed tutorials that always used {} (curly braces) to enclose blocks. However, when I began looking at other peoples code (classes on GitHub for example, or just more code in general than what a basic tutorial would show), however I've also seen block statements without being enclosed in {}, for example;

if($var < 15)
     $string = 'Hello, Jimmy!';
elseif($var >= 15)
     $string = 'Hello, Anne!';

Is the same as

if($var < 15) { 
    $string = 'Hello, Jimmy!';
} elseif($var >= 15) {
    $string = 'Hello, Anne!';
}

I've never used blocks not enclosed in {}, however I used them today and I'm starting to see the efficiency of doing so (it looks a lot cleaner too, as I'll often find my functions riddled with {} from loops, conditionals etc.

What I'm asking is;

a) are there any limitations on blocks without curly braces (; I noticed my IDE dropped back from an indent after I enter a single line and returned after an if() conditional?

b) are there any best practices to be had, when not using {}?

Any answers, specifically those inc. background/docs on the convention of curly brace usage for blocks vs. not using them would be greatly appreciated, as I'd really like to understand the usage of curly braces :)!

like image 788
Avicinnian Avatar asked Dec 12 '11 04:12

Avicinnian


People also ask

What is an open curly brace?

An open curly bracket is { and a close curly bracket is } Curly brackets are used to mark what code is in a certain block. To type a curly bracket, hold down the SHIFT key and press the key that has [ and { on it. You can find that key on your keyboard directly to the right of P.

Why does Python not have curly braces?

Python does not mandate how you indent (two spaces or four, tabs or spaces - but not both), just that you do it consistently. Those that get used to the Python way of doing things tend to start seeing curly braces as unnecessary line noise that clutters code.

What is enclosed in curly braces?

How are curly brackets used? Curly brackets are commonly used in programming languages such as C, Java, Perl, and PHP to enclose groups of statements or blocks of code.

Can we skip the curly braces?

So we can omit curly braces only there is a single statement under if-else or loop. Here in both of the cases, the Line1 is in the if block but Line2 is not in the if block. So if the condition fails, or it satisfies the Line2 will be executed always.


2 Answers

Best practice is to always use them. It's far too easy for another developer to come along and add a line of code or delete a line of code and completely break the conditional unintentionally.

like image 183
AlienWebguy Avatar answered Oct 17 '22 11:10

AlienWebguy


You can omit the {} for one single line:

if(something)
  do something else

however you cannot omit it and have it keep going like so:

if(something)
   do one thing
   do another
   do some more

The example above would only have 1 conditional element (the 'do one thing'). The rest would just run unconditionally.

And yes I've seen the sloppy method before without the {}, I myself prefer using {} to separate the logic, and its easier to read.

So stick to using {} in your code.

like image 42
Jakub Avatar answered Oct 17 '22 12:10

Jakub