Sie sind auf Seite 1von 8

Programming Language Fundamentals

What are the three concepts fundamental to programming, regardless of the language?
Understand when each of these concepts might apply when writing a program.
Name two strategies that can be helpful in planning your program.
What is Programming?

In the most basic sense, programming means creating a set of instructions for completing some
specific task. In this sense, many of our daily activities can be described as programmatic—they involve
specific steps that often follow a set order. For instance, if you get home from school and want to make
yourself a peanut butter and jelly sandwich, you know that you will have to get two slices of bread, butter
each piece, spread peanut butter on one slice and jelly on the other, and finally put the two together. If you
leave out a step you might end up with something other than a PB&J sandwich. If you do things out of
order—say, you put the slices of bread together before you spread the peanut butter—you'll end up with
a mess.

In this general sense, our lives are filled with programs and programming. When you make your
bed you follow certain steps in a programmatic fashion. The steps must be the correct ones and they must
be in the correct order. If you want to make your grandmother's favorite apple pie, you would ask her how
to do it, and she most likely would send you a program—a recipe. A program is therefore also useful for
replicating a product even if you are far removed from the original creator of the product.

But programs are not only useful for reproducing products. Humans—even young children—have
a remarkable capacity for mastering programmatic processes and behaviors. Take the familiar children's
game, One Potato, Two Potato. This chant is a common way for children to choose who is going to be "it" in
a game. The children will all stand in a circle around the counter and hold their fists out in front of them.
The counter recites the rhyme, striking each fist in the circle in turn for each word of the rhyme. When the
counter strikes a fist on the last word of the rhyme—"...seven potato, more."—that fist is knocked out of
the game. The counter then repeats the rhyme, each time knocking out another fist. The last person with a
fist remaining raised is "it". This simple game has all of the elements of a program—a set sequence of
events, actions performed based on the outcome of a process, and repetition.

In the context of computing, programming means creating a set of instructions not for a person but
for a computer, in order to accomplish a specific task. To do so you use a set of directives—a programming
language—known to both the programmer and the computer operating system. The kind of things we
program computers to do is different from what we “program” ourselves to do. Usually a set of instructions,
or program, for a computer is intended to complete a task that:

 is repetitious, and therefore would exceed human patience or capacity for long term attention to
detail;
 controls machinery in conditions unsuitable for humans because of physical limitations, hazardous
conditions, etc.;
 requires a high degree of accuracy;
 requires high speed.

Basic Programming Concepts

Even though each programming language you use is unique, there are certain concepts common to
all languages, including LiveCode's scripting language. Let's look at three of the most common concepts
and structures used in programming.
Sequence of commands (The right commands in the right order.)

It is important not only to give the right commands or steps—they must also be given in the correct
sequence. We can easily see in some of our mundane examples—making a sandwich, tying one's shoes,
following a recipe—that proper order is essential to our success. We might call such obvious sequences
task order, because the proper sequence is dictated by the nature of the task.

But there are also procedures in which the order of steps is unimportant. Often in such procedures,
a conventional order emerges to avoid confusion. An excellent example in the U.S. context is addressing
letters for mailing. Every school child knows that you do it in this order:

First name Last name


House number Street name
City, State Zip code
Country

Oddly enough, this conventional order is exactly the reverse of how the address is examined at the
various postal distribution points in the mail system. The post office at the point of origin would look at the
country and put the letter in a pile for international mail. The distribution center in the destination country
would look at the zip code and perhaps state. The local post office would examine the street address and
place it in the proper mail carrier's route, and the mail carrier, upon arriving on that street, would place it
into the mailbox bearing that address. Finally, whoever checked the mail at the house would look at the
name and give the letter to the person it was addressed to.

Envelope showing Russian address order


In some countries the conventional order follows the logical task order in addressing envelopes. In
Russia, for example, letters are addressed in exactly the opposite order to the U.S. convention.

Example: You want to clear your screen of all buttons and fields, show a field with text, wait for the user to
click, then hide the field and show the former ones.
To work correctly, not only do all the commands have to be there, they have to be in the right order.

Conditional structures (Do certain things based on a true or false, yes or no decision.)

These provide for one outcome or sequence of events to be executed if a statement is true, and
another outcome or sequence of events to be triggered if the statement is false.

In most programming languages these structures take the form if . . . then . . . else.

The One Potato, Two Potato game uses lots of conditional decisions.
If the counter lands on your fist on the word "more" then you must remove your fist from
the circle.
If both of your fists are knocked out of the circle then you are out of the game.

Computing examples:

Example 1:
If a word exists in a list, then print it out,
Else tell the user that the word does not exist.
Examp le 2:
If a sentence contains the word "silly" then put that sentence into the silly list.
Else if it doesn't contain the word "silly" then put it into the serious list.

Looping structures (A list of instructions to do more than once.)


Used to make the computer repeat a certain command or sequence of commands. The loop may run
for a predetermined number of times, until a certain condition becomes true, or as long as a certain
condition remains true.

