literate programming

19
<<LITERATE PROGRAMMING>> Charles Anderson October 2016

Upload: charles-anderson

Post on 15-Apr-2017

83 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Literate Programming

<<LITERATE PROGRAMMING>>

Charles AndersonOctober 2016

Page 2: Literate Programming

MOTIVATION

Barney Stinson Motivational Quotes

Page 3: Literate Programming

MOTIVATION

• What does it mean to write a computer program?

• To produce a set of instructions for a computer

• Right?

Page 4: Literate Programming

MOTIVATION

• Who “reads” the program?

• The computer - duh

• Who really reads the program?

Page 5: Literate Programming

MOTIVATION

• Although we write programs to instruct computers, people are the ones who spend the most time reading programs.

• WORM - Write Once - Read Many

Page 6: Literate Programming

LITERATE PROGRAMMING

• “Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want the computer to do.” - Donald Knuth

Page 7: Literate Programming

LITERATE PROGRAMMING

• An approach to programming that enables the programmer to develop and present programs based on the logic on flow of his/her thoughts

• Rather than what the computer demands

• Writing an essay not a program

Page 8: Literate Programming

KEY FEATURE• Re-ordering the code

• Write in the order of human logic, not the computer

• Top-down, bottom-up, whatever

• Work at the right level of abstraction at the right time - for the human

• Focus on what’s important and defer other things (e.g., error handling)

Page 9: Literate Programming

ANTI-EXAMPLE

Signal-to-noise ratio problem?

public class HelloWorld { public static void main(String[] args) { System.out.println("hello world"); } }

Page 10: Literate Programming

JUST NOISE

$_="krJhruaesrltre c a cnP,ohet";$_.=$1,print$2while s/(..)(.)//;

• Being clever is rarely a good thing, especially for the next Schmoe that comes along next - e.g, Future You

Page 11: Literate Programming

LITERATE PROGRAMMING

• One source file with two languages:

• Programming language - e.g., Pascal

• Documentation language - e.g., TeX

• The order is based on documentation - author’s idea of how best to present the program

Page 12: Literate Programming

LITERATE PROGRAMMING

• Two tools to process the input file(s):

• tangle - extract and reorder program

• weave - extract documentation - see example

Page 13: Literate Programming

LITERATE PROGRAMS

• Basic format is:

• document a bit -

• code a bit -

We must include the standard I/O definitions, since we want to send formatted output to stdout and stderr.

<<Header files to include>>= #include <stdio.h> @

Page 14: Literate Programming

LITERATE PROGRAMS

• You can refer to a <<chunk of code>> any time you like.

• No need to declare it

• Add to it

• Refer to its value even before defined

Page 15: Literate Programming

LITERATE PROGRAMS

Here, then, is an overview of the file wc.c that is defined by the noweb program wc.nw: <<*>>= <<Header files to include>> <<Definitions>> <<Global variables>> <<Functions>> <<The main program>> @

Page 16: Literate Programming

ONE WORD

mindovermunch.com

Page 17: Literate Programming

DISCUSSION• Isn’t this just lots of comments? No

• Designed to be read and understood

• Not an afterthought - think before code

• We tried this with JavaDoc, and it failed. No

• “Comments lie” - Bob Martin. LP is still different

Page 18: Literate Programming

DISCUSSION• Some of LP was driven by short-comings of Pascal

• OO improves things, but LP can still help

• AWS CloudFormation JSON

• LP is great for something you come back to

• If you inherit a legacy ball of mud

Page 19: Literate Programming

CONCLUSION

• Literate Programming is an interesting, powerful idea that never quite caught on

• LPers not as bad as Forth programmers

• You might still want to check it out - e.g.,http://www.literateprogramming.com/