Send Close Add comments: (status displays here)
Got it!  This site "www.robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Some ways to think about programming a computer without thinking like a computer
by RS  admin@robinsnyder.com : 1024 x 640


1. Some ways to think about programming a computer without thinking like a computer
Thinking. Please waitSome ways to think about programming a computer without thinking like a computer

There were too many ways to include all of them.

2. Code and data
Bad programmers worry about the code. Good programmers worry about data structures and their relationships. Linus Torvalds (Finnish-American software engineer who created what is called Linux)

Do not think in Java, Python, etc. Think about data, data flow and the problem being solved.

Information sign More: Linus Torvalds

3. ASCUE publications
ASCUE logo


Information sign More: ASCUE publications

4. Teaching
[chairs, computer science vs. business]

Information sign More: Teaching

5. Translation
Google translate Greek to Russian translation
Automatic translation still has certain issues.

6. Definitions
Input-process-output CalculatorThe purpose of a computer is to perform computations. A computer is a calculator that can push it's own buttons.

A programming language is a notation for describing a computation. Can people think like a computer? Can computers "think"?

7. Thinking
The question of whether computers can think is like the question of whether submarines can swim. Edsger Dijkstra (computer scientist)

Information sign More: Edsger Dijkstra

8. Abstract
There are many ways to think about programming a computer. Unfortunately, the beginning programming course tends to overemphasize operational semantics such as thinking like a computer, thinking in a particular language, etc., with an over-emphasis on traditional mathematical expertise. No matter how smart the person, there is a limit to what a human can do when programming is based primarily on operational semantics. Other ways of thinking about programming and the programming process will be presented that are not based on thinking like a computer and which allow one to more easily create larger and more complicated software systems. Ways to incorporate these ideas into the beginning programming course will be covered (as done by the author). The (now retired) author, with a PhD in computer science in the area of applied programming language theory, has spent many years teaching in academia, doing software research and development in industry, and, in the process, writing about a thousand pages of code each year for useful programs, small and large, in many different programming languages.

9. Thinking
Think outside the boxOne wants to abstract to think about the problem being solved and not about the machine details of how it is being done. To some, computer science is the same as coding.

Is computer science about computers?

10. Astronomy and telescopes
Computer science is no more about computers than astronomy is about telescopes. Edsger Dijkstra (computer scientist)

A similar comparison could be made about music being about musical instruments.

Information sign More: Edsger Dijkstra

11. Big picture
Can you provide a reasonable definition of each of the following? Can you provide a reasonable objective function (goal) for each of the above? [perceptions]

Does the definition have a connection to the objective function.

12. Computer science
Levels of truth
Computer science is the search for finite representations of (potentially) infinite objects. Computer science is mostly at the logic level of truth involving symbol manipulation.

Software engineering (and data science and business) is mostly at the reality level of truth.

13. Finite representations of infinite objects

 ▶ 
 + 
 - 
 1. How many dots are red? 
 2. Ratio of integers as counts 
 3. Infinite rational approximation 
 4. Finite rational approximation 
 5. Finite representation 

Computer/Information science can be defined as the search for finite representations of (potentially) infinite objects.

Is this possible? Does it make sense?

Information sign More: Finite representations of infinite objects

14. Simplified definition
Simplified definition: More simplified definition: Can you bag groceries?

Grocery bags
This is a description as a constraint logic problem.

15. Software engineering
Fast cheap goodThe goal of software engineering is to develop software on-time, on-budget and that meets the needs of the customer. You can only get, at most, two of the three. You may get less.

The more math or computer science oriented a person is, the more difficult it is to make software engineering and business trade-offs. [CS SE vs. IS SAD]

16. Amazon
Amazon The Amazon slogan about "pick two" between "low prices" and "fast delivery", reminds one of the software engineering definition and trade-offs. Who at Amazon came up with this idea?

17. Business
Book: Reengineering the corporationWhat is the purpose of business?

The goal of business is to provide value to the customer - whoever those customers are. The customer is willing to contribute for that value.