Here are some ways that looping might be done:


 Do the following 20 times.
 Do the following once for each word in the list
 Repeat the following until the user presses the option key
 Repeat the following as long as the option key is depressed.

Again, the One Potato game provides an obvious example of a looping structure. The rhyme is
repeated and fists counted for as many times as needed until just one person is left.

Another Example:
Given a list of party guests, assign everyone to one of three groups for "ice-breaker" games.

Programming Strategies

Programming can range in complexity from


solving small problems—like setting an alarm time on
your watch or cell phone—to very sophisticated
instructional or business applications. For more complex
tasks, you can use these strategies to help you think
through the logic of your program before starting to write
code.

A flow chart diagram of the one potato-two potato game

Top-down design

Top-down design is a way of approaching a


complex programming task by first mapping out the
entire program and identifying the major components
that it will require. Then the programmer would use
flowcharts and general statements to represent the
logical flow of your program. Once the major components
are identified, the programmer then focuses on each
component in greater detail, finally culminating in writing
the actual program code for creating each component.

In the example at right, we have represented the


“program” for playing One Potato, Two Potato using a top-
down approach. Each shape in the flowchart represents a major step in the game. Combinations of shapes
and arrows show conditional “if-then” decision points, as well as looping structures in which segments of
the program are repeated, perhaps with slight variations in each iteration.

pseudocode for one-potato two-potato game

Pseudocode

This term, from the prefix pseudo-, 'false' and the root word code, 'programming instructions',
describes a way of representing the detailed steps your program must perform without having to worry
about the specific vocabulary or syntax of a specific programming language. You use your knowledge of the
basic control structures, common sense and logic to write plain-English statements to explain in detail how
you will accomplish each main step. All of the examples shown here and used in class could be considered
forms of pseudocode.

Here is an example of pseudocode that describes our One Potato, Two Potato “program”. Notice how
it attempts to detail all of the steps, conditional statements and looping segments using simple statements,
indents and minimal punctuation brackets.
OBJECT ORIENTED PROGRAMMING
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects",
which may contain data, in the form of fields, often known as attributes; and code, in the form of
procedures, often known as methods. A feature of objects is that an object's procedures can access and
often modify the data fields of the object with which they are associated (objects have a notion of "this" or
"self"). In OOP, computer programs are designed by making them out of objects that interact with one
another. There is significant diversity of OOP languages, but the most popular ones are class-based,
meaning that objects are instances of classes, which typically also determine their type.

Many of the most widely used programming languages are multi-paradigm programming languages
that support object-oriented programming to a greater or lesser degree, typically in combination with
imperative, procedural programming. Significant object-oriented languages include
 Java, C++,
 C#,
 Python,
 PHP,
 Ruby,
 Perl,
 Delphi,
 Objective-C,
 Swift,
 Common Lisp,
 and Smalltalk.

Features
Object-oriented programming uses objects, but not all of the associated techniques and structures
are supported directly in languages which claim to support OOP. The features listed below are, however,
common among languages considered strongly class- and object-oriented (or multi-paradigm with OOP
support), with notable exceptions mentioned.[3][4][5][6]

Shared with non-OOP predecessor languages


Object-oriented programming languages typically share low-level features with high-level
procedural programming languages (which were invented first). The fundamental tools that can be used
to construct a program include:

Variables which can store information formatted in a small number of built-in data types like
integers and alphanumeric characters. This may include data structures like strings, lists and hash tables
that are either built-in or result from combining variables using memory pointers
Procedures – also known as functions, methods, routines, or subroutines – that take input, generate output,
and manipulate data. Modern languages include structured programming constructs like loops and
conditionals.
Modular programming support provides the ability to group procedures into files and modules for
organizational purposes. Modules are namespaced so code in one module will not be accidentally confused
with the same procedure or variable name in another file or module.

Objects and classes


Languages that support object-oriented programming typically use inheritance for code reuse and
extensibility in the form of either classes or prototypes.
Those that use classes support two main concepts:

Classes – the definitions for the data format and available procedures for a given type or class of object;
may also contain data and procedures (known as class methods) themselves. i.e. Classes contains
the data members and member functions.

Objects – instances of classes


Objects sometimes correspond to things found in the real world. For example, a graphics program
may have objects such as "circle", "square", "menu". An online shopping system might have objects
such as "shopping cart", "customer", and "product".[7] Sometimes objects represent more abstract
entities, like an object that represents an open file, or an object which provides the service of
translating measurements from U.S. customary to metric.

Each object is said to be an instance of a particular class (for example, an object with its name field
set to "Mary" might be an instance of class Employee). Procedures in object-oriented programming are
known as methods; variables are also known as fields, members, attributes, or properties. This leads to the
following terms:

Class variables – belong to the class as a whole; there is only one copy of each one
Instance variables or attributes – data that belongs to individual objects; every object has its
own copy of each one

Member variables – refers to both the class and instance variables that are defined by a particular
class

