DEV Community

Discussion on: The Reverse Polish Notation evaluator using only call stack

Collapse
 
bgadrian profile image
Adrian B.G.

I have many suggestions, but is hard to do a code review here.
Starting with those interface{}, you can get rid of them and make some custom structs or check if is a digit or sign. By removing the interface{} you will also get rid of the reflection.

I would say to move the operations in a map like

type op func(a, b int) int

var ops = map[Rune]op {
'+': func(a, b int) int {
            return a + b
        },
'-': func(a, b int) int {
            return a - b
        }
'*': func(a, b int) int {
            return a * b
        }

This way you solve a few issues:

  • you simplify the code
  • I think it will be more verbose
  • when you want to add a new operator you don't change the code, just add a new entry in the map
  • that default was not good, you treat any input as a *, which is obviously wrong. If the input is not recognized it is ok to panic, you literally do not know what to do with it, it is invalid.

Ping me anytime on if you need extra Go reviews on Github or check the #reviews channel on the Gophers slack.