Docsity

Comparing Programming Languages: Readability, Writability, and Reliability, Exercises of Programming Languages

Toggle sidebar
bg1
Introduction and Historical Background (Lecture 1-8) VU
Apprentice Hacker
===================
#!/usr/local/bin/perl
$msg="Hello, world.\n";
if ($#ARGV >= 0) {
while(defined($arg=shift(@ARGV))) {
$outfilename = $arg;
open(FILE, ">" . $outfilename) || die "Can't write $arg: $!\n";
print (FILE $msg);
close(FILE) || die "Can't close $arg: $!\n";
}
} else {
print ($msg);
}
1;
Experienced Hacker
===================
#include
#define S "Hello, World\n"
main(){exit(printf(S) == strlen(S) ? 0 : 1);}
Seasoned Hacker
===================
% cc -o a.out ~/src/misc/hw/hw.c
% a.out
Guru Hacker
===================
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Comparing Programming Languages: Readability, Writability, and Reliability and more Exercises Programming Languages in PDF only on Docsity!

Apprentice Hacker

#!/usr/local/bin/perl $msg="Hello, world.\n"; if ($#ARGV >= 0) { while(defined($arg=shift(@ARGV))) { $outfilename = $arg; open(FILE, ">". $outfilename) || die "Can't write $arg: $!\n"; print (FILE $msg); close(FILE) || die "Can't close $arg: $!\n"; } } else { print ($msg); } 1;

Experienced Hacker

#include #define S "Hello, World\n" main(){exit(printf(S) == strlen(S)? 0 : 1);}

Seasoned Hacker

% cc -o a.out ~/src/misc/hw/hw.c % a.out

Guru Hacker

% cat Hello, world. ^D

New Manager – Back to BASIC

10 PRINT "HELLO WORLD" 20 END

Middle Manager

mail -s "Hello, world." bob@b Bob, could you please write me a program that prints "Hello, world."? I need it by tomorrow. ^D

Senior Manager

% zmail jim I need a "Hello, world." program by this afternoon.

Chief Executive

% letter letter: Command not found. % mail To: ^X ^F ^C % help mail help: Command not found.

% damn! !: Event unrecognized % logout

Language Evaluation Criterion

Jokes aside, this set of programs gives the readers some initial ideas about the different types of languages. Anyway, the question is: what happened to the programming language levels? Why is there so much variation for a simple program like “Hello World”. The answer to this question lies in the question itself – “Hello World” is not the program that should be analyzed to compare different languages. It is so simple that the important features of different programming languages are not highlighted and wrong conclusions can be drawn.

If a programmer has used all these different constructs to increment a variable at different locations, the reader may get confused. It is simpler to adopt one structure and use it consistently.

  • Operator overloading

Operator overloading can be problematic if its use is inconsistent or unconventional.

  • How much simple should it be?

Simplicity has another dimension as well. If a language is very simple then its level may also be decreased as it may loose abstraction. In that case, it will be difficult to write and read algorithms in that language.

Control Statements

Control statements also play an important role in readability. We are all aware of the hazards of goto statement. If a language relies on goto statements for control flow, the logic becomes difficult to follows and hence understand and maintain. Restricted use of goto in extreme was needed and useful as well but with the emergence of new language constructs to handle those situations, it probably not an issue any more.

Data types and data structures

Data types and data structures also play an important role in improving the abstraction level of a programming language, hence making it more readable. For example, a simple omission of Boolean type in C resulted in many problems as integers were instead of Boolean which made the program difficult to follow and debug in many cases.

Similarly, in FORTRAN there was no record (or struct) type. So related data had to be put in different data structures, making it very difficult to read the logic and maintain.

Syntax

Syntax of a language plays a very important role in programming languages. There are many different considerations that we will discuss throughout this course. Here we give a brief overview of some of these considerations.

  • Variable names The first consideration is the restriction of variable names. In the beginning it was a huge issue as some languages restricted the length of a variable name up to 6 or 8 characters making it difficult to choose meaningful names for the variable. Other considerations include the type of special characters allowed in the variable names to improve readability.
  • Special keywords for signaling the start and end of key words.

Some languages allow special keywords for signaling the start and end of a construct, making it more readable and less prone to errors. This can be elaborated with the help of following examples:

Let us first consider the following C if statement.

if (some condition) // do this // now do this

It may be noted that the second part “// now do this” is not part of the body of the if statement and it will be executed regardless of the truth value of the condition. This may cause problems as it is easy for the reader to miss this point because of indentation.

Ada programming language solves this problem by using the keywords “then” and “end if” in the if statement. The resulting code is shown below:

if (some condition) then -- do this end if -- now do this

Now it can be easily seen that a reader of this program will not be confused by the indentation.

Another similar example is when the if has an else part also. Let us first consider the following C code.

if (cond1) if (cond2) // do 1 else if (cond3) // do 2 else // do 3

Is the last else part of third if or the first one? Once again the indentation has caused this confusion and the reader can easily miss this point. Ada has solved this problem by introducing an elsif keyword. The Ada code is shown below and it can be seen very easily that the confusion has been removed.