Giter Site home page Giter Site logo

Comments (7)

rrrene avatar rrrene commented on May 7, 2024 2

@philipgiuliani @mgwidmann Sorry, I forgot to ping you. This is part of v0.4.0-beta1.

from credo.

rrrene avatar rrrene commented on May 7, 2024

I should really improve the explain text for that check. 😕

The reasoning is that pattern matching in a condition will make the code harder to reason about, especially when maintained by a different person than the original author. Assigning in a condition can also be mistaken for a comparison (e.g. when working under deadline pressure).

Example:

  if {:ok, %{"error" => true, "message" => message}} = parameter1 do
    do_error_handling
  end

In more complicated cases like this, the code might be more maintainable in the future if it reads like this:

  case parameter1 do
    {:ok, %{"error" => true, "message" => message}} -> do_error_handling
    _ -> nil
  end

I am guilty of writing code like this in the past, as I thought it to be clever and less verbose than the alternative. But the path to hell is paved with clever solutions and good intentions.

That said, nothing Credo suggests is set in stone. Maybe we should have a configuration option to allow simple assignments like the one in your example?

from credo.

mgwidmann avatar mgwidmann commented on May 7, 2024

I believe your example is no longer a conditional. Meaning, if the match fails it won't execute the code either way, thats certainly worth warning about.

However, in the most common use case of the match operator is actually a presence check.

if parameter = get_parameter_data() do
  do_something_with(parameter)
end

Which is just shorthand for

parameter = get_parameter_data()
if parameter do
  do_something_with(parameter)
end

In my experience, this is ok, as long as the match is entirely a wildcard (only a variable). I believe this error message should only trigger for "complex" matches, basically anything except a variable inside a conditional.

from credo.

mgwidmann avatar mgwidmann commented on May 7, 2024

Wanted to point out this is done in the Elixir codebase: https://github.com/elixir-lang/elixir/blob/574c6db3f8566a941b55c66499078d61661d08c6/lib/elixir/lib/kernel/cli.ex#L437-L448 and I'm sure in other places as well.

I would vote for a warning about any "complex" (or non-wildcard) type matches. Simply capturing the return value shouldn't be a problem unless it's the same name as another local variable/parameter (since that would then be bad if it changed to a ==)

from credo.

rrrene avatar rrrene commented on May 7, 2024

I heard you 😉 The behaviour will change like we discussed above.

Development is a bit slow at the moment, because there is a lot of stuff happening at my day job AND I am giving a talk about Credo at next week's ElixirConf.EU in Berlin and the preparation is taking up most of my free time. 😁

from credo.

philipgiuliani avatar philipgiuliani commented on May 7, 2024

Awesome! Just looked for the same thing. Its possible to use 0.4.0 already now?

PS: Your talk was amazing @rrrene ! That's why im using Credo now... :)

from credo.

rrrene avatar rrrene commented on May 7, 2024

I will release a beta this evening. Still working on the "create your own checks" UI, but most of the other stuff is done.

Will ping you here when it is released! 👍

from credo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.