0% found this document useful (0 votes)
144 views20 pages

Readability PDF

The document discusses the concept of readability in programming. It provides examples comparing code snippets in different languages to illustrate how some languages are more readable than others. The key aspects that make code more readable include: using common language terms instead of symbols, minimizing unnecessary syntax, highlighting important concepts visually, and having an orthogonal design with consistent rules. Orthogonal languages like Tcl and VAX assembly use a small set of primitive operations that can be combined in straightforward ways, making the language simpler to learn and use.

Uploaded by

rishabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views20 pages

Readability PDF

The document discusses the concept of readability in programming. It provides examples comparing code snippets in different languages to illustrate how some languages are more readable than others. The key aspects that make code more readable include: using common language terms instead of symbols, minimizing unnecessary syntax, highlighting important concepts visually, and having an orthogonal design with consistent rules. Orthogonal languages like Tcl and VAX assembly use a small set of primitive operations that can be combined in straightforward ways, making the language simpler to learn and use.

Uploaded by

rishabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Readability

Language Evaluation Criteria:


• The art of programming is
the art of organizing
complexity, of mastering
multitude and avoiding its
bastard chaos as effectively
as possible. (Edsger W
Dijkstra)

Programming
• Any fool can write code that
a computer can understand.
Good programmers write
code that humans can
understand. (Kent Beck)

Programming vs Good
Programming
Readability measures how easy it is to, well, read a bit
of code and figure out what it is doing.

Ideally, a programmer can easily understand what


code does simply by reading it; but can a common
man?
Readability vs
Simplicity It’s tempting to make things as simple as possible, in
the hopes that that will make them more readable.

For example, in MATLAB, there’s usually no way to tell


whether fft(x,y) is an indexing expression, method, or
a function call until the moment that the line is
executed.
What makes a programming language readable is high
correlation between an idea’s visual intensity in code
and its marginal importance to the understanding of
what the code does.

Readable vs In highly-readable languages, key concepts are more


Important visually intense in the code, while less important ones
don’t even show.
Instructions
To be precise, Readability is not a property of
programming languages, but of programs written in
those languages. Although there are languages that
do not allow to create readable programs, you can
create unreadable programs in every language.
• In swift, a variable-sized array of strings is declared like this:

var shoppingList = ["Eggs", "Milk"]

• While in Java:

Example #1 - ArrayList<String> shoppingList = new ArrayList<>();


shoppingList.add("Eggs");
array of strings shoppingList.add("Milk");

• Referring to the Java code: the importance of the second


