Enhance your Elixir Coding with

10 Killer Elixir Tips #7

Never write the code which is not worthy

Blackode
Blackode
Feb 19, 2018 · 8 min read
Photo by Form on Unsplash

Hey guys !
Welcome back to another set of pro elixir tips.
Read incase if you missed Killer Elixir Tips#1 #2 #3 #4 #5 #6

I am at the moment of writing this article

10-> Pretty Printing quoted Expression

Macro.to_string |> IO.puts

In general, when you pass the quote expression to , it returns the actual code in a string format and we all knew this.

The weird thing is, it gives the output in a single line along with \n characters inside the string. Check it down

To print the new lines, you pipe the string output from to like in the following code snippet. It gives the clean output by printing in new line.

Let’s check this in action

9-> Finding the loaded files in iex and dynamic loading files on-fly

  1. Finding the loaded files

Code.loaded_files

In elixir, we are having a definition in the module used to find the loaded files.

Let’s see this.

Run the command inside your terminal and call the function .

It gives an empty list as output because we haven’t loaded any files so far.

File Loading

Let’s create a file with a single function definition which just prints a string “Welcome to the world” for the demo purpose.

Save the file after editing.

Now, make sure you are in the same directory of where the file exists and run the command

It opens the Elixir interactive shell by loading the file as well.

Now, you can see the loaded file by calling . It outputs the path to the file.

That is one way of loading files. You can also load them on-fly in

Dynamic loading on-fly

Code.load_file/1

Unlike loading files with , you can load them on-fly in session with the help of .

Let us assume that we have a file in our current directory and open session from the same directory.

Let’s see this in action.

8-> Providing deprecation reason with

@deprecated

You might be noticed the warnings of using deprecated functions in a library along with some useful hint text. Today, we build this from scratch.

Suppose, you have written a library and want to update the name of one function in your next build release and if the user tried to access the old function then you have to warn him of using deprecated function instead of updated one.

To see this in action, you need to create new mix project.

Let’s do that.

Next change the directory to the project created.

Now, edit the file in the project with the following code

File Scrennshot Vim Editor

This file comprises of two modules and . The module comprises of two functions and . Here purposely, is considered as a deprecated function used inside the module.

The mix compiler automatically looks for calls to deprecated modules and emit warnings during compilation.

So, when you compile the project , it gives a warning saying

“print/0 is deprecated use show/0”

like in the following screenshot.

Deprecated Function Warning

Check the live coding

7-> Module Creation with create function

Module.create/3

As we all know is used to create a module. In similar, is also used to create a module.

The only difference in defining a module with is the definition inside the module is quoted expression.

However, we can use the help of to create a quoted expression.

We have to pass three parameters to .

The first one is name of the module. The second one is module definition in quoted expression. The third one is location. The location is given in like or else you can just take the help of which returns the same.

location of loc

The above line of code gives the context of the module definition and collected in . We can use this to pass as second parameter to function.

module definition context

Let’s put all together and see the magic

Creating another module with same module function

Full Code Execution Screenshot

Check out the live execution

6-> Finding the list of bound values in iex

binding

This is used in debugging purpose inside the function.

It is also very common to use with , which returns all variable names and their values:

When is invoked with , it prints:

You can also give context to the

If the given context is nil (by default it is), the binding for the current
context is returned.

Check out the live execution

5-> Fun with Char_Lists

We can convert any integer to charlist using . It can be used in two ways either passing the base the base value or not passing.

Let’s try with base and with out base.

without base

with base

So , try your own names as well and find your value with base

4-> Extracting Process Information

With the help of , we can extract the process information like linked processes etc…

It is used in two different ways.

Note: We get the information if and only if process is alive

Here, we try to create a process and linking it to the current process using and will try to extract only linked processes. With no surprise, it should give the output pid which is the current process in our case.

Extracting whole information

3-> Forcing Keys in a Map parameter in functions

Consider that you need to pass a map to a function and you have to ensure certain keys in the map then allow it to use the function body.

You can achieve this using structs with attribute. But, you can still use the pattern matching to the keys of a map.

Here, we don’t care how many keys present inside the map but we need atleast two keys , to be present inside the map with any values.

Execution screenshot

If you observer the screenshot, we tried to access the function by sending a map with single key parameter where it is not allowed then with two keys map and the keys are exactly pattern matched where it is allowed to use the function then evenutally tried with more keys still it worked.

Thanks to pattern matching.

2-> Universal Code base formatter

Code formatting is easy now with the help of mix task . It will take care of all you care about cleaning and refactoring as well. It can assure a clean code base or simply universal code base which maintains some useful standards. This really saves a lot of time.

To know more about the codebase formatting, check out my recent article on

Code Formatter The Big Feature in Elixir 1.6

which explains all about using task and its configuration in detail with screen shots of vivid examples.

1-> Alias multiple modules in one line

is same as following

In similar fashion, you can alias your current module like

In general, with out using , we have to type full module name like to define the struct but now simply will be the better approach.

if worth_clapping, do: “clap”, else: nil

Happy Coding !!

Thanks Giving
Hey guys thanks for the support you have shown in reading and clapping the previous parts of this category.

to be continued … in 10 Killer Elixir Tips Part 8

Thanks 4 UR Tm.. Read all parts of Killer Elixir Tips and download file at .
Click on link All Killer Elixir Parts. Contribute with a ★

blackode

Coding, thoughts, and ideas.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store