Skip to content

Instantly share code, notes, and snippets.

@scravy
Created December 11, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scravy/6d07397d5fa482f68df0723a535cff97 to your computer and use it in GitHub Desktop.
Save scravy/6d07397d5fa482f68df0723a535cff97 to your computer and use it in GitHub Desktop.
Sanity and Insanity in different programming languages

Java:

class Person {
    private String firstName;
    private String lastName;
      
    public String getDisplayName() {
        return firstName + " " + lastName;
    }
}

C#

class Person
{
    private string FirstName;
    private string LastName;
    
    public String GetDisplayName() {
        return FirstName + " " + LastName;
    }
}

Haskell

data Person = Person String String

displayName :: Person -> String
displayName (Person firstName lastName) = firstName ++ " " ++ lastName

Ruby

class Person 
    attr_reader :first_name, :last_name
    def initialize (first_name, last_name)
        @first_name = first_name  
        @last_name = last_name
    end
    def display_name
        return @first_name + " " + @last_name
    end
end

PHP

class Person {
    public $firstName;
    public $lastName;
    
    function getDisplayName() {
        return "$this->firstName $this->lastName";
    }
}

C++

class person {
    private:
        std::string first_name;
        std::string last_name;
    public:
        std::string get_display_name() {
            return first_name + " " + last_name;
        }
}

C++ (have to do that because of header files, as the C++ compiler can not just infer the interface from the c source file...)

person.h

class person {
    private:
        std::string first_name;
        std::string last_name;
    public:
        std::string get_display_name();
}

person.cpp

std::string person::get_display_name() {
    return first_name + " " + last_name;
}

C++ templates

template <typename T>
struct point {
    T x;
    T y;
    std::string to_string() {
        return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
    }
}

C++ templates (the less sane version, note that you could put the separate definition in a *.cpp file but now you need to instantiate every possible use of the class in there already...)

template <typename T>
class point {
    T x;
    T y;
    std::string to_string();
};

template <typename T>
std::string point<T>::to_string() {
    return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment