• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 13:21
CEST 19:21
KST 02:21
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL20] Ro24 Preview Pt2: Take-Off0[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18
Community News
Maestros of The Game—$20k event w/ live finals in Paris24Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195Weekly Cups (Jul 28-Aug 3): herO doubles up6
StarCraft 2
General
What mix of new and old maps do you want in the next 1v1 ladder pool? (SC2) : 2v2 & SC: Evo Complete: Weekend Double Feature Geoff 'iNcontroL' Robinson has passed away The GOAT ranking of GOAT rankings RSL Revival patreon money discussion thread
Tourneys
Maestros of The Game—$20k event w/ live finals in Paris RSL: Revival, a new crowdfunded tournament series Sparkling Tuna Cup - Weekly Open Tournament Monday Nights Weeklies Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
External Content
Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below Mutation # 484 Magnetic Pull
Brood War
General
[ASL20] Ro24 Preview Pt2: Take-Off BW General Discussion No Rain in ASL20? Flash On His 2010 "God" Form, Mind Games, vs JD BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[ASL20] Ro24 Group C [ASL20] Ro24 Group B BWCL Season 63 Announcement [CSLPRO] It's CSLAN Season! - Last Chance
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting Muta micro map competition
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread General RTS Discussion Thread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The year 2050 European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
High temperatures on bridge(s) Gtx660 graphics card replacement Installation of Windows 10 suck at "just a moment"
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
Evil Gacha Games and the…
ffswowsucks
Breaking the Meta: Non-Stand…
TrAiDoS
INDEPENDIENTE LA CTM
XenOsky
[Girl blog} My fema…
artosisisthebest
Sharpening the Filtration…
frozenclaw
ASL S20 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 3956 users

The Big Programming Thread - Page 138

Forum Index > General Forum
Post a Reply
Prev 1 136 137 138 139 140 1031 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
heishe
Profile Blog Joined June 2009
Germany2284 Posts
Last Edited: 2012-05-29 14:08:59
May 29 2012 14:07 GMT
#2741
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


Care to tell us why they suck? Also, feel free to address my post:


On May 29 2012 17:50 heishe wrote:
Suggesting friends for his problems makes no sense. You'd have to add a friend to the class for every type that wants to access its members, which is one of the least generic and hardest to maintain ways to program imaginable.

Friends suck. They introduce code-level dependencies between classes (the class who befriends another must know it, because you have to actually declare the friend in the header), while not really solving any problems.

The only case where they're actually useful is if you purposefully absolutely require that only a specific set of classes must have access to your private data. However, in practice, this should very, very rarely be the case.

If outside classes need to access your private data in its raw form to do stuff with it, there's either no reason to make it private or you're handling your private state in regards to the public interface incorrectly (I think in practice this probably usually means that you've divided responsibilities incorrectly, resulting in the fact that one type of problem needs to access another type of problem's private data because it can not make do with whatever the class provides publicly).

Getters don't suck. Apart from the minimal typing overhead, there's no downside to them. Optimizing them away is one of the most basic optimizations there is, and as such, they're not even slower, since even if you don't mark them as inline, compilers (even GCC which is relatively bad at inlining) will inline them basically all the time, completely removing the cost of a function call.

For data access, usually either make it public, or use getters and(or) setters. Which of the two depends on your problem. If you think that the way of accessing the data in the member is never going to be anything else other than a direct read or write (such as would be the case with matrices or vectors, for example), then simply make them public.
For other cases (where you're not sure, or you already have accessing behavior that is not just read/write) use getters and setters.




If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse.
ForgottenOne
Profile Joined August 2010
Romania236 Posts
Last Edited: 2012-05-29 14:30:30
May 29 2012 14:18 GMT
#2742
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


1. If you're gonna learn basketball or soccer, the idea is to do it THE RIGHT way. You learn nothing by trying to kick the ball with your butt. So, no, don't use friends unless you're gosu at C++.

2. Passing the ball around with the proper means (hand/foot) doesn't suck. Is the way to do it. So, yes, getters are the way to access properties of objects.

3. The fun part about this argument is how getters are meant exactly for the problem this guy is having. It's exactly what u do in OOP, hide the field by making it private and provide a public getter. This may actually be the very first lesson of OOP.
Born free, as free as the wind blows...
tofucake
Profile Blog Joined October 2009
Hyrule19077 Posts
May 29 2012 14:33 GMT
#2743
First, explaining why they suck takes a lot of time that I don't have. Basically it boils down to making huge assumptions, which is bad, and it's just adding more complication to code. The only real time they should be used is when they have to do something (first article gives examples) other than set or get.

this and this are good write ups about why they suck, in detail.
Liquipediaasante sana squash banana
tofucake
Profile Blog Joined October 2009
Hyrule19077 Posts
Last Edited: 2012-05-29 14:34:07
May 29 2012 14:33 GMT
#2744
On May 29 2012 23:18 ForgottenOne wrote:
Show nested quote +
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


1. If you're gonna learn basketball or soccer, the idea is to do it THE RIGHT way. You learn nothing by trying to kick the ball with your butt. So, no, don't use friends unless you're gosu at C++.

2. Passing the ball around with the proper means (hand/foot) doesn't suck. Is the way to do it. So, yes, getters are the way to access properties of objects.

3. The fun part about this argument is how getters are meant exactly for the problem this guy is having. It's exactly what u do in OOP, hide the field by making it private and provide a public getter. This may actually be the very first lesson of OOP.

It's dumb. See my above post.
Liquipediaasante sana squash banana
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
May 29 2012 14:40 GMT
#2745
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


Nothing wrong with getters. Like a lot of normal conventions, there are some crazy people out there that will do anything to vilify it just to make a name for themselves.

Its not that getters suck, its that often people mindlessly create getters when often it could have been encapsulated in a much more meaningful abstraction.

As for using friends instead of getters. That's just one of the many obnoxious features of C++ that should only be used if you have legendary foresight, or not at all, because these days people have realised that its better to just code what is necessary and refactor later than to design (see "the rise of worse is better"). Just stick with getters if you have no alternative way of abstracting it, which a lot of times you just don't.
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
Last Edited: 2012-05-29 14:58:43
May 29 2012 14:43 GMT
#2746
On May 29 2012 23:33 tofucake wrote:
First, explaining why they suck takes a lot of time that I don't have. Basically it boils down to making huge assumptions, which is bad, and it's just adding more complication to code. The only real time they should be used is when they have to do something (first article gives examples) other than set or get.

this and this are good write ups about why they suck, in detail.


Alan Holub is a sensationalist dijkstra wannabe. Take whatever he says with a grain of salt.

---- edit

His articles are just citations of his frequent mental masturbation that have no practicality in the real world.

Here's a good example of what happens when you take his "why extends is evil" article literally.

+ Show Spoiler +

[image loading]


*barf

---- edit

Some satire on variable scope encapsulation, and why you shouldn't use friend.

http://steve-yegge.blogspot.com.au/2010/07/wikileaks-to-leak-5000-open-source-java.html

I used to be an encapsulation nazi, now I've realised its not important and don't scope my variables at all, my code-base has been a lot smaller and easier to maintain since. Although it helps that the language I use doesn't have a convention where attribute names need to be different from method names, so its easy to replace a variable with a getter.
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
ForgottenOne
Profile Joined August 2010
Romania236 Posts
May 29 2012 14:55 GMT
#2747
On May 29 2012 23:33 tofucake wrote:
Show nested quote +
On May 29 2012 23:18 ForgottenOne wrote:
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


1. If you're gonna learn basketball or soccer, the idea is to do it THE RIGHT way. You learn nothing by trying to kick the ball with your butt. So, no, don't use friends unless you're gosu at C++.

2. Passing the ball around with the proper means (hand/foot) doesn't suck. Is the way to do it. So, yes, getters are the way to access properties of objects.

3. The fun part about this argument is how getters are meant exactly for the problem this guy is having. It's exactly what u do in OOP, hide the field by making it private and provide a public getter. This may actually be the very first lesson of OOP.

It's dumb. See my above post.

1. What exactly is dumb?

2. The first answer to the problem was spot on. Everybody should have shut up after that.

But you came along and said he should use friends to solve his problem. I want you to appologize for this. Its such a bad suggestion in this case, it's ridiculous.

3. None of the two articles support your claim that "getters suck". And although the second one makes some interesting points, it doesn't apply at all for the problem the guy was asking. (And, is anyway, way too high level for someone new to C++ and OOP).
Born free, as free as the wind blows...
gedatsu
Profile Joined December 2011
1286 Posts
May 29 2012 15:02 GMT
#2748
On May 29 2012 06:16 snively wrote:
i have a question. suppose i delcare a class in c++. is there any way to make the variables "read-only" outside of the class, so to speak? as in, functions not part of the class could read the variable, but not change its value.

any help wuold be appreciated.

Getters have been suggested, but generally getters and setters should be avoided because they are bad design. Only use them if you want a complete dummy class whose only purpose it to be a group of variables. Usually what you want to do is move the computations into that class.

Bad design:
class asdf {
void dosomething() {
if (jkl.getNumber() > 5) {
//do something
}
}
}

class jkl {
private int number;
public int getNumber() {
return number;
}
}



Better design:
class asdf {
void dosomething() {
jkl.dosomethingifover5();
}
}

class jkl {
private int number;
public void dosomethingifover5() {
if (number > 5) {
//do something
}
}
}
tofucake
Profile Blog Joined October 2009
Hyrule19077 Posts
Last Edited: 2012-05-29 15:24:54
May 29 2012 15:24 GMT
#2749
On May 29 2012 23:40 sluggaslamoo wrote:
Show nested quote +
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


Nothing wrong with getters. Like a lot of normal conventions, there are some crazy people out there that will do anything to vilify it just to make a name for themselves.

Its not that getters suck, its that often people mindlessly create getters when often it could have been encapsulated in a much more meaningful abstraction.

As for using friends instead of getters. That's just one of the many obnoxious features of C++ that should only be used if you have legendary foresight, or not at all, because these days people have realised that its better to just code what is necessary and refactor later than to design (see "the rise of worse is better"). Just stick with getters if you have no alternative way of abstracting it, which a lot of times you just don't.

Actually, there's a lot wrong with getters, particularly when they only get and don't do. There's no need to encapsulate a variable that's going to be modified outside the class with getters and setters, just make it public. If it doesn't need to be modified, friend! Or even public still.


On May 29 2012 23:43 sluggaslamoo wrote:
Show nested quote +
On May 29 2012 23:33 tofucake wrote:
First, explaining why they suck takes a lot of time that I don't have. Basically it boils down to making huge assumptions, which is bad, and it's just adding more complication to code. The only real time they should be used is when they have to do something (first article gives examples) other than set or get.

this and this are good write ups about why they suck, in detail.


Alan Holub is a sensationalist dijkstra wannabe. Take whatever he says with a grain of salt.

---- edit

His articles are just citations of his frequent mental masturbation that have no practicality in the real world.

Here's a good example of what happens when you take his "why extends is evil" article literally.

+ Show Spoiler +

[image loading]


*barf

---- edit

Some satire on variable scope encapsulation, and why you shouldn't use friend.

http://steve-yegge.blogspot.com.au/2010/07/wikileaks-to-leak-5000-open-source-java.html

I used to be an encapsulation nazi, now I've realised its not important and don't scope my variables at all, my code-base has been a lot smaller and easier to maintain since. Although it helps that the language I use doesn't have a convention where attribute names need to be different from method names, so its easy to replace a variable with a getter.
Holub may be a blowhard but he doesn't just make things up; he's got plenty of good points in there.



On May 29 2012 23:55 ForgottenOne wrote:
Show nested quote +
On May 29 2012 23:33 tofucake wrote:
On May 29 2012 23:18 ForgottenOne wrote:
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


1. If you're gonna learn basketball or soccer, the idea is to do it THE RIGHT way. You learn nothing by trying to kick the ball with your butt. So, no, don't use friends unless you're gosu at C++.

2. Passing the ball around with the proper means (hand/foot) doesn't suck. Is the way to do it. So, yes, getters are the way to access properties of objects.

3. The fun part about this argument is how getters are meant exactly for the problem this guy is having. It's exactly what u do in OOP, hide the field by making it private and provide a public getter. This may actually be the very first lesson of OOP.

It's dumb. See my above post.

1. What exactly is dumb?

2. The first answer to the problem was spot on. Everybody should have shut up after that.

But you came along and said he should use friends to solve his problem. I want you to appologize for this. Its such a bad suggestion in this case, it's ridiculous.

3. None of the two articles support your claim that "getters suck". And although the second one makes some interesting points, it doesn't apply at all for the problem the guy was asking. (And, is anyway, way too high level for someone new to C++ and OOP).

1. Your initial 3rd point. It's dumb. It's pointless complexity. It being the first lesson is also dumb. "Always use getters and setters" is a stupid lesson and I hated it in every course I took. The later courses were better and taught to use them properly. And now that I think about it, comparing C++ to Soccer is also dumb.

2. So telling him to try something else isn't okay? Go away. You don't learn new stuff by doing the same thing over and over.

3. Sorry I used a colloquialism in a forum post and then linked to more formal pages. I'll remember that this is a sprsrsbsns forum from now on and will only use the most proper English possible and will not post without first having my post reviewed by at least 4 different editors from the writing staff. Or I'll just continue posting how I want. Yeah I like the second option there.
Liquipediaasante sana squash banana
Morga
Profile Joined August 2010
Belgium35 Posts
May 29 2012 15:25 GMT
#2750
On May 30 2012 00:02 gedatsu wrote:
Show nested quote +
On May 29 2012 06:16 snively wrote:
i have a question. suppose i delcare a class in c++. is there any way to make the variables "read-only" outside of the class, so to speak? as in, functions not part of the class could read the variable, but not change its value.

any help wuold be appreciated.

Getters have been suggested, but generally getters and setters should be avoided because they are bad design. Only use them if you want a complete dummy class whose only purpose it to be a group of variables. Usually what you want to do is move the computations into that class.

Bad design:
class asdf {
void dosomething() {
if (jkl.getNumber() > 5) {
//do something
}
}
}

class jkl {
private int number;
public int getNumber() {
return number;
}
}



Better design:
class asdf {
void dosomething() {
jkl.dosomethingifover5();
}
}

class jkl {
private int number;
public void dosomethingifover5() {
if (number > 5) {
//do something
}
}
}


I disagree, if the knowledge of the >5 belongs in the 'bad design' part of the code then your 'good' design is horrible. Imagine this situation :
A Warehouse needs to be restocked & orders have to be placed for this, to manage the ordering of stock you check what items need to be ordered extra. I'ts not because you can solve it with polymorphism that you should.


class Warehouse
{
public int getItemCount(ItemType)
{
//dull implementation
}
private void changed()
{
for each OrderManagmentObject o
o.notifyChange();
}
}
class WarehouseStockManagementObject // bad name lol :D
{
//this should probably be a method that is only visible to warehouse.
public void notifyChange()
{
if(warehouse.getItemCount(LightBulb)+ordermanager.getTotalOrdered(LightBulb) < 5)
{
// place order
}

}
}



There are many other factors that need to be considered obviously but don't ever say things like that's bad because the design is bad.
gedatsu
Profile Joined December 2011
1286 Posts
May 29 2012 15:40 GMT
#2751
On May 30 2012 00:25 Morga wrote:
Show nested quote +
On May 30 2012 00:02 gedatsu wrote:
On May 29 2012 06:16 snively wrote:
i have a question. suppose i delcare a class in c++. is there any way to make the variables "read-only" outside of the class, so to speak? as in, functions not part of the class could read the variable, but not change its value.

any help wuold be appreciated.

Getters have been suggested, but generally getters and setters should be avoided because they are bad design. Only use them if you want a complete dummy class whose only purpose it to be a group of variables. Usually what you want to do is move the computations into that class.

Bad design:
class asdf {
void dosomething() {
if (jkl.getNumber() > 5) {
//do something
}
}
}

class jkl {
private int number;
public int getNumber() {
return number;
}
}



Better design:
class asdf {
void dosomething() {
jkl.dosomethingifover5();
}
}

class jkl {
private int number;
public void dosomethingifover5() {
if (number > 5) {
//do something
}
}
}


I disagree, if the knowledge of the >5 belongs in the 'bad design' part of the code then your 'good' design is horrible. Imagine this situation :
A Warehouse needs to be restocked & orders have to be placed for this, to manage the ordering of stock you check what items need to be ordered extra. I'ts not because you can solve it with polymorphism that you should.


class Warehouse
{
public int getItemCount(ItemType)
{
//dull implementation
}
private void changed()
{
for each OrderManagmentObject o
o.notifyChange();
}
}
class WarehouseStockManagementObject // bad name lol :D
{
//this should probably be a method that is only visible to warehouse.
public void notifyChange()
{
if(warehouse.getItemCount(LightBulb)+ordermanager.getTotalOrdered(LightBulb) < 5)
{
// place order
}

}
}



There are many other factors that need to be considered obviously but don't ever say things like that's bad because the design is bad.

In this example your WarehouseStockManagementObject should obviously be an inner class. Or just make notifyChange() a method in Warehouse with ItemType argument. In either case you don't need getters.

I'm not saying you can never use getters. I'm saying they should be avoided when it's possible and doing so won't give you a headache. Obviously you could cook up an example where "dosomething" includes manipulation of objects that you don't want to reveal to jkl - a getter may be needed then. But the design is bad, because classes should manage their own variables.
ForgottenOne
Profile Joined August 2010
Romania236 Posts
May 29 2012 15:45 GMT
#2752
On May 30 2012 00:24 tofucake wrote:
Show nested quote +
On May 29 2012 23:40 sluggaslamoo wrote:
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


Nothing wrong with getters. Like a lot of normal conventions, there are some crazy people out there that will do anything to vilify it just to make a name for themselves.

Its not that getters suck, its that often people mindlessly create getters when often it could have been encapsulated in a much more meaningful abstraction.

As for using friends instead of getters. That's just one of the many obnoxious features of C++ that should only be used if you have legendary foresight, or not at all, because these days people have realised that its better to just code what is necessary and refactor later than to design (see "the rise of worse is better"). Just stick with getters if you have no alternative way of abstracting it, which a lot of times you just don't.

Actually, there's a lot wrong with getters, particularly when they only get and don't do. There's no need to encapsulate a variable that's going to be modified outside the class with getters and setters, just make it public. If it doesn't need to be modified, friend! Or even public still.


Show nested quote +
On May 29 2012 23:43 sluggaslamoo wrote:
On May 29 2012 23:33 tofucake wrote:
First, explaining why they suck takes a lot of time that I don't have. Basically it boils down to making huge assumptions, which is bad, and it's just adding more complication to code. The only real time they should be used is when they have to do something (first article gives examples) other than set or get.

this and this are good write ups about why they suck, in detail.


Alan Holub is a sensationalist dijkstra wannabe. Take whatever he says with a grain of salt.

---- edit

His articles are just citations of his frequent mental masturbation that have no practicality in the real world.

Here's a good example of what happens when you take his "why extends is evil" article literally.

+ Show Spoiler +

[image loading]


*barf

---- edit

Some satire on variable scope encapsulation, and why you shouldn't use friend.

http://steve-yegge.blogspot.com.au/2010/07/wikileaks-to-leak-5000-open-source-java.html

I used to be an encapsulation nazi, now I've realised its not important and don't scope my variables at all, my code-base has been a lot smaller and easier to maintain since. Although it helps that the language I use doesn't have a convention where attribute names need to be different from method names, so its easy to replace a variable with a getter.
Holub may be a blowhard but he doesn't just make things up; he's got plenty of good points in there.



Show nested quote +
On May 29 2012 23:55 ForgottenOne wrote:
On May 29 2012 23:33 tofucake wrote:
On May 29 2012 23:18 ForgottenOne wrote:
On May 29 2012 22:59 tofucake wrote:
He specifically said he was using C++, and using stuff is (imo) the best way to learn it. And getters do suck. Just because they are prevalent doesn't make them good.


1. If you're gonna learn basketball or soccer, the idea is to do it THE RIGHT way. You learn nothing by trying to kick the ball with your butt. So, no, don't use friends unless you're gosu at C++.

2. Passing the ball around with the proper means (hand/foot) doesn't suck. Is the way to do it. So, yes, getters are the way to access properties of objects.

3. The fun part about this argument is how getters are meant exactly for the problem this guy is having. It's exactly what u do in OOP, hide the field by making it private and provide a public getter. This may actually be the very first lesson of OOP.

It's dumb. See my above post.

1. What exactly is dumb?

2. The first answer to the problem was spot on. Everybody should have shut up after that.

But you came along and said he should use friends to solve his problem. I want you to appologize for this. Its such a bad suggestion in this case, it's ridiculous.

3. None of the two articles support your claim that "getters suck". And although the second one makes some interesting points, it doesn't apply at all for the problem the guy was asking. (And, is anyway, way too high level for someone new to C++ and OOP).

1. Your initial 3rd point. It's dumb. It's pointless complexity. It being the first lesson is also dumb. "Always use getters and setters" is a stupid lesson and I hated it in every course I took. The later courses were better and taught to use them properly. And now that I think about it, comparing C++ to Soccer is also dumb.

2. So telling him to try something else isn't okay? Go away. You don't learn new stuff by doing the same thing over and over.

3. Sorry I used a colloquialism in a forum post and then linked to more formal pages. I'll remember that this is a sprsrsbsns forum from now on and will only use the most proper English possible and will not post without first having my post reviewed by at least 4 different editors from the writing staff. Or I'll just continue posting how I want. Yeah I like the second option there.



1. The first lesson is encapsulation, not making getters.
2. He is a newb now getting into C++. The right anwer to his problem IS to make a getter. So it's the right way to learn and practice the fundamentals first. The right way is to learn to hit the ball with your feet. Then you'll see if you can score with your butt.
3. You made an extreme claim against something extremely fundamental in a let's say something cool fashion. Then proceded to sugest an alternate, wrong solution, without providing explanations.

I see you think you kinda know some stuff and you may very well not be completly off. But you really dropped the ball with that first comment.
Born free, as free as the wind blows...
heishe
Profile Blog Joined June 2009
Germany2284 Posts
May 29 2012 16:13 GMT
#2753
On May 29 2012 23:33 tofucake wrote:
First, explaining why they suck takes a lot of time that I don't have. Basically it boils down to making huge assumptions, which is bad, and it's just adding more complication to code. The only real time they should be used is when they have to do something (first article gives examples) other than set or get.

this and this are good write ups about why they suck, in detail.


I have nothing to do right now so I read both articles. Have you even read the things you linked? I will point something out for you:

First link:

Without realizing it, the author isn't even talking about getters and setters, he is talking about design in general. He's complaining about making designs that are too data-driven and thus ugly. This, however, has nothing to do with getters and setters. Making everything public instead of using getters and setters won't change a thing about the ugliness of the code. Instead of saying "If you have a lot of getters and setters your design is probably bad" he should have said "if the components of your program need to operate on raw data of other components often, your design is probably bad".

As such, the point about getters and setters that the first write is trying to make is completely invalid. The real point he makes (operate on services, not on data) is completely valid, though.

By the way, making friends (as you seem to suggest) makes it much, much worse, because you need to go back to the providing class and add friends if you want someone to have access to your data. You now have a 1:1 dependency between the two classes (provider and receiver of data), even though only the receiver really depends on the provider. You can now not use the provider class in any other project unless you take the receiver with you, which is simply bad.

Second link:

Again, on the entire six pages he doesn't talk about setters and getters once. Sure, he "talks about" getters and setters, but much like the first writer, his problems actually don't have anything to do with getters and setters. Purely replacing them with something else will change nothing about the problem he presents and tries to solve.

(The general design lesson he provides is excellent though, but again, it doesn't have anything to do with getters and setters).

Your point: Huge assumptions

I'm assuming you're referring to the fact that when people try to explain what problems getters and setters solve they say that the access behavior of the required data might change in the future. Why is that a huge assumption? Even if it's not true in 99% of the cases, besides having you write a couple of more characters per data access, how do setters and getters hurt anything in your code, its maintainability, readability or extensibility?

So again, I ask you: What's bad about getters and setters? Please provide better links or take some time to make the points yourself.



If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse.
MisterD
Profile Blog Joined June 2010
Germany1338 Posts
May 29 2012 16:23 GMT
#2754
getters and setters are actually be usefull for providing a clean interface, or actually providing values through an abstract interface at all (provided your language doesn't have interfaces that can declare fields).

Also, don't maintain getter/setter code manually, that's tedious work that can be automated. Where possible, i for myself have moved to use tools like http://projectlombok.org/ for java, that allows you to just make a field with accessors:

@Getter
@Setter
private String name;

Or if you have a flat data object, you can just @Getter and/or @Setter the entire class, and it even automatically adds null-checks to setters if you define fields as @NonNull. and there you have it, almost no code bloat, interface conformity, possibility to add nullchecks or change the underlying implementation without changing the outside interface, and additional maintenance is reduced to a minimum. The only feature that's missing from that is that if you do a field rename, it automatically makes a method rename on all getters and setters, but i assume they will include such a feature soon.
Gold isn't everything in life... you need wood, too!
typedef struct
Profile Joined January 2011
United States84 Posts
May 29 2012 16:26 GMT
#2755
This debate says a lot about object-oriented programming in general. You'd never find this kind of disagreement about a fundamental concept in a functional language/style.

Moving the functionality into the class you are accessing (the technique getdatsu posted) is only the correct solution if said functionality is something the class is directly responsible for. If not, you're just adding more responsiblities to the class (in violation of the Single Responsiblity Principle).

Friend is almost never the answer, and if you're new to C++ you should forget that keyword exists (as evidenced by the fact that plenty of good OOP languages don't have it).

Providing a getter method is usually the best solution. You can see this by exploring one of the many well-written open-source OOP code bases out there. You will see getters/setters used, but not abused.

It's pretty easy to tell if you're overusing getters/setters, because you'll get f*cking sick of writing them and be tempted to just make everything public. If you run into this situation, you probably have too few classes for what you are trying to do, and should break some of them up.
gedatsu
Profile Joined December 2011
1286 Posts
May 29 2012 16:55 GMT
#2756
On May 30 2012 01:26 typedef struct wrote:
Moving the functionality into the class you are accessing (the technique getdatsu posted) is only the correct solution if said functionality is something the class is directly responsible for. If not, you're just adding more responsiblities to the class (in violation of the Single Responsiblity Principle).

Even if you don't want to delegate the whole subroutine to the jkl class, you should still avoid a getter and make a specific-purpose test method.

asdf class:
if (jkl.testcertainconditions()) {
//do something
}

jkl class:
public boolean testcertainconditions() {
return number > 5;
}

This in no way violates SRP, but in fact it strengthens it as it ensure that no other class gets the responsibility of fiddling with jkl's variables. It also creates a higher degree of abstraction, as it hides implementation details of class jkl. The warehouse example could use a similar approach:
if (warehouse.itsTimeToBuyNew(LightBulb)) {
//code to buy new light bulbs
}
typedef struct
Profile Joined January 2011
United States84 Posts
Last Edited: 2012-05-29 17:16:39
May 29 2012 17:11 GMT
#2757
Oh yeah, definitely. If you can avoid having your getters return member variables directly, and instead have them return some meaningful abstraction of the object's state, that's usually a good thing. "itsTimeToBuyNew" is better than "numberInStock".

To nitpick, you are still adding responsiblity, because now the Warehouse class now needs to know at what point new lightbulbs should be ordered (the "5" in your example). That may be the responsiblity of another class (maybe one that tracks the median batch size for lightbulb orders, and uses that value to determine when new ones should be ordered).

EDIT: I guess by "getter" I didn't mean pure getters that just return a variable, but rather any method that queries an object's state but doesn't modify it. Basically any method that can be made const.
heishe
Profile Blog Joined June 2009
Germany2284 Posts
May 29 2012 17:52 GMT
#2758
On May 30 2012 01:55 gedatsu wrote:
Show nested quote +
On May 30 2012 01:26 typedef struct wrote:
Moving the functionality into the class you are accessing (the technique getdatsu posted) is only the correct solution if said functionality is something the class is directly responsible for. If not, you're just adding more responsiblities to the class (in violation of the Single Responsiblity Principle).

Even if you don't want to delegate the whole subroutine to the jkl class, you should still avoid a getter and make a specific-purpose test method.

asdf class:
if (jkl.testcertainconditions()) {
//do something
}

jkl class:
public boolean testcertainconditions() {
return number > 5;
}

This in no way violates SRP, but in fact it strengthens it as it ensure that no other class gets the responsibility of fiddling with jkl's variables. It also creates a higher degree of abstraction, as it hides implementation details of class jkl. The warehouse example could use a similar approach:
if (warehouse.itsTimeToBuyNew(LightBulb)) {
//code to buy new light bulbs
}


What you're describing is actually an important part of design-by-contract. The idea is to define certain pre- and postconditions of a method, as well as invariants of a certain class:
If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse.
gyth
Profile Blog Joined September 2009
657 Posts
May 29 2012 18:43 GMT
#2759
On May 30 2012 01:26 typedef struct wrote:
This debate says a lot about object-oriented programming in general.

It isn't fair to indict OOP based on how C++/java tacked on their buzzword support.

You'd never find this kind of disagreement about a fundamental concept in a functional language/style.

Is the C++ implementation of the functional style flawless?
(or is it not a true Scotsman?)
The plural of anecdote is not data.
mmp
Profile Blog Joined April 2009
United States2130 Posts
May 29 2012 18:58 GMT
#2760
Only time I found a need to use getters was when an spec I was implementing demanded I implement a field by name (not a new method), but my implementation wanted to multiplex the field value, so I literally had to compute the correct value every time the field was read, as opposed to manipulating the value in the background which would be far more complex.

There are probably other cases where they're necessary, but I try to avoid them.
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
Prev 1 136 137 138 139 140 1031 Next
Please log in or register to reply.
Live Events Refresh
Chat StarLeague
16:00
Chicago LAN Final Day
Razz vs Julia
StRyKeR vs ZZZero
Semih vs TBD
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SteadfastSC 292
ProTech133
SpeCial 109
BRAT_OK 60
JuggernautJason30
EmSc Tv 25
MindelVK 25
StarCraft: Brood War
Britney 41410
Mini 712
firebathero 164
JulyZerg 124
Hyuk 84
Pusan 63
ggaemo 44
Sacsri 36
soO 34
HiyA 16
[ Show more ]
Free 12
Noble 7
Stormgate
BeoMulf146
Dota 2
Gorgc15197
XcaliburYe254
Counter-Strike
fl0m1565
flusha225
Stewie2K124
Heroes of the Storm
Khaldor275
Other Games
gofns8271
FrodaN1488
Beastyqt803
B2W.Neo334
Hui .302
ToD215
KnowMe148
Organizations
Other Games
gamesdonequick675
StarCraft 2
EmSc Tv 25
EmSc2Tv 25
StarCraft: Brood War
CasterMuse 3
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• StrangeGG 44
• LUISG 27
• IndyKCrew
• AfreecaTV YouTube
• sooper7s
• intothetv
• Kozan
• Migwel
• LaughNgamezSOOP
StarCraft: Brood War
• Michael_bg 8
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
League of Legends
• Nemesis4985
Counter-Strike
• Shiphtur151
• imaqtpie143
Other Games
• WagamamaTV439
Upcoming Events
Replay Cast
6h 39m
Afreeca Starleague
16h 39m
Queen vs HyuN
EffOrt vs Calm
Wardi Open
17h 39m
RotterdaM Event
21h 39m
Replay Cast
1d 6h
Afreeca Starleague
1d 16h
Rush vs TBD
Jaedong vs Mong
WardiTV Summer Champion…
1d 17h
Cure vs Classic
ByuN vs TBD
herO vs TBD
TBD vs NightMare
TBD vs MaxPax
PiGosaur Monday
2 days
Afreeca Starleague
2 days
herO vs TBD
Royal vs Barracks
Replay Cast
3 days
[ Show More ]
The PondCast
3 days
WardiTV Summer Champion…
3 days
Replay Cast
4 days
LiuLi Cup
4 days
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
4 days
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
BSL Team Wars
5 days
Team Hawk vs Team Dewalt
BSL Team Wars
5 days
Team Hawk vs Team Bonyth
SC Evo League
5 days
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
[BSL 2025] Weekly
6 days
SC Evo League
6 days
Liquipedia Results

Completed

Jiahua Invitational
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
CSL Season 18: Qualifier 1
Acropolis #4 - TS1
CSLAN 3
SEL Season 2 Championship
WardiTV Summer 2025
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSL Season 18: Qualifier 2
CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
Sisters' Call Cup
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
Roobet Cup 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.