Applies to business for profit and to non-profit, political, educational organizations, etc.

BPR = Business Process Reengineering defined business, for the first time, in the 1990's. "Do more with less".

18. BPR
Interestingly, Michael Hammer had a Ph.D. in computer science and applied OOP (Object Oriented Programming) ideas to business processes. James Champy helped fill in the human-management perspective.

19. Education
What is a reasonable definition of the goal of the educational system? Assume college level.

It is a business. The goal of business is to provide value to the customer.

Is there only one customer - the student? For those who get confused by the word "customer" the word "stakeholder" is used.

[customer segmentation, supply and demand, quality]

20. Theory and practice
In computer science, software engineering and business, one has to often deal with trade-offs between theory and practice.

21. Business-technical trade-offs
An important trade-off between theory and practice in business and software engineering is called a business-technical trade-off. This must be recognized and a trade-off determined. This needs to be presented to a manager for an informed decision.

22. Issues
Two types of issues arise in business-technical trade-offs.

23. Vocational training
I fear - as far as I can tell - that most undergraduate degrees in computer science these days are basically Java vocational training. Alan Kay (American computer scientist)

That is, many computer science programs are coding camps but often without teachers who really understand coding and sometimes without teachers who understand computer science.

Information sign More: Alan Kay

24. Program semantics
In thinking about problem solving and programming, semantics and meaning are important.

Semantics and meaning: The Church-Turing hypothesis/thesis is that these ways of thinking can compute the same functions.

Each way may not be as beneficial as the way for a human to think about computing.

25. Assignment statement and swaps

26. Assignment statement and swaps

 ▶ 
 + 
 - 
 1. Assignment statement 
 2. Evaluate expression to a value 
 3. Replace variable with value 

The assignment statement, with the destructive update as a side-effect of execution, causes many problems in programming.

Information sign More: Assignment statement and swaps

27. Assignment statement

v = e;

Command execution of an assignment statement is a two step process. Whatever value was in variable v before is lost (forever).

28. Step 1
Evaluate expression to a value

29. Step 2
Replace variable with value Whatever value was in variable v before is lost (forever).

30. Swapping values
Will the following code fragment correctly swap the values that are in integer variables x and y? Why or why not?
x = y; y = x;


31. Explanation
Did you think like a machine to reason about your answer?
x = y; y = x;

Thinking like a machine is thinking operationally.

How would you explain your reasoning to a student learning programming?

32. Testing
TestHow could you test your program to make sure it is correct?
x = y; y = x;

Would a flow chart help?

It is only two assignment statements. How hard could that be?

33. Testing
Programming fallacyFallacy: One can test a program to insure it is correct.
Let us adapt Dijkstra's argument from 1972 to this problem. For two 64 bit integers, there are 2128 possible ways to do the swap code.

There are only about 280 microseconds in 16 billion years.

So it would take about 250,000,000,000,000(about 250 trillion) computers about 16 billion years to go through all the ways of swapping two 64 bit values.

Testing (brute-force) only helps find errors! It does not show that program code is correct.

34. Computer bugs
As I have now said many times and written in many places: program testing can be quite effective for showing the presence of bugs, but is hopelessly inadequate for showing their absence. Edsger Dijkstra (computer scientist)

Dijkstra, E. (1976). A discipline of programming. Englewood Cliffs, NJ: Prentice-Hall., 20.

Information sign More: Edsger Dijkstra

35. Quantum computing in brief
Bit and qubitQuantum computing:

Dunbar number [exponential speedup not clearly defined, like entanglement]qc-11

36. Quantum computing analogies
Bit and qubitQuantum computing analogies: pick the best way

37. Correctness
Let us show/use program verification techniques to determine correctness.

The precondition and postcondition are as follows.
// { pre: (x = a) and (y = b) } ... code to swap values ... // { post: (x = b) and (y = a) }


