Fork me on GitHub
#code-reviews
<
2023-12-01
>
Cenny Davidsson20:12:59

Hello, I’ve been doing some Clojure and now feel that some feedback would really help me learn some more.

(ns exercise-6.core
  (:gen-class))
(require '[tick.core :as tick])

(defn year-format [date]
  (tick/format (tick/formatter "yyyy") date))

(defn parse-year [date]
  (Integer/parseInt (year-format date)))

(defn today []
  (tick/date (tick/instant)))

(defn ask [text]
  (println text)
  (flush)
  (read-string (read-line)))

(defn retirement-year [this-year years-left]
  (str 
    "You have " 
    years-left 
    " years left to retirement"
    "/n"
    "It's "
    this-year
    ", so you can retire in "
    (+ this-year years-left)))

(def retirement-age-reached "You can already retire")

(defn retire [retirement-age current-age this-year]
  (let [years-left (- retirement-age current-age)]
    (if (>= 0 years-left)
      retirement-age-reached 
      (retirement-year this-year years-left))))

(defn -main "Retirement" [& args]
  (let [current-age (ask "What is your current age?")
        retirement-age (ask "When would you like to retire?")
        this-year (parse-year (today))]
    (println (retire retirement-age current-age this-year))))

Cenny Davidsson20:12:08

For context, the problem i’ve solved:

Your computer knows what the current year is, which means you can incorporate that into your programs. You just have to figure out how your programming language can provide you with that information.

Create a program that determines how many years you have left until retirement and the year you can retire. It should prompt for your current age and the age you want to retire and display the output as shown in the example that follows.

**Example Output**
What is your current age? 25
At what age would you like to retire? 65
You have 40 years left until you can retire.
It's 2015, so you can retire in 2055.

**Constraints**
- Again, be sure to convert the input to numerical data before doing any math.
- Don't hard-code the current year into your program. Get it from the system time via your programming language.
        
**Challenge**
- Handlesituationswheretheprogramreturnsanegative number by stating that the user can already retire.

seancorfield21:12:19

I wouldn't reach for a time library for this. You can get the current year like this:

user=> (.getYear (java.time.LocalDate/now))
2023

seancorfield21:12:51

But otherwise it's all nice and clear. The require would normally be inside the ns:

(ns exercise-6.core
  (:require [tick.core :as tick])
  (:gen-class))

phronmophobic22:12:44

yea, looks pretty good. One very minor improvement might be to change (>= 0 years-left) to (neg? years-left) which tests if a number is negative.

seancorfield22:12:29

(>= 0 years-left) allows for zero? or neg?

seancorfield22:12:04

You could use pos? and swap the conditions.

phronmophobic22:12:27

Honestly, I would write it as (<= years-left 0), but that's just a personal preference and I don't see anything wrong with the way it's currently written.

phronmophobic22:12:22

Some other thoughts that are also pretty minor: • I would consider extract-year over parse-year since parse makes me think that the function expects a string • I might change the name of ask to ask! to just highlight that the function has I/O and is impure • I might change retirement-year to something like retirement-year-response or similar to show make it apparent that it's not returning a year (ie. number) • I might change retirement-age-reached to retirement-age-reached-response or similar to match • same with retire to retire-response • Doc strings could be helpful.

phronmophobic22:12:09

If you wanted to get fancy you could add some validation and error handling on the responses to ask.

Cenny Davidsson23:12:16

Wow, thanks so much for all that feedback! exactly what I needed, really good advice from all of you