-1

While not finding a "foreachelse" in the PHP manual, I came up with my own.

Would you say the following is recommended? I am a fan of compact code, and I think it has good readability. Plus it works.

$consequences = array("You ", "will ", "burn ", "for ", "this!");

if(!empty($consequences)) foreach($consequences as $consequence) { 
    // 100 lines of code to be inserted here
    echo $consequence;
} else echo "No consequences";

Before testing this I actually thought I could not use curly brackets inside a "no curly bracket statement", but it looks like you can.

Are there any arguments for using curly brakets in this case?

OnklMaps
  • 807
  • 8
  • 17
  • 2
    That is not recommended at all. Thats an unreadable mess. – castis Jun 25 '18 at 22:09
  • Possible duplicate of [PHP - If/else, for, foreach, while - without curly braces?](https://stackoverflow.com/questions/8726339/php-if-else-for-foreach-while-without-curly-braces) – chiliNUT Jun 25 '18 at 22:39
  • 1
    What you consider easy to read, others may rather disagree with. Be verbose unless it affects performance, and leave the compacting to code minifiers. The interpreter doesn't care about your conventions (if the code is syntactically correct, it will run it without looking at whether it's pretty or not). If you ever need to write code that others will need to read, or maintain, or work on at the same time as you, this habit is going to make it very hard to do that job. – Mike 'Pomax' Kamermans Jun 25 '18 at 23:13

1 Answers1

1

While to you this perhaps is compact and neat, it doesn't follow the convention on how these things are written. Your code will be hard to read by other developers. While there is no absolute "standard" on how to write code, there are several conventions that are adopted by a large percentage of the world.

One line if (something) do; statements are evil, break it into two lines even if you do not need to use curly braces:

if (something)
  do;

This not only is easier to read but makes debugging easier as a break point can be placed on the "do" line if a debugger is being used, or if stepping the code it will be clear if the expression evaluated true or not.

In your case this would become:

if(!empty($consequences))
  foreach($consequences as $consequence) { 
    // 100 lines of code to be inserted here
    echo $consequence;
  }
else
  echo "No consequences";

While this is not "compact" as you would call it, it is maintainable, and very easy to see the code path which is your primary objective when writing clear code that is easy to debug later.

Also note that since PHP5.4 the array keyword is no longer required in PHP, you can use the following:

$consequences = ["You ", "will ", "burn ", "for ", "this!"];

Which is not only more "compact" but more standard inline with other languages such as C, and as such very readable.

In short, if you are not willing to adjust your viewpoint on what is "neat" code you are going to struggle in this industry. It will be hard for others to work with you, and it will be hard for you to read code written by others.

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • So, in your opinion its the usage of oneline`if(something) do;` that is the evil here, not also the omitted curly braces? Or would you recommend to use curly brackets in my case? – OnklMaps Jun 26 '18 at 07:35
  • 1
    @OnklMaps it is up to you to decide what coding style you would like to use, personally in this simple example I would not use them, but that is my personal preference, other developers will insist on their usage. What you do need to do however is ensure you make your code path clear with the use of new lines and indentation. – Geoffrey Jun 26 '18 at 18:31