38. Apply the rules
Apply the axiomatic semantics proof rule by working backwards in a top-down backward-chaining manner.
// { pre: 5: (x = a) and (y = b) } // Meet in the middle (are there contradictions at this point?) //{ 4: y = b = a = x } // { 3: (x = a) and (y = b) and (y = a) } // { 2: (y = b) and (y = a) } x = y; // { 1: (x = b) and (x = a) } y = x; // { post: 0: (x = b) and (y = a) }

Algebra is critical to proving program fragments correct. Is knowing algebra important to learning programming?

39. Work backwards
DONEMany students have trouble working backwards, using algebra, and realizing when the proof is done.
Stepwise refinement - numberedStair analogy:

40. When is it done?
Penrose steps 6Many students tend to be very used to thinking like a machine and not algebraically using symbol manipulation.

They may spin their wheels confused. It is like climbing the Penrose steps.

DONEAside: When is the program (or software) done? Think laser eye surgery.

41. Penrose steps: how it is done

 ▶ 
 + 
 - 
 1. Penrose steps 1 
 2. Penrose steps 2 
 3. Penrose steps 3 
 4. Penrose steps 4 
 5. Penrose steps 5 
 6. Penrose steps 6 

The Penrose never-ending steps illusion, also called the stairs illusion, cannot exist in reality as depicted.

How is the Penrose steps illusion created?

Information sign More: Penrose steps: how it is done
Interpretation: The statements
x = y; y = x;

will correctly swap the values in x and y if and only if x and y have the same initial values.

This result follows without thinking operationally (like a machine). We only have to apply the rules and interpret the results.

42. Algebra
From the axiomatic rules used to prove program code correct, one should see that algebra is an integral part of that process.

Thus, the ability to algebra, as in substitute and simplify, is an important and implicit part of the programming process.

43. Programming
Much of programming reduces to learning code invariant transformation rules and abstraction rules and applying them in a iterative refinement process - often without thinking about what they mean.

That is the purpose of algebra. That is, to transform mathematical expressions without thinking about what they mean (in an operational sense).

44. Miller's Law
Remember Miller's Law that one can only keep about up to 7 things in memory at once.

Programmers who rigidly enforce this force you to go through many screens, each of which has only a few choices. One can quickly lose track of where things are in the interface.

Information sign More: George Armitage Miller

45. Viewpoint
Viewpoints:

46. Changing code
Cat in a boxMoving code around using invariant transformations is to a programmer who has memorized that code and thinks like a computer, like moving things around in a house where a cat lives. The cat's memory map of where everything is located needs to get relearned.

47. Problem types
1. Think like a computer:
Here is some code using loop, if, etc. What is the output?

2. Thinking like a computer not required:
Here are four code fragments. Which one can compute a different output?


48. Problem comparison
Think like a computer:
Yes: What is the output of each code fragment?
No: Which code fragment can compute a different output?
Assume all variables are integers and declared (omitted).
i = 0; while (i != 8) { i += 1; print(i); }

h = 0; while (h != 8) { print(h); h += 1; }

k = 1; while (k != 8) { print(k); k += 1; }

The for loop has very subtle semantics that vary from language to language. Best to avoid except for 1 to n or 0 to n-1. Python does not have a for loop.

49. Assignment statements
What do the following assignment statements do? That is, what is their effect.
x = y - x; y = y - x; x = x + y;

Explain how you arrived at your conclusion.

The exclusive or operation can be used for the same effect.

Information sign More: The XOR operation and one-time pads

50. Swapping values
Parallel assignment: (syntax varies)
x, y = y, x


51. Procedures
A procedure can be used to swap values.

This handles the noncomputer-checked redundancy.

Reference parameters are needed. Some languages do not have reference parameters.

Information sign More: Procedure to swap values

