Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Specify that caught exceptions should be assigned to a variable named $e #4491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

dantleech
Copy link
Contributor

320 out of 372 catches in the symfony code base use $e.

git grep "catch (.*Exception" | grep "\$e)" | wc -l                                                                                                                                               
320
git grep "catch (.*Exception" | wc -l
372

I think it would be good to make this a standard?

i.e.

try {
    // foo
} catch (SomeException $e) {
    // bar
}

as opposed to using variations: $ex (5 instances) $exception (2 instances), etc.

@linaori
Copy link
Contributor

linaori commented Nov 19, 2014

It's just a minor thing, but I think it will help a lot. One example could be where you have a kernel.exception event being fired, you do a fallback and you end up having 2 exception variables in the same method. There shouldn't be any confusion if you always use $e.

@Nek-
Copy link
Contributor

Nek- commented Nov 19, 2014

👍

@@ -141,6 +141,8 @@ Naming Conventions

* Don't forget to look at the more verbose :doc:`conventions` document for
more subjective naming considerations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This item should then end with a semicolon.

@xabbuh
Copy link
Member

xabbuh commented Nov 19, 2014

In my opinion this makes sense. But let's see what the rest (especially @symfony/deciders) think about this.

@wouterj
Copy link
Member

wouterj commented Nov 19, 2014

-1

  • It doesn't add much value to standardize it
  • A catch block is often a very small block, it wouldn't be hard to find the correct variable name
  • Forcing it means having problems with nested try..catch blocks.
  • Forcing exact variable names is never a good thing imo, as it all depends on the context.

@dantleech
Copy link
Contributor Author

But the standard should be $e, if somebody was writing

try {
   // foo
} catch (Exception $foobar) {
   // bar
}

That would be wrong

So would:

try {
    //
} catch (Exception $excep) {
   //
}

try {
} catch (Exception $ex) {
}

I think the standard should be $e, unless there is reason not to do this.

@javiereguiluz
Copy link
Member

@wouterj your answer surprises me because you are always in for cleaning inconsistencies :) I think @dantleech has reported a hard to justify inconsistency: Symfony uses $e "almost always" ... why not make it just "always"?

@linaori
Copy link
Contributor

linaori commented Nov 19, 2014

I agree with @wouterj about dictating conventions for variables, but I also agree with @javiereguiluz. I think that we should make an exception for $e.

  • How many cases of nested exceptions are there in Symfony packages?
  • What would the recommended variable naming be there?

@dantleech
Copy link
Contributor Author

I can find one, which uses $e2

https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40

using a very unconclusive search that assumes that nested try keywords are within the following five lines of the catch.

I would say that this should be a guideline. If you must have nested exceptions you should make an exception to the rule. It seems to happen rarely enough.

@wouterj
Copy link
Member

wouterj commented Feb 19, 2015

@symfony/deciders what are your opinion on this proposed CS?

@wouterj
Copy link
Member

wouterj commented May 17, 2015

ping @stof @fabpot @jakzal @weaverryan can you please share your opinion on this topic?

@jakzal
Copy link
Contributor

jakzal commented May 17, 2015

👎 I would not put this into coding standards. It should be up to the author of code to give proper names so the code is readable, and that's not something you can standardise.

However, I'd vote 👍 if someone sent a PR to symfony/symfony fixing the inconsistency. Especially that there's only few instances that don't follow this pattern.

@javiereguiluz
Copy link
Member

This pull request can be closed because doc managers and Symfony deciders are against it. Moreover, the idea proposed by @dantleech to update the exception variables to $e across the framework has been implemented in symfony/symfony#14937 and it's waiting for the Symfony deciders opinion. Thanks.

@xabbuh
Copy link
Member

xabbuh commented Jun 11, 2015

I agree. Though thanks to @dantleech for starting the discussion and to @javiereguiluz for updating the code.

@xabbuh xabbuh closed this Jun 11, 2015
fabpot added a commit to symfony/symfony that referenced this pull request Jun 15, 2015
…guiluz)

This PR was squashed before being merged into the 2.3 branch (closes #14937).

Discussion
----------

Standardize the name of the exception variables

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See symfony/symfony-docs#4491 for the context of this change.

In Symfony source code there are 410 `try ... catch` blocks. More than 95% of them use `$e` as the name of the exception variable. After applying these changes, 407 out of 410 variables are named `$e`.

These are the three cases where I didn't change the name of the `$e` variable:

  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40. It uses `$e2` as the name of the nested variable.
  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php#L82. I changed the name of the `$ex` variable to `$e2` to match the previous syntax.
  * https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Console/Helper/DialogHelper.php#L463. I don't know if it's safe to change the name of the `$error` exception variable.

Commits
-------

e8b924c Standardize the name of the exception variables
fabpot added a commit to symfony/config that referenced this pull request Jun 15, 2015
…guiluz)

