Ruby – How does “Assignment Branch Condition size for index is too high” work

code metrics rubocop ruby

Rubocop is always report the error:

app/controllers/account_controller.rb:5:3: C: Assignment Branch Condition size for index is too high. [30.95/24]

How to fix it? Anyone has good idea?

Best Answer

The ABC size [1] [2] is

computed by counting the number of assignments, branches and conditions for a section of code. The counting rules in the original C++ Report article were specifically for the C, C++ and Java languages.

The previous links details what counts for A, B, and C. ABC size is a scalar magnitude, reminiscent of a triangulated relationship:

Actually, a quick google on the error shows that the first indexed page is the Rubocop docs for the method that renders that message .

Your repo or analysis tool will define a threshold amount when the warning is triggered.

Calculating, if you like self-inflicting....

Your code calcs as

That's a 'blind' calculation with values I've made up ( 1 s). However, you can see that the error states numbers that probably now make sense as your ABC and the threshold:

So cop threshold is 24 and your ABC size is 30.95 . This tells us that the rubocop engine assign different numbers for A, B, and C. As well, different kinds or Assignments (or B or C) could have different values, too. E.G. a 'normal' assignment x = y is perhaps scored lower than a chained assignment x = y = z = r .

tl;dr answer

At this point, you probably have a fairly clear idea of how to reduce your ABC size. If not:

  • a simple way it to take the conditional used for your elsif and place it in a helper method.
  • since you are assigning an @ variable, and largely calling from one as well, your code uses no encapsulation of memory. Thus, you can move both if and elsif block actions into each their own load_search_users_by_role and load_search_users_by_order methods.

Related Solutions

How to convert to google encoded polyline algorithm format.

The definitive reference to encoding and decoding polylines is by Professor Mark McClure, at http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/

It contains utilities, a discussion of the algorithm and ports of the Javascript code into Perl, Ruby, PHP, Java and Mathematica.

Note: for Version 3, you don't need the levels string which is needed for Version 2. Version 3 works out the levels for itself.

Related Topic

DEV Community

DEV Community

Truemark Technology profile image

Posted on Jun 26, 2020 • Updated on Aug 3, 2020 • Originally published at thedevpost.com

11 Most Asked Questions About RuboCop

RuboCop is a Ruby static code analyzer and code formatter which helps to track errors easily and fix minor code issues during the development process saving your time. It has many advantages and you can learn more about RuboCop on https://docs.rubocop.org/en/stable/ .

Today, we will be talking about the most asked questions about RuboCop.

1. How to check if record exists from controller in Rails

How to test if at least one record exists?

Option 1: Using .exists?

Option 2: Using .present? (or .blank? , the opposite of .present? )

Option 3: Variable assignment in the if statement

This option can be considered a code smell by some linters (RuboCop for example).

Option 3b: Variable assignment

You can also use .find_by_user_id(current_user.id) instead of .where(...).first

Best option:

  • If you don’t use the Business object(s): Option 1
  • If you need to use the Business object(s): Option 3

Alternative Answer:

In this case, you can use the exists? method provided by ActiveRecord:

2. How to ignore lines with comments?

There is a way to ignore cops on a per-line basis.

There is also a way to do it via the configuration file.

Run rubocop --auto-gen-config and it will generate a file that you can use to disable the offenses.

The command also gives a hint on what to do to load those options.

On a line per line basis, you can enable and disable the cops as well.

You can also do more than one rule at a time in your code.

By using an inline directive, the directive becomes valid only for that line, and it would look like this:

It’s possible to define regex patterns to automatically ignore certain lines in rubocop.yml , so you could choose to ignore all lines starting with a # character:

This could be improved so that “indented” comment lines (i.e. whitespace followed by a # character) is also ignored if that’s what you want.

Note that this doesn’t account for lines of code that end with a comment, though:

3. How to split Ruby regex over multiple lines?

You need to use the /x modifier, which enables free-spacing mode.

Like in this case:

Using %r with the x option is the preferred way to do this.

See this example from the GitHub ruby style guide

4. RuboCop: Line is too long ← How to Ignore?

You can disable a bunch of lines like this:

Or add this to your .rubocop.yml file to increase the max length:

Creating a .rubocop.yml file (keep an eye on the initial . in the filename) in the root of your project, you’ll have a bunch of options:

5. What is meant by ‘Assignment Branch Condition Size too high’ and how to fix it?

Assignment Branch Condition (ABC) size is a measurement of the size of a method. It is essentially determined by counting the number of A ssignments, B ranches, and C onditional statements.

To reduce the ABC score, you could move some of those assignments into before_action calls:

6. How to tell RuboCop to ignore a specific directory or file?

You can add the following to .rubocop.yml:

where the path is relative to .rubocop.yml

From rubocop/default.yml :

7. How to integrate RuboCop with Rake?

The simple answer is just adding this to your Rakefile:

As of version 0.10.0 RuboCop contains a custom rake task that you can use. Just put the following in your Rakefile

Make sure to use upper-case ‘R’ and ‘C’ or you will get a NameError.

8. How to silence RuboCop warning on Assignment Branch Condition?

This is the message for the Metrics/AbcSize cop.

# rubocop:disable Metrics/AbcSize

On your RuboCop config

9. How to disable frozen string literal comment checking?

Add the following to your .rubocop.yml :

10. How to pass &:key as an argument to map instead of a block with Ruby?

Pass &:key as an argument to map instead of a block.

11. How to fix "SublimeLinter-RuboCop not running even when enabled and RuboCop in the path"?

First, specify the right path for you ruby env in Packages/User/SublimeLinter.sublime-settings as this:

After that close sublime completely and reopen it.

In Conclusion

These are the most asked questions about the RuboCop. If you have any suggestions or any confusion, please comment below. If you need any help, we will be glad to help you.

We, at Truemark , provide services like web and mobile app development, digital marketing, and website development. So, if you need any help and want to work with us, please feel free to contact us.

Hope this article helped you.

Original Source: DevPostbyTruemark

Top comments (0)

pic

Templates let you quickly answer FAQs or store snippets for re-use.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .

Hide child comments as well

For further actions, you may consider blocking this person and/or reporting abuse

krlz profile image

Stay Connected: Networking, Conferences, and Engaging Experiences online!

krlz - Mar 29

devluc profile image

5 Websites for Free Nuxt Templates

Devluc - Mar 30

frncscgvdesarrollos profile image

ayuda con MercadoPago checkoutpro.

Francisco - Mar 30

vulcantechs profile image

Web3 and Blockchain: Peeling Back the Layers

Vulcantechs - Mar 30

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Search code, repositories, users, issues, pull requests...

Provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

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

Metric/AbcSize cop specific configurations not respected #6455

@Drenmi

jodeci commented Nov 7, 2018

@jonas054

jonas054 commented Nov 8, 2018

Sorry, something went wrong.

jodeci commented Nov 8, 2018

Jodeci commented nov 9, 2018.

@Drenmi

Drenmi commented Nov 9, 2018 • edited

@koic

koic commented Nov 9, 2018

@stale

stale bot commented May 8, 2019

@stale

stale bot commented Jun 7, 2019

No branches or pull requests

@koic

More than 3 years have passed since last update.

ruby assignment branch condition size for is too high

RuboCopでこんなエラーが出た。Assignment Branch Condition size for search is too high.

コミットをする前にこんなエラーが出た。 調べてみると、なげーよ、幅取りすぎだろみたいなエラーらしい。

RuboCopとは。簡単に

RuboCopのGithub https://github.com/rubocop-hq/rubocop/tree/v0.28.0

このRubCopはコードを検査してくれるものらしくて、コードが長いとか、インデントが変だとかいろんなことを教えてくれる。勝手に直してくれる(不自然なインデントを修正)ときもあれば、警告文だけだして、自分でどうにかしてくれみたいな時もある。ただ、このRuboCopからの指摘も全てが正しい訳ではないっぽい。

 今回のエラーに関して

最初に述べたように、なげーよ的な意味らしい。

実際に指摘を受けたコード

自分でも書いていて、長いなと思った。縦に長い。ただ、今の知識ではどうやってコードをより簡潔なものにすればいいか思いつかなかった。だれか教えてください。

それで結局どうしたか・・・

.rubocop.yml RuboCopの設定を変更した。 エラー文を見てみると、、、、

[<10, 21, 5> 23.79/20] この部分が、点数を表しているっぽい。これでみると、これだと『MAXスコアが20なのにお前のは23.79だよ』ってことらしく、これをどうにかするしかないと思った。

それで、.rubocop.yml内にある設定を変更した。

どう変更したかというと、、、

このMaxの部分を 25に設定を変更した。そしてコミットすると、RubCopからの指摘を受けずにすんだ。

設定変えられるのかー程度に、この記事では感じていただければ、幸いです。

Register as a new user and use Qiita more conveniently

  • You get articles that match your needs
  • You can efficiently read back useful information
  • You can use dark theme

IMAGES

  1. Decision Making in Ruby (if else)

    ruby assignment branch condition size for is too high

  2. Ruby Programming

    ruby assignment branch condition size for is too high

  3. Ruby Assignment Operator Example

    ruby assignment branch condition size for is too high

  4. Ruby Conditional Assignment. This code example shows the Array#each

    ruby assignment branch condition size for is too high

  5. Assignment Operators in Ruby

    ruby assignment branch condition size for is too high

  6. Introduction to How OOP Works in Ruby

    ruby assignment branch condition size for is too high

VIDEO

  1. Loadline Convention

  2. 5.5 Branch and Bound

COMMENTS

  1. ruby on rails

    Assignment Branch Condition (ABC) size is a measurement of the size of a method. It is essentially determined by counting the number of Assignments, Branches, and Conditional statements. (more detail..) To reduce ABC score, you could move some of those assignments into before_action calls:

  2. ruby on rails

    'Assignment Branch Condition Size too high' is a linting error, produced by a tool performing static code analysis. It indicates that there are too many assignments, branches and conditionals in one method or function.

  3. Understanding Assignment Branch Condition · Red Green Repeat

    Assignment Branch Condition size for [method] is too high The default is 15 and the first pass of my code is always way past this, probably on average twice this value. So cleaning up my code to meet the default value really takes a lot of work.

  4. Ruby

    Ruby-on-rails - meant by 'Assignment Branch Condition Size too high' and how to fix it Assignment Branch Condition (ABC) size is a measurement of the size of a method. It is essentially determined by counting the number of A ssignments, B ranches, and C onditional statements.

  5. ruby

    However, I've noticed that rubocop points out the following status: Assignment Branch Condition size for fetch_images is too high. Stack Exchange Network Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their ...

  6. Assignment Branch Condition size : r/ruby

    1 - move the require statements to the top of your file. It's clearer when they're there. Rubocop may be counting them as function calls as well. 2 - split out ARGV in a main method and pass in only the arguments you need. You should have a dedicated function parsing ARGV. It shouldn't be in the middle of a function like this.

  7. Rating the quality of a response based on the number of typos

    I have this method which returns the number, but Rubocop said: Assignment Branch Condition size for rating is too high. [15.84/15] def rating response = response_quality if response...

  8. ruby

    The problem is that Rubocop says that the Assignment Branch Condition size for the method initialize is too high (15.81 over a limit of 15). ... Conditional merge with Ruby to include a parameter if it hasn't been included. 2. Message class, with constructors for data from user and from database.

  9. 11 Most Asked Questions About RuboCop

    5. What is meant by 'Assignment Branch Condition Size too high' and how to fix it? Answer: Assignment Branch Condition (ABC) size is a measurement of the size of a method. It is essentially determined by counting the number of Assignments, Branches, and Conditional statements.

  10. Assignment Branch Condition size for hashes #1974

    Assignment Branch Condition size for hashes #1974. Closed firedev opened this issue Jun 17, 2015 · 7 comments Closed ... Rubocop complaints that assignment branch condition is too high, but there is no actual assignment (apart from hash keys) and certainly no branches or conditions.

  11. Being Your Own Rubocop

    Assignment Branch Condition size is too high. ... In fact, it only made matters worse! I spent hours toiling away at my ruby file in order to build this solution. And while the specific ...

  12. Class: RuboCop::Cop::Metrics::AbcSize

    This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions.

  13. ruby

    I have a method in my Ruby code that sets the default name for my model. Rubocop complains that Assignment Branch Condition Size is too high, 21.24/15. How can I improve this? def set_default_display_name return unless display_name.blank?

  14. Metric/AbcSize cop specific configurations not respected #6455

    Actual behavior Still getting warnings for test files. Metrics/AbcSize: Assignment Branch Condition size for test_offender... Expected behavior # .rubocop.yml Metrics/AbcSize: Exclude: - "test/**/*" Expected to disable warnings under tests. ... Assignment Branch Condition size for test_offender is too high. Disabling the cop itself does not ...

  15. ruby

    Assignment Branch Condition too high. I have a simple class that, on initialization, takes between one and eight parameters. It sets the accessors to these to use later. Rubocop is trying to arrest me for the ABC being too high, but I'm not sure if there's actually anything wrong with what I've done. Is this a case where I just disable the ...

  16. RuboCopでこんなエラーが出た。Assignment Branch Condition size for search is too high

    なにこのエラーAssignment Branch Condition size for search is too high. [<10, 21, 5> 23.79/20] コミットをする前にこん…

  17. ruby

    Rubocop complains that Assignment Branch Condition Size is too high: 20... Stack Exchange Network. Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, ...

  18. ruby

    I am confused here about what RuboCop is complaining about. "Assignment Branch Condition size for draw is too high. [29/15]" for the method below: class Ball attr_reader :color attr_reader :

  19. Class: RuboCop::Cop::Metrics::AbcSize

    The ABC size is based on assignments, branches (method calls), and conditions. ... '>` 30 dangerous. You can have repeated "attributes" calls count as a single "branch". For this purpose, attributes are any method with no argument; no attempt is meant to distinguish actual 'attr_reader` from other methods. ... ' Assignment Branch ...

  20. ruby

    After refactoring, we get a new issue that our method got too big. The complexity is still too high. Metrics/AbcSize: Assignment Branch Condition size for update is too high. [25.06/15] Metrics/MethodLength: Method has too many lines. [15/10] We are adviced to break up this method. It's doing a bit too much. So we'll put the next part in ...