Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-09-2017, 02:29 PM   #16
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 142Reputation: 142

Originally Posted by sundialsvcs View Post
The whole idea behind "Getters and Setters," in my estimation, is that the language is allowing the users of your class to ignore the details of exactly how the values of any of your properties are retrieved or set. From their point of view, they merely reference the property as they usually do. Meanwhile, the language substitutes a subroutine or function call at each appropriate spot. The end-user's code is none the wiser. In fact, "they neither know nor care!"

Since an "interface" is supposed to be "your class's contract with the public," I would strongly encourage you to simply give them ... "properties." Don't tell them how it actually works, and don't require them to be party to it. ("You want a property? Hey, you got it!" And that's all you need to know. Pay no attention to the little man behind the curtain, who you can't see anyway.)

"Getters and Setters" are properly part of the "implementation." The stuff that goes on behind the curtain. The messy details that they will never have to bother their silly little heads about . . .

Like in my simple example, it only works for right triangles, but you can make the object more useful by expanding it to all triangles and have to use cos() or sin() or tan() or something to come up with the right value for a non-right triangle, but the code already using it will still work to return the longest side of a triangle (not really technically the hypotenuse, since that, by definition refers to a right triangle, but whatever).

Last edited by Laserbeak; 06-09-2017 at 02:35 PM.
Old 06-09-2017, 04:10 PM   #17
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 142Reputation: 142
I wrote a better Triangle Objective-C class, although it compiles, it's completely untested since it's just a class, it has no programatic interface to it.

But, it still might be helpful to those looking to see how these things work:

//  Triangle.h
//  test

#import <Foundation/Foundation.h>

@interface Triangle : NSObject

@property long double lengthOfSide1;
@property long double lengthOfSide2;
@property long double largestAngleInDegrees;
@property (readonly, nonatomic) long double lengthOfLongestSide;
@property (readwrite, nonatomic) long double largestAngleInRadians;

//  Triangle.m
//  test

#import "Triangle.h"

@implementation Triangle

@synthesize lengthOfSide1; //Automatically create getters/setters for side1
@synthesize lengthOfSide2; //Automatically create getters/setters for side2
@synthesize largestAngleInDegrees; //Automatically create getters/setters for the angle between side1 & side2

-(long double) getLengthOfLongestSide {
    if (self.largestAngleInDegrees == (long double) 60.0) {
        return lengthOfSide1; // All sides are of equal length
    } else if (self.largestAngleInDegrees == (long double) 90.0) { // right triange
        return sqrtl([self square:lengthOfSide1] + [self square:lengthOfSide2]);
    } else {
         The law of cosines for calculating one side of a triangle when the angle opposite and the other two sides are known. Can be used in conjunction with the law of sines to find all sides and angles.
         c^2 = a^2 + b^2 - 2ab cos(C)
        long double c = sqrt ([self square:lengthOfSide1] + [self square:lengthOfSide2] - 2 * lengthOfSide1 * lengthOfSide2 * cosl (self.largestAngleInRadians));
        return c;

-(long double)square :(long double)x {
    return x * x;

-(long double) getLargestAngleInRadians {
    return (M_PI * largestAngleInDegrees)/180;

-(void) setLargestAngleInRadians :(long double)inRadians {
    largestAngleInDegrees = (180 * inRadians) / M_PI;


Last edited by Laserbeak; 06-09-2017 at 07:21 PM.
Old 06-09-2017, 04:45 PM   #18
Registered: Apr 2012
Location: California
Posts: 311
Blog Entries: 14

Rep: Reputation: Disabled
Originally Posted by ntubski View Post
I think you misread that other thread, they were saying that an interface having only getters and setters likely indicates a design problem. Taking your Media example, doing it like this would be bad:

public interface Media {
If anything is weird here, it's setPlayingState. I don't know the real type signature, but I can take a pretty good guess that it's going to lead to a switch statement being implemented in each class which implements that method. Polymorphism is for decoupling what is being done from how it's done, so baking in a function that's going to be a switch statement, a construct that instead couples those together, is probably not the best choice. The rest of those don't seem like the worst things ever, but my brain is getting tired.

I think that interfaces and abstract base classes are constructs for implementing polymorphism, and they probably smell, real bad, if they're not actually supporting that goal in a given program.

Beyond that, it's probably some sort of smell to stare at a 6 line program fragment and try to derive the rest of the program, so I'd better stop that.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
3 network interfaces - only works from boot with two interfaces enabled linux_driver Linux - Networking 4 11-26-2014 05:42 PM
ifup: couldn't read interfaces file "/etc/network/interfaces" dobharweim Linux - Networking 1 12-13-2013 11:20 AM
[SOLVED] /etc/network/interfaces configuration for virtual interfaces nonshatter Linux - Networking 4 10-25-2010 06:22 AM
ifup: couldn't read interfaces file "/etc/network/interfaces" debian lenny lorimer73 Linux - Networking 1 08-24-2010 03:47 PM
changing objects parameters via getters inside functions in c++? markhod Programming 1 03-08-2005 05:30 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration