CSC/ECE 517 Fall 2011/ch1 1h ps: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 185: Line 185:
|-
|-
|PHP
|PHP
|<pre>
|ObjectName.MethodName(Parameters);
function mycircle(r) {
|ObjectName.Attribute
  this.radius = r;
 
  this.retCirc = function () {
  return ( Math.PI * this.radius * 2 ); };
}
 
var testcircle = new mycircle(5);
testcircle.retArea();
 
</pre>
|<pre>
function mycircle(r) {
  this.radius = r;
 
  this.retCirc = function () {
  return ( Math.PI * this.radius * 2 ); };
}
 
var testcircle = new mycircle(5);
alert(testcircle.radius);
 
</pre>
|<pre>
|<pre>
var lost = {
var lost = {

Revision as of 03:58, 8 September 2011

Common Attribute/Member Syntax

Different object oriented programming languages include different ways of accessing the member variables and methods. This page discusses the various usages and their advantages and disadvantages.

Introduction

The purpose of Object Oriented Programming is to make programs model things the way people think or deal with the world i.e it's modeled around data rather than logic. Objects form the basis of OOP. Let us now look at objects, their manipulation and the ways to do so.


Objects - Attributes and Methods

Similar to real world objects, software objects also consist of state and related behavior. An object stores it's state in attributes and exposes its behavior through methods. Let's look at the pseudo code for a bicycle object.

CLASS Bicycle
ATTRIBUTES:
	speed: NUMBER
	gear: NUMBER
	pedal_cadence: NUMBER
METHODS:
	ride(speed)
	changeGear(gear)
	changeCadence(pedal_cadence)

In the above pseudo code, the attributes define the object and the methods provide a way of achieving the desired functionality from that object. The methods use the attributes to manipulate the object and achieve the functionality. From this we can observe that this manipulation is the very essence of programming using objects. Therefore the access of attributes and way of defining method calls very important in OOP.

Method call / Instance Variable reference Syntax

A running debate in O-O languages is whether method calls and instance-variable references should have the same syntax. Different programming languages implement this in different ways. Let's look at them by examples.

Same Syntax

Languages like Ruby use the same syntax for method calls and variable references. There is no distinction between these. We can see this in the example below:

class Song
  def duration=(newDuration)
    @duration = newDuration
  end
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.duration	»	260
aSong.duration = 257   # set attribute with updated value
aSong.duration	»	257

This makes it simpler for the programmer to code. Also, it presents uniformity in the code.

Differing Syntax

Languages like Java employ different syntax for method calls and variable references as seen in this example below:

class Song{
	int duration;
	Song(){}
	void setDuration(int dur){
		duration = dur;
	}
}

Song sng = new Song();
sng.setDuration(260);
System.out.println("Song duration = "+sng.duration);

As seen above, a method is always called using parenthesis, within which arguments may be included if required. However, instance variable is accessed directly.

Comparison of syntax between various Programming Languages

The usage syntax of methods, instance variables, getters and setters is in the table below

Programming Languages Method call Fields/Attributes Getter / Setter Comparison
ABAP (Advanced Business Application Programming)
CALL METHOD <meth> EXPORTING... <ii> =.<f i>... 
                   IMPORTING... <ei> =.<g i>... 
                   CHANGING ... <ci> =.<f i>... 
                   RECEIVING         r = h 
                   EXCEPTIONS... <ei> = rc i...
data field type type Same as other methods with the only difference that they start with SET or GET Syntax is different for method calls and instance-variable reference
C++ <instance-variable>.method(parameters) [Eg: x.method()] type field Same as methods. Example below:
int getHealth() const { return health; }
void setHealth(int h) { health = h; }
Different syntax is used to call methods and instance-variable references
C# <instance-variable>.method(parameters) [Eg: x.method()] <instance-variable>.field [Eg: x.field] They are just like methods. Example below:
public string GetName()
    {
        return m_name;
    }

    public void SetName(string name)
    {
        m_name = name;
    }
Different syntax is used to call methods and instance-variable references
Java <instance-variable>.method(parameters) [Eg: x.method()] <instance-variable>.field [Eg: x.field] They are nothing but method calls Different syntax is used to call methods and instance-variable references
D <instance-variable>.method(parameters) [Eg: x.method()] <instance-variable>.field [Eg: x.field] No need for getters and setters. It can be directly accessed.
import std.stdio;

class Car
{
    float speed = 1.0;
}

void main(){
    auto my_car = new Car();
    my_car.speed = 2.5;
    writefln(my_car.speed);
}
Syntax used for method calls and variable references is different
Objective C output = [object methodWithOutput] x->field An example of getter is below
#import <Cocoa/Cocoa.h>
    
@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}

- (NSString*) caption;
- (NSString*) photographer;

@end        

An example of setter is below

#import <Cocoa/Cocoa.h>
    
@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}
- (NSString*) caption;
- (NSString*) photographer;

- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;

@end
Syntax used for method calls and variable references is different
Python x.method(parameters) x.field Pythonic way of implementing getters and setters is to using public members and turning them into properties when needed Syntax used for method calls and variable references is sometimes the same and different at times (when there are arguments)
PHP ObjectName.MethodName(Parameters); ObjectName.Attribute
var lost = {
	loc : "Island",
	get location () {
		return this.loc;
	},
	set location(val) {
		this.loc = val;
	}
};
lost.location = "Another island";
Syntax is different for method calls and instance-variable reference


Conclusion

Using the same syntax for method calls and instance variable references have some advantages and disadvantages. One could argue that having the same syntax makes it simpler for the programmer. It makes the code lighter in terms of lines of code, thus avoiding bulky code. However, including distinct ways for using methods and variables enhances readability by enabling better understanding. This is possible because the manipulation is visible whereas if the syntax were the same(i.e. compact), the one reading has no idea about what is happening "under the hood".

References

  1. Declaring and Calling Methods
  2. Comparison of Programming Languages
  3. Setters and Getters in ABAP
  4. Getters and Setters
  5. Objective C