How do I fix the unused variable's error which I used?

In my controller, I improved and prevent dirty code in my project, for example, I was trying to put my params in variable like this:

Original codes :

def create_cms_post(conn, %{ 
      "title" => title, 
      "status" => status, 
      "post_type" => post_type, 
      "download_ext_link" => download_ext_link, 
      "price" => price, 
      "pic_x1_link" => pic_x1_link, 
      "pic_x2_link" => pic_x2_link, 
      "pic_x3_link" => pic_x3_link, 
      "group_acl" => group_acl, 
      "description" => description, 
      "changelog" => changelog, 
      "changelog_category" => changelog_category, 
      "plugin" => plugin, 
      "plugin_category" => plugin_category, 
      ....
      ....
      ....
    }) do

      create_cms_posts = case PostQuery.insert_post(title, status, post_type, **.... and more ...**) do

when I have done it my code was clear.

Edited codes :

def create_cms_post(conn, %{ 
      "title" => title, 
      "status" => status, 
      "post_type" => post_type, 
      "download_ext_link" => download_ext_link, 
      "price" => price, 
      "pic_x1_link" => pic_x1_link, 
      "pic_x2_link" => pic_x2_link, 
      ....
      ....
      ....
    } = allreq) do
      create_cms_posts = case PostQuery.insert_post(allreq) do

I have put params in one variable and I was putting in my function which I needed.

Now my codes works for me without any problems. but I have many the unused errors when I’m compiling my project.

  1. first question, does the errors have a bad efect on me ?
  2. and second , how can I fix this not to show me?

Thanks.

They are warnings, if they were errors they had the bad effect of failing the compilation.

Unused variable warnings are emitted during compiletime and should be a hint to you as the developer, that you might have forgotten to actually process a value that you have bound to a name. Thats the main reason for the warning.

It does not affect runtime behaviour off your application (well, maybe stack size is increased because of that, but I am not 100% sure on this as we have a lot of optimizer passes in between).

  1. Use the variables you bound
  2. Prefix the variables with an understcore (eg. _example) while binding
  3. Do not bind at all.
2 Likes