52. Procedure using reference parameters
Here is the C code [#1]


53. Output
Here is the output of the C code.


54. Macro expansion
A procedure can be considered a textual macro that is expanded in-line with the parameters.

Macro expansion can be used such as is found in C, C++, Julia, etc.

Redundancy is generated but is handled by the computer.

55. Macro expansion
Here is the C code [#2]

Note: Due to the replacement of swap(x1,y2) with a block (delimited by curly braces), a terminating semicolon can be used or not used.

56. Output
Here is the output of the C code.


57. Text formatter
A text formatter approach uses macros to expand text in-line. Somewhat like AOP = Aspect Oriented Computing
@ MACRO(swap(a,b)=[a, b = b, a;])

Another way:
@ MACRO(swap(a,b,t)=[t = a; a = b; b = t;])

Combined way:
@ MACRO(swap(a,b,t)=[@ IFDEF(t,y [t = a; a = b; b = t;] ,n [a, b = b, a;])])


58. Code and data
Rounded squares interleavedCode and data are two views of the same phenomena. Von Neumann first noticed this in the 1950's though it is assumed by Turing in his limits of computation work involving the Halting Problem in the 1930's.

59. Redundancy
Groupthink - before Groupthink - afterRedundancy is repetition. This can be good or bad. Not to be overly repetitive and redundant: Have you tired of the global worldwide pandemic that is everywhere?

60. Noncomputer-checked redundancy
Noncomputer-checked redundancy is the primary problem in software systems.

Design goal: Every important aspect of a program should be changeable in one and only one place. To make a change, find that one place.

Design goal: In general, every block of code should have one entry point and one exit point.

As with all rules: unless you have a really good reason.

61. Data world
Rounded squares interleavedData redundancy issues: Solved with RDBMS = Relational Database Management System and SQL = Structured Query Language. Still present in No-SQL systems.

62. Code world
Rounded squares interleavedCode redundancy issues: Solved (in part) with OOP = Object Oriented Programming. Still present in non-OOP systems (Go, Julia, etc.)

Text (and code) world involves text formatting using macros, etc.

63. Programming
It is almost always better to move "code-driven programming" to "data-driven programming". This should be covered in the first programming course so students learn both ways and which is better. Better to transform the data to new data without changing the data in-place (assignment statement issues), the idea of XSLT.

64. Code and data
Bad programmers worry about the code. Good programmers worry about data structures and their relationships. Linus Torvalds (Finnish-American software engineer who created what is called Linux)

Do not think in Java, Python, etc. Think about data, data flow and the problem being solved.

Information sign More: Linus Torvalds

65. Program facing aspects
Two aspects of programs that follow from specifications are the following.

66. Customer facing output
Functional requirement: Customer-facing output is to be "Hello. How is you today.".

Nonfunctional requirement: Use the Go (or golang) programming language.

67. Code example
Here is a code example in Go.

If you change the output text in any way, you lose points.

The customer specifies the customer-facing requirements.

The company (teacher) will specify the programmer-facing requirements.

The only way to determine if the code has an error is to compare it to a specification.

68. Errors and changes
Obzerved and desired behaviorThe definition of an error and a desired change is very similar.
Both an error and a change are a difference between the observed behavior and the expected or desired behavior.

To identify an error requires a specification and one should not use the operational code (in programming, or reality in general) as a specification.

Information sign More: Errors and changes

69. Fallacy
Programming fallacyFallacy: The programmer is allowed to change the customer-facing specifications when they think it will make a better program.
The customer gets what they requested and approved. In real situations, one goes to the manager, the manager goes to the client, and a change to the specification is worked out.

70. Customer facing
In a beginning programming course, the customer-facing parts of the program are the input and output statements.

71. Grading
Statistics analogy:

72. Correctness semantics

73. Constants
... more to be added ...

74. Variables
... more to be added ...

75. Loop unrolling
... more to be added ...

76. Abstraction
Abstraction 1
To abstract is to take away from the essentials and thereby to ignore certain differences.
The similarity is what is the same. The difference is what is different.

Human brains are built for complex abstraction.

The Latin word "abstractus""take away from". In abstract art, something is taken away, something remains, one needs to then interpret what is meant or intended.

Information sign More: Abstraction

77. Abstraction and psychological chunking
Part of the process of abstraction is what Hofstadter calls "psychological chunking". Hofstadter, D. (1979). Gödel, Escher, Bach: An eternal golden braid. New York: Vintage Books., p. 285-287.

Hofstadter relates how Adriaan de Groot performed studies of expert and novice chess players in the 1940's.

Information sign More: Abstraction and psychological chunking

78. Procedural abstraction introduction

79. Procedural abstraction introduction
Let us look at a abstraction example for the first programming course using the children's song "Mary had a little lamb". Another song, such as "Old MacDonald had a farm" is used for labs and assignments based in the following example.

Information sign More: Procedural abstraction introduction
Let us look at procedure abstraction using a children's nursery rhyme as an example.

Note: Using a simple song has the advantage that the output is known and easily recognizable while one is programming the abstractions.

80. Music
Here are the lyrics to the song, "Mary had a little lamb" as music.

Mary had a little lamb
The image was created with a LilyPond script. (omitted)

81. Lyrics
Here are the lyrics to the song, "Mary had a little lamb" as text.
Mary had a little lamb, little lamb, little lamb Mary had a little lamb, it's fleece was white as snow. And everywhere that Mary went, Mary went, Mary went. Everywhere that Mary went, the lamb was sure to go.


82. Print statements
Obviously, we could output this text using print statements as follows.
printf("Mary had a little lamb,\n"); printf("little lamb, little lamb\n"); printf("Mary had a little lamb,\n"); printf("it's fleece was white as snow.\n"); printf("\n"); printf("And everywhere that Mary went,\n"); printf("Mary went, Mary went.\n"); printf("Everywhere that Mary went,\n"); printf("the lamb was sure to go.\n");

The student is shown how to use editor shortcuts to not type code. Just copy and paste such as printf(" ... "):.

83. Program
Here is a program to do just that.

The output is as expected.

Regression testing can be used to check the output with the specification.

84. Code development

 ▶ 
 + 
 - 
 1. Code dev 1 
 2. Code dev 2 
 3. Code dev 3 
 4. Code dev 4 
 5. Code dev 5 

In order to develop code, a code development process is needed.

Most students write lots of code and then try to get it working.

This does not work very well.

Information sign More: Code development

85. New requirements
Mary had a little lamb, little lamb, little lamb Mary had a little lamb, it's fleece was white as snow. And everywhere that Mary went, Mary went, Mary went. Everywhere that Mary went, the lamb was sure to go.

Johnny had a tiny pig, tiny pig, tiny pig Johnny had a tiny pig, it's hide was black as coal. And everywhere that Johnny went, Johnny went, Johnny went. Everywhere that Johnny went, the pig was sure to go.

Original and new requirements.

There may be new similar requirements in the future.
Susan had a hopping rabbit, hopping rabbit, hopping rabbit Susan had a hopping rabbit, it's fur was brown as earth. And everywhere that Susan went, Susan went, Susan went. Everywhere that Susan went, the rabbit was sure to go.


86. Color-coded differences
Mary had a little lamb , little lamb , little lamb Mary had a little lamb , it's fleece was white as snow . And everywhere that Mary went, Mary went, Mary went. Everywhere that Mary went, the lamb was sure to go.

Johnny had a tiny pig , tiny pig , tiny pig Johnny had a tiny pig , it's hide was black as coal . And everywhere that Johnny went, Johnny went, Johnny went. Everywhere that Johnny went, the pig was sure to go.

Colors help one see similarities and differences.
  • Similarities: procedure body
  • Differences: actual/formal parameters.
  • Susan had a hopping rabbit , hopping rabbit , hopping rabbit Susan had a hopping rabbit , it's fur was brown as earth . And everywhere that Susan went, Susan went, Susan went. Everywhere that Susan went, the rabbit was sure to go.


    87. Comparison
    A program such as WinMerge can help see the similarities and differences. Here is a comparison of Mary (#1) and Susan (#3). WinMerge: Mary and Susan compareOne still has to see differences between "little lamb" and "hopping rabbit" in the overall context.

    88. Abstractions
    Do you see that there are similarities and differences? If not, you may have a difficult time in computer science.

    Some appropriate abstractions are needed.

    A common example of such requirements are a web page system that must generate certain output or reports based on data in a database. The method is similar to the mail merge process in the document world.

    89. Mail merge
    A mail-merge process using, say, a template in Microsoft Word and a database of differences in an Excel worksheet is another example. In the mail merge process, the differences (fields from each database record) are merged into the similarities (document with fill-in fields) to create the collection of documents merged documents

    90. Color-coded differences
    Here is a color-coded table of differences.
      Go: Procedural abstraction introduction
      Name v. 1 v. 2 v. 3
      NAME Mary Johnny Susan
      SIZE little tiny hopping
      ANIMAL lamb pig rabbit
      FUR fleece hide fur
      COLOR white black brown
      OBJECT snow coal earth
    Susan had a hopping rabbit , hopping rabbit , hopping rabbit Susan had a hopping rabbit , it's fur was brown as earth . And everywhere that Susan went, Susan went, Susan went. Everywhere that Susan went, the rabbit was sure to go.


    91. Differences
    Here is a table of differences without color.
      Go: Procedural abstraction introduction
      Name v. 1 v. 2 v. 3
      NAME Mary Johnny Susan
      SIZE little tiny hopping
      ANIMAL lamb pig rabbit
      FUR fleece hide fur
      COLOR white black brown
      OBJECT snow coal earth
    Susan had a hopping rabbit, hopping rabbit, hopping rabbit Susan had a hopping rabbit, it's fur was brown as earth. And everywhere that Susan went, Susan went, Susan went. Everywhere that Susan went, the rabbit was sure to go.


    92. Color-coded input data
    Here are color-coded input data sets.
    Input set #1 Input set #2 Input set #3 Output #3
    Mary little lamb fleece white snow

    Johnny tiny pig hide black coal

    Susan hopping rabbit fur brown earth

    Susan had a hopping rabbit , hopping rabbit , hopping rabbit Susan had a hopping rabbit , it's fur was brown as earth . And everywhere that Susan went, Susan went, Susan went. Everywhere that Susan went, the rabbit was sure to go.


    93. Input data without color
    Here are input data sets without color.
    Input set #1 Input set #2 Input set #3 Output #3
    Mary little lamb fleece white snow

    Johnny tiny pig hide black coal

    Susan hopping rabbit fur brown earth

    Susan had a hopping rabbit, hopping rabbit, hopping rabbit Susan had a hopping rabbit, it's fur was brown as earth. And everywhere that Susan went, Susan went, Susan went. Everywhere that Susan went, the rabbit was sure to go.


    94. Actual input
    For physical input, some decisions need to be made. How is the above data recognized? Are there other data in the input, such as a debug output setting?

    95. Solutions
    Some ways of creating a program to take the input data and output the text to each of the songs are the following. Nonfunctional requirements: code style according to company policy such as using variables, arrays, records, etc.

    Each improvement is a "refactoring" of the code. It does the same thing, just differently.

    96. Observation
    Programming fallacyFallacy: If you are good at math, you will be good at computer science.
    1In the above development, and in the solutions, what is often thought of as mathematics - using addition, subtraction, multiplication, division, formulas, etc., is not needed.

    The math needed is abstraction and symbol manipulation and algebra as in lambda calculus.

    97. Textual abstraction
    The core of computer science and programming involves abstraction. Many students who are good at "math" have trouble with these programs. How many students who are not good at "math" can do this type of abstraction?

    Most students have problems with abstraction in that they want to think like a machine rather than doing textual pattern matching and algebraic code transformations.

    98. Engineering efficiency
    Although mathematics is very important to the computer scientist, there is much more than mathematics to computer science.
    Math and engineering
    Programming is as much a mathematical discipline as an engineering discipline; correctness is as much our concern as, say, efficiency. Dijkstra, E. (1976). A discipline of programming. Englewood Cliffs, NJ: Prentice-Hall., p. 54-55.

    99. Summary
    Some ways of programming without thinking like a machine have been presented. Many more were omitted.

    100. Questions, comments
    Questions and comments and more information. [circle back]

    101. End of page

    by RS  admin@robinsnyder.com : 1024 x 640