occurrence of “ArrayList” is minor, because the type has been
specified. And why does the type even needs to be specified?
the reader already knows that “Eggs” and “Milk” are strings.
So there’s a lot of unnecessary markup that doesn’t even help
with setting the initial value.
• A for-loop in Javascript looks like:
for (var i = 0; i < arr.length; i++ {
console.log(arr[i]);
}
• or
arr.forEach(function(value) {
Example #2 - console.log(value);
});
for loop
• And in Python:
for value in arr:
print(value)
• The Python code is more readable because it doesn’t need all
those extra tokens such as “var”, “.length”, “function”, curly
braces, and even the semicolon.
• In swift (and other languages) we can pass named parameters
so we don’t have to memorize the function declaration.

move(inches: 3, direction: .south)


Example #3 -
• In C, it would be something like:
named
parameters move(3, SOUTH);

• The C code isn’t as clear as the swift code on what unit might
be used (inches), while the visual intensity of “SOUTH” is very
high, for no good reason.
• In Perl:

$x =~ s/cat/dog/;

• In Python:
Example #4 -
string x = x.replace('cat', 'dog')

replacement • This is an example where something important is happening: a


string replacement.
• In perl, it’s almost hidden in the tiny tilde operator, while in
Python, we explicitly use the English word “replace”.
• Also, in Perl we have some characters that don’t add new
information such as the dollar sign, the semicolon, and the
slashes as an unnecessarily alternative way to terminate strings.
Short snippets of Python are readable because
they’re less verbose than Java. You don’t have to
repeatedly declare variable types.

Large Java systems are more readable than Python

Comparison because you don’t have to memorize the return


types or argument types of your methods: they’re
explicitly stated in the method signatures.

C# is almost exactly as readable and almost exactly


as verbose as Java, having started from a very
similar base, although the immediate syntactic
sugar of C#’s getter and setter declarations speaks
to a desire for brevity where appropriate.
• “The use of COBOL cripples the mind; its teaching should,
therefore, be regarded as a criminal offence.”
• “APL is a mistake, carried through to perfection.
• It is the language of the future for the programming
Dijkstra on •

techniques of the past: it creates a new generation
of coding bums.”
Language • “FORTRAN, 'the infantile disorder’ … is hopelessly inadequate
for whatever computer application you have in mind today:
Design it is now too clumsy, too risky, and too expensive to use.”
• “It is practically impossible to teach good programming to
students that have had a prior exposure to BASIC: as
potential programmers they are mentally mutilated beyond
hope of regeneration.”
Flon’s Axiom
“There is not now, nor has
there ever been, nor will there
ever be, any programming
language in which it is the
least bit difficult to write bad
code.” -
Lawrence Flon
s l ide 12
A language is orthogonal if its features
are built upon a small, mutually
independent set of primitive operations.

Fewer exceptional rules = conceptual


Orthogonality simplicity

• E.g., restricting types of arguments to a function

Tradeoffs with efficiency

slide
13
Orthogonality in a programming language means that a
relatively small set of primitive constructs can be combined
in a relatively small number of ways to build the control and
data structures of the language.

It is associated with simplicity; the more orthogonal the


design, the fewer exceptions.

Orthogonality
This makes it easier to learn, read and write programs in a
programming language.

The meaning of an orthogonal feature is independent of


context; the key parameters are symmetry and consistency
(for example, a pointer is an orthogonal concept).
Example : IBM
Mainframe and VAX
• An IBM mainframe has two different instructions for
adding the contents of a register to a memory cell (or
another register). These statements are shown below:

A Reg1, memory_cell
AR Reg1, Reg2

• In the first case, the contents of Reg1 are added to the


contents of a memory cell; the result is stored in Reg1.
• In the second case, the contents of Reg1 are added to
the contents of another register (Reg2) and the result
is stored in Reg1.
VAX
• In contrast to the above set of statements, VAX has only one statement for
addition:

ADDL operand1, operand2

• In this case the two operands (operand1 and operand2) can be registers,
memory cells, or a combination of both; the instruction adds the contents
of operand1 to the contents of operand2, storing the result in operand1.

• VAX’s instruction for addition is more orthogonal than the instructions


provided by IBM; hence, it is easier for the programmer to remember (and
use) the one provided by VAX.
The C language is somewhat inconsistent in its
treatment of concepts and language structure,
making it difficult for the user to learn (and
use) the language.

Is C Examples of exceptions follow:


Orthogonal?
• Structures (but not arrays) may be returned from a
function.
• An array can be returned if it is inside a structure.
• A member of a structure can be any data type (except
void, or the structure of the same type).
• An array element can be any data type (except void).
Everything is passed by value (except arrays).
Tcl has 12 rules that govern
the entire language.
[1] Commands.
[2] Evaluation.
[3] Words.
[4] Double quotes.
[5] Argument expansion.
[6] Braces.
[7] Command substitution.
[8] Variable substitution.
[9] Backslash substitution.
[10] Comments.
[11] Order of substitution.
[12] Substitution and word boundaries.
◼ Control statements
◼ The presence of well-known and reliable control structures

◼ Research of the 70s led to the desire for language constructs that made “goto-less”
programming possible.
◼ “A program that can be read from top to bottom is much easier to understand than a
program that requires the reader to jump from one statement to some nonadjacent
statement in order to follow the order of execution.” Sebesta .

◼ For example, what output is generated by this code segment?


inum1 = 1;

Control loop1:
if(inum1 > 10) goto end;
inum2 = 1;
Statements loop2:
if (inum2 > 10) goto next
print (inum1 + “ * “ + inum2 + “ = “ + inum1 * inum2);
inum2++;
goto loop2;
next:
inum1++;;
goto loop1;
}
end:
◼ Equivalent code in java for the Nested loop

inum1 = 1;
while (inum1 <= 10)
{

Control inum2 = 1;
while (inum2 <= 10)
{

Statements }
print (inum1 + “ * “ + inum2 + “ = “ + inum1 * inum2);
inum2++;

inum1++;
}

You might also like