008: Pacman Rules in Elixir
Reviewing old code
Welcome to Snippets of Code, a programming language live journal. The current focus is on the Elixir programming language. Feel free to leave comments or suggestions!
Pacman Rules in Elixir on Exercism
defmodule Rules do def eat_ghost?(power_pellet_active, touching_ghost) do power_pellet_active && touching_ghost end def score?(touching_power_pellet, touching_dot) do touching_power_pellet || touching_dot end def lose?(power_pellet_active, touching_ghost) do !power_pellet_active && touching_ghost end def win?(has_eaten_all_dots, power_pellet_active, touching_ghost) do has_eaten_all_dots && !lose?(power_pellet_active, touching_ghost) end end
eat_ghost?: takes two parameters, power_pellet_active and touching_ghost, and returns a boolean value indicating whether the player can eat a ghost or not based on whether the power pellet is active or the player is touching a ghost.
score?: takes two parameters, touching_power_pellet and touching_dot, and returns a boolean value indicating whether the player can score points based on whether the player is touching a power pellet or a dot.
lose?: takes two parameters, power_pellet_active and touching_ghost, and returns a boolean value indicating whether the player has failed or not based on whether the power pellet is not active and the player is touching a ghost.
win?: takes three parameters, has_eaten_all_dots, power_pellet_active, and touching_ghost, and returns a boolean value indicating whether the player has won or not based on whether the player has eaten all dots and has not lost.
The function names are self-explanatory and follow a consistent pattern of ending with a question mark to state that they return a boolean value. The function parameters are also named, making the code more readable. Yet, a few improvements can be made to enhance the code's readability and maintainability.
Include comments that describe each function's purpose and how it is used. The function names could be more descriptive. For example, score? could be renamed to can_score? or scoring_possible? to make its purpose more clear.
Learning from the Community
ElixirConf 2022 - Yiming Chen - Porting Legacy Backend Services into Elixir, Seamlessly
Yiming Chen's talk at ElixirConf 2022 focused on the challenges of migrating legacy backend services into Elixir, particularly when avoiding user-facing disruptions. Chen emphasizes the value of an experimental approach when migrating, focusing on safety and short feedback loops. He discusses a reverse proxy approach and how to put cloud features into Elixir in a way that highlights two-way decision-making. Chen also shares the lessons learned from migrating a complex content management pipeline from an express backend to Elixir, emphasizing the importance of scalability and maintainability.
ElixirConf 2022 - Kimberly Johnson - Self-Taught to First Job!
In her speech, the speaker shares her journey as a self-taught engineer and offers advice for aspiring tech professionals, such as having grit, finding mentors, and utilizing learning resources. She discusses her interests, family, and learning experiences with various courses, mentors, and podcasts. She finally discusses her job interviewing experience and the importance of asking questions and seeking help.
ElixirConf 2022 - Jason Axelson - Quick Iteration in Elixir - Tips from 6 Yrs of Elixir Development
In this talk, Jason Axelson shares tips on quick iteration in Elixir learned over his 6 years of experience working with Elixir. He emphasizes caring about one's craft, investing time in tools, and having shorter feedback cycles. Some tips include getting familiar with IEX, configuring IEX to print lists instead of char lists, interrupting infinite loops, and auto-closing brackets. He also suggests aliasing functions to avoid typing long namespace names and using IEX configuration files to save shell history.