This PR was squashed before being merged into the 2.3 branch (closes #14937).

Discussion
----------

Standardize the name of the exception variables

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See symfony/symfony-docs#4491 for the context of this change.

In Symfony source code there are 410 `try ... catch` blocks. More than 95% of them use `$e` as the name of the exception variable. After applying these changes, 407 out of 410 variables are named `$e`.

These are the three cases where I didn't change the name of the `$e` variable:

  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40. It uses `$e2` as the name of the nested variable.
  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php#L82. I changed the name of the `$ex` variable to `$e2` to match the previous syntax.
  * https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Console/Helper/DialogHelper.php#L463. I don't know if it's safe to change the name of the `$error` exception variable.

Commits
-------

e8b924c Standardize the name of the exception variables
fabpot added a commit to symfony/console that referenced this pull request Jun 15, 2015
…guiluz)

This PR was squashed before being merged into the 2.3 branch (closes #14937).

Discussion
----------

Standardize the name of the exception variables

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See symfony/symfony-docs#4491 for the context of this change.

In Symfony source code there are 410 `try ... catch` blocks. More than 95% of them use `$e` as the name of the exception variable. After applying these changes, 407 out of 410 variables are named `$e`.

These are the three cases where I didn't change the name of the `$e` variable:

  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40. It uses `$e2` as the name of the nested variable.
  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php#L82. I changed the name of the `$ex` variable to `$e2` to match the previous syntax.
  * https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Console/Helper/DialogHelper.php#L463. I don't know if it's safe to change the name of the `$error` exception variable.

Commits
-------

e8b924c Standardize the name of the exception variables
fabpot added a commit to symfony/dependency-injection that referenced this pull request Jun 15, 2015
…guiluz)

This PR was squashed before being merged into the 2.3 branch (closes #14937).

Discussion
----------

Standardize the name of the exception variables

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See symfony/symfony-docs#4491 for the context of this change.

In Symfony source code there are 410 `try ... catch` blocks. More than 95% of them use `$e` as the name of the exception variable. After applying these changes, 407 out of 410 variables are named `$e`.

These are the three cases where I didn't change the name of the `$e` variable:

  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40. It uses `$e2` as the name of the nested variable.
  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php#L82. I changed the name of the `$ex` variable to `$e2` to match the previous syntax.
  * https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Console/Helper/DialogHelper.php#L463. I don't know if it's safe to change the name of the `$error` exception variable.

Commits
-------

e8b924c Standardize the name of the exception variables
fabpot added a commit to symfony/http-kernel that referenced this pull request Jun 15, 2015
…guiluz)

This PR was squashed before being merged into the 2.3 branch (closes #14937).

Discussion
----------

Standardize the name of the exception variables

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See symfony/symfony-docs#4491 for the context of this change.

In Symfony source code there are 410 `try ... catch` blocks. More than 95% of them use `$e` as the name of the exception variable. After applying these changes, 407 out of 410 variables are named `$e`.

These are the three cases where I didn't change the name of the `$e` variable:

  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40. It uses `$e2` as the name of the nested variable.
  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php#L82. I changed the name of the `$ex` variable to `$e2` to match the previous syntax.
  * https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Console/Helper/DialogHelper.php#L463. I don't know if it's safe to change the name of the `$error` exception variable.

Commits
-------

e8b924c Standardize the name of the exception variables
symfony-splitter pushed a commit to symfony/config that referenced this pull request Feb 23, 2016
…guiluz)

This PR was squashed before being merged into the 2.3 branch (closes #14937).

Discussion
----------

Standardize the name of the exception variables

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See symfony/symfony-docs#4491 for the context of this change.

In Symfony source code there are 410 `try ... catch` blocks. More than 95% of them use `$e` as the name of the exception variable. After applying these changes, 407 out of 410 variables are named `$e`.

These are the three cases where I didn't change the name of the `$e` variable:

  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php#L40. It uses `$e2` as the name of the nested variable.
  * Nested exception in https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/TwigEngine.php#L82. I changed the name of the `$ex` variable to `$e2` to match the previous syntax.
  * https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/Console/Helper/DialogHelper.php#L463. I don't know if it's safe to change the name of the `$error` exception variable.

Commits
-------

e8b924c Standardize the name of the exception variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants