dwahyudi

dwahyudi

In Ruby community, we have something like Rubocop to review and give feedback to us, developers, regarding code style. There are certain limits in which a method must contain less than the specified amount of lines.

You got 40 lines of codes in a method? It’s time to extract some of them to a new place. Even madam Sandi Metz has a rule that won’t let you have a method with 5 lines or longer, something like that. This way we can have automated pull request review, so when a PR violates the rubocop’s rule, such PR cannot be merged to master. And sometimes I can tweak the limit number as well.

Now, I’ve seen my coworker’s Elixir code which have a function with 112 lines of codes in it. :anguished_face:

Somehow I want to enforce the limit, but I have no idea, how long it should be and which tool I can use.

Any suggestion?

Showing Posts 1 to 10

kokolegorille

kokolegorille

Maybe credo for code styling? Although I am not sure if it checks function length.

cmkarlsson

cmkarlsson

I think the only thing that works reliably is manual code review. Tools that check function length tend to become an obstacle to work around without actually improving the code.

I agree that generally the number of lines should be short but there are use cases where it makes more sense to have longer functions than splitting it up into multiple pieces. In my experience a developer struggling with writing functional code generally will not make the right decisions to make a function shorter and need help to get into the right way of thinking.

Qqwy

Qqwy

TypeCheck Core Team

Credo is a great tool, which performs many of the functionality you’d use Rubocop for in Ruby-land.
The nice thing about it, is that there are many style-related issues it hints at by looking at what you (and other contributors to the project!) do: If everywhere you use two spaces for indentation but here you use four, then it will warn you about it. But if you use four everywhere, and use two spaces at one place, then you’ll be warned about that line instead.

As for checking for function size: Credo checks at least for cyclomatic complexity (source), which is a matric that is slightly more sophisticated than lines-of-code for a function.

dwahyudi

dwahyudi OP

At least you may skip a rule with rubocop, if you want to.

But still, in my preference, limiting a subroutine with small amount of code is better. Small function with single purpose, small classes/modules, etc, make them easier to read, refactor, document and test.

dwahyudi

dwahyudi OP

Cyclomatic complexity is different with function’s length.

Still, my question is not yet answered.

My personal preference is around 20.

idi527

idi527

This big:

___________________________
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-______________________________
NobbZ

NobbZ

Even though we do not do elixir in my company, there have been discussions about setting our linters to fixed body-lengths.

As we are using go and python as primary languages as well as puppet, docker and bash as secondary languages, we decided we can not agree on a limit that suites all of those languages, therefore we decided to not put a limit at all. But we do not merge without review. So if the reviewer is in doubt about the length, we need to discuss. But usually we refactor as soon as a single person is in doubt.

After about a year, we consider this superior over fixed linting rules, and after we learned to know each other better, we ran less and less in situations that did not pass review (because of function length).

A good starting point as a rule of thumb we discovered was: “Would it fit on Xs screen?” (X is actually me, when working from the park or coffee shop with my 14" laptop).

fuelen

fuelen

I think there is no sense to set a strict limit to the function size in immutable languages.

david_ex

david_ex

This book came out recently:

https://smile.amazon.com/Philosophy-Software-Design-John-Ousterhout/dp/1732102201

The author discusses how software design could be improved by increasing focus on how design decisions impact the system’s overall complexity.

You can watch a 1 hour talk the author gave about some of his ideas here. One thing he discusses is the idea that classes should be “deep” (see talk for definition), and in that context he touches on the “n lines rule” at 17:50.

Discussion of the book, and number of lines vs design is sprinkled throughout episode 20 of the Elixir Outlaws podcast, but is mentioned more specifically at 21:40.

To be clear: I’m just adding relevant sources to the discussion (I hope!), not taking sides :wink:

dwahyudi

dwahyudi OP

These answers really cotradict with what I’ve learned from Dave Thomas, Uncle Bob, Martin Fowler, etc.

This world is weird :rofl:

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews