Functional programming

Marko Rauhamaa marko at pacujo.net
Tue Mar 4 14:49:13 EST 2014


Antoon Pardon <antoon.pardon at rece.vub.ac.be>:

> In the same way writing unit tests is the most tedious, boring,
> annoying, *unproductive* part. Amost always you are giving the program
> results it can work out for itself.

Undoubtedly, explicit type declarations add a dimension of quality to
software. However, they also significantly reduce readability and tempt
you to dirty shortcuts (to avoid writing truckloads of boilerplate
code).

On the balance, I estimate the explicit style reduces code quality.

Example (found by a random Google search):

===JAVA BEGIN===========================================================

class WrappedSqlException extends RuntimeException {
    static final long serialVersionUID = 20130808044800000L;
    public WrappedSqlException(SQLException cause) { super(cause); }
    public SQLException getSqlException() { return (SQLException) getCause(); }
}

public ConnectionPool(int maxConnections, String url) throws SQLException {
    try {
        super(() -> {
            try {
                return DriverManager.getConnection(url);
            } catch ( SQLException ex ) {
                throw new WrappedSqlException(ex);
            }
        }, maxConnections);
    } catch (WrappedSqlException wse) {
        throw wse.getSqlException();
    }
}

===JAVA END=============================================================

===PYTHON BEGIN=========================================================

def __init__(self, max_connections, url):
    super().__init__(lambda: DriverManager.get_connection(url), max_connections)

===PYTHON END===========================================================

or, a bit less cryptically:

===PYTHON BEGIN=========================================================

def __init__(self, max_connections, url):
    def get_connection():
        return DriverManager.get_connection(url)

    super().__init__(get_connection, max_connections)

===PYTHON END===========================================================


Marko



More information about the Python-list mailing list