Class methods – belong to the class as a whole and have access only to class variables and inputs
from the procedure call

Instance methods – belong to individual objects, and have access to instance variables for the
specific object they are called on, inputs, and class variables

Objects are accessed somewhat like variables with complex internal structure, and in many
languages are effectively pointers, serving as actual references to a single instance of said object in memory
within a heap or stack. They provide a layer of abstraction which can be used to separate internal from
external code. External code can use an object by calling a specific instance method with a certain set of
input parameters, read an instance variable, or write to an instance variable. Objects are created by calling
a special type of method in the class known as a constructor. A program may create many instances of the
same class as it runs, which operate independently. This is an easy way for the same procedures to be used
on different sets of data.

Object-oriented programming that uses classes is sometimes called class-based programming,


while prototype-based programming does not typically use classes. As a result, a significantly different yet
analogous terminology is used to define the concepts of object and instance.

In some languages classes and objects can be composed using other concepts like traits and mixins.

Dynamic dispatch/message passing


It is the responsibility of the object, not any external code, to select the procedural code to execute
in response to a method call, typically by looking up the method at run time in a table associated with the
object. This feature is known as dynamic dispatch, and distinguishes an object from an abstract data type
(or module), which has a fixed (static) implementation of the operations for all instances. If there are
multiple methods that might be run for a given name, it is known as multiple dispatch.

A method call is also known as message passing. It is conceptualized as a message (the name of the method
and its input parameters) being passed to the object for dispatch.

Encapsulation
If a class does not allow calling code from accessing internal object data and forces access through
methods only, this is a strong form of abstraction or information hiding known as encapsulation. Some
languages (Java, for example) let classes enforce access restrictions explicitly, for example denoting
internal data with the private keyword and designating methods intended for use by code outside the class
with the public keyword. Methods may also be designed public, private, or intermediate levels such as
protected (which typically allows access from other objects of the same class, but not objects of a different
class). In other languages (like Python) this is enforced only by convention (for example, private methods
may have names that start with an underscore). Encapsulation prevents external code from being
concerned with the internal workings of an object. This facilitates code refactoring, for example allowing
the author of the class to change how objects of that class represent their data internally without changing
any external code (as long as "public" method calls work the same way). It also encourages programmers
to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy
comprehension by other programmers. Encapsulation is a technique that encourages decoupling.

Composition, inheritance, and delegation


Objects can contain other objects in their instance variables; this is known as object composition.
For example, an object in the Employee class might contain (point to) an object in the Address class, in
addition to its own instance variables like "first_name" and "position". Object composition is used to
represent "has-a" relationships: every employee has an address, so every Employee object has a place to
store an Address object.

Languages that support classes almost always support inheritance. This allows classes to be
arranged in a hierarchy that represents "is-a-type-of" relationships. For example, class Employee might
inherit from class Person. All the data and methods available to the parent class also appear in the child
class with the same names. For example, class Person might define variables "first_name" and "last_name"
with method "make_full_name()". These will also be available in class Employee, which might add the
variables "position" and "salary". This technique allows easy re-use of the same procedures and data
definitions, in addition to potentially mirroring real-world relationships in an intuitive way. Rather than
utilizing database tables and programming subroutines, the developer utilizes objects the user may be
more familiar with: objects from their application domain.[8]

Subclasses can override the methods defined by superclasses. Multiple inheritance is allowed in
some languages, though this can make resolving overrides complicated. Some languages have special
support for mixins, though in any language with multiple inheritance, a mixin is simply a class that does
not represent an is-a-type-of relationship. Mixins are typically used to add the same methods to multiple
classes. For example, class UnicodeConversionMixin might provide a method unicode_to_ascii() when
included in class FileReader and class WebPageScraper, which don't share a common parent.

Abstract classes cannot be instantiated into objects; they exist only for the purpose of inheritance
into other "concrete" classes which can be instantiated. In Java, the final keyword can be used to prevent a
class from being subclassed.

The doctrine of composition over inheritance advocates implementing has-a relationships using
composition instead of inheritance. For example, instead of inheriting from class Person, class Employee
could give each Employee object an internal Person object, which it then has the opportunity to hide from
external code even if class Person has many public attributes or methods. Some languages, like Go do not
support inheritance at all.

The "open/closed principle" advocates that classes and functions "should be open for extension, but
closed for modification". Delegation is another language feature that can be used as an alternative to
inheritance.

Polymorphism
Subtyping, a form of polymorphism, is when calling code can be agnostic as to whether an object
belongs to a parent class or one of its descendants. For example, a function might call "make_full_name()"
on an object, which will work whether the object is of class Person or class Employee. This is another type
of abstraction which simplifies code external to the class hierarchy and enables strong separation of
concerns.

Open recursion
In languages that support open recursion, object methods can call other methods on the same object
(including themselves), typically using a special variable or keyword called this or self. This variable is late-
bound; it allows a method defined in one class to invoke another method that is defined later, in some
subclass thereof.

Das könnte Ihnen auch gefallen