Python is pseudo code

18 Mar 2014

When I write code in my head, it’s always in Python. Because Python is basically pseudo code.

Pseudo code

class Bean {
    bool cool;
    iscool() return cool;
    becool(bool) set cool;
}

wecool(list<Bean> beans)
    for (Bean in beans)
        if !bean.iscool()
            print "Not cool."
            bean.becool(true);
    print "We cool."

beans = [Bean(true), Bean(false)]
wecool(beans)

Real code

Below is the pseudo code written in different languages, and the shell commands to actually run each program.

Java

Java is a bit overkill with its System.out.println

// CoolBeans.java

import java.util.ArrayList;

class Bean {

    private boolean mCool;

    public Bean(boolean cool) {
        mCool = cool;
    }

    public boolean isCool() {
        return mCool;
    }

    public void beCool(boolean cool) {
        mCool = cool;
    }

}

public class CoolBeans {

    public static void weCool(ArrayList<Bean> beans) {
        for (Bean bean : beans) {
            if (!bean.isCool()) {
                System.out.println("Not cool.");
                bean.beCool(true);
            }
        }
        System.out.println("We cool.");
    }

    public static void main(String[] args) {
        ArrayList<Bean> beans = new ArrayList<>();
        beans.add(new Bean(true));
        beans.add(new Bean(false));
        weCool(beans);
    }

}
$ javac CoolBeans.java
$ java CoolBeans

C++

C++ is definitely not pseudo code.

// coolbeans.cpp

#include <iostream>
#include <vector>

using namespace std;

class Bean
{
private:
    bool m_cool;

public:
    Bean(bool cool) :
        m_cool(cool)
    {
    }

    bool isCool() const
    {
        return m_cool;
    }

    void beCool(bool cool)
    {
        m_cool = cool;
    }
};

void we_cool(vector<Bean>& beans)
{
    for (auto& bean : beans) {
        if (!bean.isCool()) {
            cout << "Not cool." << endl;
            bean.beCool(true);
        }
    }
    cout << "We cool." << endl;
}

int main(int argc, char* argv[])
{
    vector<Bean> beans = { Bean(true), Bean(false) };
    we_cool(beans);
    return 0;
}
$ g++ coolbeans.cpp -o coolbeans
$ ./coolbeans

Go

Go is getting closer, but not quite there.

// coolbeans.go

package main

import (
	"fmt"
)

type Bean struct {
	cool bool
}

func (bean Bean) isCool() bool {
	return bean.cool
}

func (bean Bean) beCool(cool bool) {
	bean.cool = cool
}

func weCool(beans []Bean) {
	for _, bean := range beans {
		if !bean.isCool() {
			fmt.Printf("Not cool.\n")
			bean.beCool(true)
		}
	}
	fmt.Printf("We cool.\n")
}

func main() {
	beans := []Bean{ {true}, {false} }
	weCool(beans)
}
$ go run coolbeans.go

Ruby

I tried to find a scripting language I didn’t very well to compare with Python. I started with Perl, but that got ugly quick.

Ruby is a nice looking language, that I’m now interested to learn more about. The number of end statements would not show up on a whiteboard, but other than that it’s a pretty pseudo language.

# coolbeans.rb

class Bean
  def initialize(cool)
    @cool = cool
  end

  def is_cool
    return @cool
  end

  def be_cool=(cool)
    @cool = cool
  end
end

def we_cool(beans)
  for bean in beans
    if not bean.is_cool()
      puts "Not cool."
      bean.be_cool = true
    end
  end
  puts "We cool."
end

beans = [Bean.new(true), Bean.new(false)]
we_cool beans
$ ruby coolbeans.rb

Python

Finally, Python. Aside from some weirdness with __init__ (I think Ruby’s init looks better), and the use of self, this code looks pretty much like pseudo code.

# coolbeans.py

class Bean:
    def __init__(self, cool):
        self.cool = cool

    def is_cool(self):
        return self.cool

    def be_cool(self, cool):
        self.cool = cool


def we_cool(beans):
    for bean in beans:
        if not bean.is_cool():
            print 'Not cool.'
            bean.be_cool(True)
    print 'We cool.'


beans = [Bean(True), Bean(False)]
we_cool(beans)
$ python coolbeans.py

Python is really cool, and one of my favorite languages.