• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 18:10
CET 00:10
KST 08:10
  • 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
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3RSL Season 3: RO16 results & RO8 bracket13
StarCraft 2
General
BGE Stara Zagora 2026 announced Weekly Cups (Nov 24-30): MaxPax, Clem, herO win SC2 Proleague Discontinued; SKT, KT, SGK, CJ disband Information Request Regarding Chinese Ladder SC: Evo Complete - Ranked Ladder OPEN ALPHA
Tourneys
$5,000+ WardiTV 2025 Championship Constellation Cup - Main Event - Stellar Fest RSL Revival: Season 3 Tenacious Turtle Tussle [Alpha Pro Series] Nice vs Cure
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation
Brood War
General
BW General Discussion [ASL20] Ask the mapmakers — Drop your questions Which season is the best in ASL? FlaSh's Valkyrie Copium BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [BSL21] RO16 Group B - Sunday 21:00 CET [BSL21] RO16 Group C - Saturday 21:00 CET Small VOD Thread 2.0
Strategy
Game Theory for Starcraft How to stay on top of macro? Current Meta PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread The Perfect Game Path of Exile Nintendo Switch Thread Should offensive tower rushing be viable in RTS games?
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
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine US Politics Mega-thread The Big Programming Thread Artificial Intelligence Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
James Bond movies ranking - pa…
Topin
Esports Earnings: Bigger Pri…
TrAiDoS
Thanks for the RSL
Hildegard
Saturation point
Uldridge
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1272 users

The Big Programming Thread - Page 355

Forum Index > General Forum
Post a Reply
Prev 1 353 354 355 356 357 1032 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.
tec27
Profile Blog Joined June 2004
United States3702 Posts
Last Edited: 2013-09-17 05:05:08
September 17 2013 05:04 GMT
#7081
On September 17 2013 14:00 sluggaslamoo wrote:
Show nested quote +
On September 17 2013 12:57 Millitron wrote:
On September 17 2013 09:12 sluggaslamoo wrote:
If your language requires you to write getters/setters then it is probably a terrible language, e.g Java/.NET.

If you don't have a choice however (working in a corporate environment), the debate is asking the wrong question. You shouldn't be lumping Getter and Setter in the same question.

Getters are perfectly fine in any situation and very useful for immutable attributes, in fact there's no point in not having them unless you are just feeling lazy. If it is mutable then don't provide a Getter obviously.

Setters on the other hand are a problem, there are almost no instances where you would need a Setter for an object. If you have setters for your objects you are in the wrong frame of mind and need to do a lot more research on the paradigm of OOP.

Neither Java nor .NET force getters and setters. You can have public instance variables in both of them.


I never said force, I said require.

Force and require are the same thing in that context? It would only be different if you had said "if your language's idiomatic style requires..." or something along those lines. C# also has properties, which remove the need to write getters/setters for "future refactoring possibility" purposes and make it so even the idiomatic style does not encourage them.
Can you jam with the console cowboys in cyberspace?
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
Last Edited: 2013-09-17 05:27:56
September 17 2013 05:27 GMT
#7082
On September 17 2013 14:04 tec27 wrote:
Show nested quote +
On September 17 2013 14:00 sluggaslamoo wrote:
On September 17 2013 12:57 Millitron wrote:
On September 17 2013 09:12 sluggaslamoo wrote:
If your language requires you to write getters/setters then it is probably a terrible language, e.g Java/.NET.

If you don't have a choice however (working in a corporate environment), the debate is asking the wrong question. You shouldn't be lumping Getter and Setter in the same question.

Getters are perfectly fine in any situation and very useful for immutable attributes, in fact there's no point in not having them unless you are just feeling lazy. If it is mutable then don't provide a Getter obviously.

Setters on the other hand are a problem, there are almost no instances where you would need a Setter for an object. If you have setters for your objects you are in the wrong frame of mind and need to do a lot more research on the paradigm of OOP.

Neither Java nor .NET force getters and setters. You can have public instance variables in both of them.


I never said force, I said require.

Force and require are the same thing in that context? It would only be different if you had said "if your language's idiomatic style requires..." or something along those lines. C# also has properties, which remove the need to write getters/setters for "future refactoring possibility" purposes and make it so even the idiomatic style does not encourage them.


If your language's idiomatic style requires you to write getters/setters then it is probably a terrible language.

Properties are really the same as writing getters and setters. When it is pre-compiled they are literally turned into a get_ and set_ method by name.

Future refactoring possibility isn't really a good reason to be writing a setter.

Objects are about providing services to other objects, it is called Responsibility Driven Design. If you are thinking about writing properties you are already doing it wrong.

The only time you should write properties is for creating read-only attributes, but then I don't know why the just don't have read-only attributes instead of having to waste time writing properties. Unfortunately read-only in C# is reserved when they really meant immutable not read-only.
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
Amnesty
Profile Joined April 2003
United States2054 Posts
September 17 2013 05:37 GMT
#7083
Write any container class without setters.
See if you like them better afterwards.

std::vector would be pretty useless without at() and overloaded [] operator for example.
The sky just is, and goes on and on; and we play all our BW games beneath it.
WindWolf
Profile Blog Joined July 2012
Sweden11767 Posts
September 17 2013 05:56 GMT
#7084
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself
EZ4ENCE
tec27
Profile Blog Joined June 2004
United States3702 Posts
September 17 2013 05:58 GMT
#7085
On September 17 2013 14:27 sluggaslamoo wrote:
Show nested quote +
On September 17 2013 14:04 tec27 wrote:
On September 17 2013 14:00 sluggaslamoo wrote:
On September 17 2013 12:57 Millitron wrote:
On September 17 2013 09:12 sluggaslamoo wrote:
If your language requires you to write getters/setters then it is probably a terrible language, e.g Java/.NET.

If you don't have a choice however (working in a corporate environment), the debate is asking the wrong question. You shouldn't be lumping Getter and Setter in the same question.

Getters are perfectly fine in any situation and very useful for immutable attributes, in fact there's no point in not having them unless you are just feeling lazy. If it is mutable then don't provide a Getter obviously.

Setters on the other hand are a problem, there are almost no instances where you would need a Setter for an object. If you have setters for your objects you are in the wrong frame of mind and need to do a lot more research on the paradigm of OOP.

Neither Java nor .NET force getters and setters. You can have public instance variables in both of them.


I never said force, I said require.

Force and require are the same thing in that context? It would only be different if you had said "if your language's idiomatic style requires..." or something along those lines. C# also has properties, which remove the need to write getters/setters for "future refactoring possibility" purposes and make it so even the idiomatic style does not encourage them.


If your language's idiomatic style requires you to write getters/setters then it is probably a terrible language.

Properties are really the same as writing getters and setters. When it is pre-compiled they are literally turned into a get_ and set_ method by name.

Future refactoring possibility isn't really a good reason to be writing a setter.

Objects are about providing services to other objects, it is called Responsibility Driven Design. If you are thinking about writing properties you are already doing it wrong.

The only time you should write properties is for creating read-only attributes, but then I don't know why the just don't have read-only attributes instead of having to waste time writing properties. Unfortunately read-only in C# is reserved when they really meant immutable not read-only.

I don't know why you're arguing with me, I wasn't trying to get into this debate again; its been had at least like 4 times in this thread already. I was just replying because you were a bit short/rude with the previous poster and acted as if what you said made sense. It didn't.

I don't even disagree with you really, so your arguments are kind of useless here.
Can you jam with the console cowboys in cyberspace?
tec27
Profile Blog Joined June 2004
United States3702 Posts
September 17 2013 06:02 GMT
#7086
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself

This is an example of where you probably wouldn't want to name the method setSeed. getX and setX have connotations that they have very little side effects (e.g. getX has no side effects and setX has a single side effect, namely setting X). If they are expensive and/or have more side effects than just that, a different naming choice could indicate that to the user better.

But you're setting up a straw-man for your "reasoning for using setters" here. Your alternative is to repeat the same code everywhere, which is obviously bad and not what any of the anti-gettersetter crowd is arguing for.
Can you jam with the console cowboys in cyberspace?
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
Last Edited: 2013-09-17 06:09:58
September 17 2013 06:06 GMT
#7087
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself


There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.

Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.

The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.

IMO it is also simpler and much more intuitive than using setSeed.
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
rasnj
Profile Joined May 2010
United States1959 Posts
September 17 2013 06:08 GMT
#7088
On September 17 2013 13:34 Amnesty wrote:
Anyone know of a way to put something into a input stream. So there is a default value there.

So instead of


cout >> "Input a number :";
int x;
cin << x;

printing
Input a number :

it would print
Input a number :150_<cursor right here>

For example.


I do not believe there is a standard C++ way of doing this. You can inject data into cin as follows:
std::cin.putback('1');
std::cin.putback('5');
std::cin.putback('0');
But it will not show up on the console and you cannot erase those characters. Generally standard C++ is very limited in what you can do at the console. If you are just using the console because you are still learning and it is more convenient than coding a GUI interface, then I would just settle for something simpler like:
Input a number (default: 150): _

If you actually want to do it properly you will need to do something platform dependent or use a third-party library that abstracts away the platform dependence. On Win32 you could do (this has no error-handling and can only handle digits, but the idea can easily be generalized if you wish):
#include <iostream>
#include <windows.h>

void write_digit_to_stdin(char digit)
{
// Calculate virtual-key code.
// Look up on MSDN for codes for other keys than the digits.
int virtual_key_code = static_cast<int>(digit) + 0x30 - static_cast<int>('0');

// Fill out an input record that simulates pressing the desired key followed
// by releasing the same key.
INPUT_RECORD input_record[2];
for(int i = 0; i < 2; ++i)
{
input_record[i].EventType = KEY_EVENT;
input_record[i].Event.KeyEvent.dwControlKeyState = 0;
input_record[i].Event.KeyEvent.uChar.UnicodeChar = digit;
input_record[i].Event.KeyEvent.wRepeatCount = 1;
input_record[i].Event.KeyEvent.wVirtualKeyCode = virtual_key_code;
input_record[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(virtual_key_code, MAPVK_VK_TO_VSC);
}
input_record[0].Event.KeyEvent.bKeyDown = TRUE;
input_record[1].Event.KeyEvent.bKeyDown = FALSE;

DWORD tmp = 0;
WriteConsoleInput( GetStdHandle(STD_INPUT_HANDLE) ,input_record,2,&tmp);
}

int main()
{
int x;
std::cout << "Input a number: ";
write_digit_to_stdin('1');
write_digit_to_stdin('5');
write_digit_to_stdin('0');
std::cin >> x;
std::cout << "Wrote: " << x << std::endl;

return 0;
}
rasnj
Profile Joined May 2010
United States1959 Posts
September 17 2013 06:20 GMT
#7089
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself

It is some what misleading to call it "set seed" because that suggests that you are just setting an internal seed variable (which very well may happen, but you don't want the user to care). A better way to think of it is that you are asking the RNG to seed itself with some value, you are asking it to perform an action, not accessing a data element (whether or not the two may equivalent in implementation is irrelevant). You should call it "seed" not "setSeed" in my opinion.

However in any case a RNG is probably one of the cases where the user might very well care about implementation-details and therefore leaking implementation details might not be as bad.
Amnesty
Profile Joined April 2003
United States2054 Posts
September 17 2013 06:34 GMT
#7090
On September 17 2013 15:08 rasnj wrote:
Show nested quote +
On September 17 2013 13:34 Amnesty wrote:
Anyone know of a way to put something into a input stream. So there is a default value there.

So instead of


cout >> "Input a number :";
int x;
cin << x;

printing
Input a number :

it would print
Input a number :150_<cursor right here>

For example.


I do not believe there is a standard C++ way of doing this. You can inject data into cin as follows:
std::cin.putback('1');
std::cin.putback('5');
std::cin.putback('0');
But it will not show up on the console and you cannot erase those characters. Generally standard C++ is very limited in what you can do at the console. If you are just using the console because you are still learning and it is more convenient than coding a GUI interface, then I would just settle for something simpler like:
Input a number (default: 150): _

If you actually want to do it properly you will need to do something platform dependent or use a third-party library that abstracts away the platform dependence. On Win32 you could do (this has no error-handling and can only handle digits, but the idea can easily be generalized if you wish):
#include <iostream>
#include <windows.h>

void write_digit_to_stdin(char digit)
{
// Calculate virtual-key code.
// Look up on MSDN for codes for other keys than the digits.
int virtual_key_code = static_cast<int>(digit) + 0x30 - static_cast<int>('0');

// Fill out an input record that simulates pressing the desired key followed
// by releasing the same key.
INPUT_RECORD input_record[2];
for(int i = 0; i < 2; ++i)
{
input_record[i].EventType = KEY_EVENT;
input_record[i].Event.KeyEvent.dwControlKeyState = 0;
input_record[i].Event.KeyEvent.uChar.UnicodeChar = digit;
input_record[i].Event.KeyEvent.wRepeatCount = 1;
input_record[i].Event.KeyEvent.wVirtualKeyCode = virtual_key_code;
input_record[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(virtual_key_code, MAPVK_VK_TO_VSC);
}
input_record[0].Event.KeyEvent.bKeyDown = TRUE;
input_record[1].Event.KeyEvent.bKeyDown = FALSE;

DWORD tmp = 0;
WriteConsoleInput( GetStdHandle(STD_INPUT_HANDLE) ,input_record,2,&tmp);
}

int main()
{
int x;
std::cout << "Input a number: ";
write_digit_to_stdin('1');
write_digit_to_stdin('5');
write_digit_to_stdin('0');
std::cin >> x;
std::cout << "Wrote: " << x << std::endl;

return 0;
}


I'm not new to C++, just i was rather surprised i was stumped on this. I don't think I ever wanted to do such a thing before.

Anyway, i started learning assembly and wrote this puzzle game using just assembly.
[image loading]

Since i'm not really good at assembly yet i wrote the map editor in C++ and its a console app because a full blown GUI app would be a bit overkill.
It would be nice to press S for save and instead of getting a blank input for the filename it would default to something. And after saving it as a certain name, it would default to that too. Its been really annoying typing in the full name every time.

I could just write my own input loop where i push back chars into a string, popback on backspace, and set the cursor position accordingly. But I thought i would ask if there was something easy to do.
The sky just is, and goes on and on; and we play all our BW games beneath it.
WindWolf
Profile Blog Joined July 2012
Sweden11767 Posts
September 17 2013 07:20 GMT
#7091
On September 17 2013 15:06 sluggaslamoo wrote:
Show nested quote +
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself


There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.

Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.

The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.

IMO it is also simpler and much more intuitive than using setSeed.

I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again.

And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)
EZ4ENCE
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
September 17 2013 07:37 GMT
#7092
RNG just happens to be a very unfortunate example since it's probably one of the main examples where you DON'T want to let anything set the value. To get as random results as possible, it's best practice to only make a singleton Random object, set the seed at program start and then poll that same object. Changing seed is just straight up bad practice, so an RNG class which lets you do that wouldn't make much sense.

However, the background concept makes sense of course. If a class has a mutable property in its interface which is implemented with a lot of code, it's definitely worth doing a setter for it. RNG just doesn't have such a mutable property in its interface, you're not supposed to change the seed of a RNG object.
WindWolf
Profile Blog Joined July 2012
Sweden11767 Posts
September 17 2013 07:46 GMT
#7093
On September 17 2013 16:37 Tobberoth wrote:
RNG just happens to be a very unfortunate example since it's probably one of the main examples where you DON'T want to let anything set the value. To get as random results as possible, it's best practice to only make a singleton Random object, set the seed at program start and then poll that same object. Changing seed is just straight up bad practice, so an RNG class which lets you do that wouldn't make much sense.

However, the background concept makes sense of course. If a class has a mutable property in its interface which is implemented with a lot of code, it's definitely worth doing a setter for it. RNG just doesn't have such a mutable property in its interface, you're not supposed to change the seed of a RNG object.

Yeah, I just couldn't think of any better thing at that very moment
EZ4ENCE
alexbrown
Profile Joined August 2013
United Kingdom6 Posts
September 17 2013 08:36 GMT
#7094
Thanks for the information.
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
September 17 2013 23:33 GMT
#7095
On September 17 2013 16:20 WindWolf wrote:
Show nested quote +
On September 17 2013 15:06 sluggaslamoo wrote:
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself


There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.

Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.

The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.

IMO it is also simpler and much more intuitive than using setSeed.

I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again.

And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)


The same principle still applies for lots of things. You want to reduce the amount of side effects to a minimum in order to make maintenance easier. I can't even remember the last time I have used a setter.
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
berated-
Profile Blog Joined February 2007
United States1134 Posts
September 17 2013 23:45 GMT
#7096
On September 18 2013 08:33 sluggaslamoo wrote:
Show nested quote +
On September 17 2013 16:20 WindWolf wrote:
On September 17 2013 15:06 sluggaslamoo wrote:
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself


There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.

Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.

The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.

IMO it is also simpler and much more intuitive than using setSeed.

I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again.

And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)


The same principle still applies for lots of things. You want to reduce the amount of side effects to a minimum in order to make maintenance easier. I can't even remember the last time I have used a setter.


Can you make some clarifications, for those of us in the peanut gallery.

What language do you do most of your programming in? And what are the size of the projects you work on? Also, are you excluding domain objects from outside of your discussions of getters and setters are bad.
phar
Profile Joined August 2011
United States1080 Posts
September 18 2013 00:27 GMT
#7097
Mutable objects that you pass around a lot in general are confusing as shit to keep track of, especially in larger code bases. Immutability wherever possible keeps things sane.

Setters are just one example of a highly mutable object that can easily get out of hand.

For example, see Effective Java ch 15.
Who after all is today speaking about the destruction of the Armenians?
waxypants
Profile Blog Joined September 2009
United States479 Posts
Last Edited: 2013-09-18 01:20:17
September 18 2013 01:17 GMT
#7098
On September 17 2013 16:20 WindWolf wrote:
Show nested quote +
On September 17 2013 15:06 sluggaslamoo wrote:
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself


There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.

Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.

The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.

IMO it is also simpler and much more intuitive than using setSeed.

I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again.

And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)


What you described is not a "setter" just because it has name "set" in it. Of course almost every function or even single statement in programming involves something getting "set" to something else. Doesn't make it a "setter".

edit: Although I think you inadvertently brought up a good question, which is "how much work can a method do before it's not really a 'setter' or 'getter' anymore?" I think you brought up an example where it is clearly doing too much work to be a "setter".
sluggaslamoo
Profile Blog Joined November 2009
Australia4494 Posts
Last Edited: 2013-09-18 04:02:56
September 18 2013 03:57 GMT
#7099
On September 18 2013 08:45 berated- wrote:
Show nested quote +
On September 18 2013 08:33 sluggaslamoo wrote:
On September 17 2013 16:20 WindWolf wrote:
On September 17 2013 15:06 sluggaslamoo wrote:
On September 17 2013 14:56 WindWolf wrote:
One reason I'm wondering why people hate setters and getters:

Lets assume that you've written a RNG that has complex internal code (just as an example) and requires lots of code just to set a seed (once again, just as an example). In that case, I would find it easier to read setSeed() than reading all of the code required to set the seed itself


There are plenty of reasons. In this case it could make the code hard to debug. Someone could easily already be using setSeed() deeply nested within some code file.

Then when your code doesn't randomize differently, you are sitting their scratching your head why your RNG code didn't work when really it was setSeed causing the issue.

The seed should be set in the initialiser or as a config variable or both. You aren't going to need to set the seed on the fly for RNG in a game. When the game starts you initialise the RNG object with the seed using the game's config table, after that it doesn't need to change until the next game. This allows you to isolate the issue and make debugging much easier.

IMO it is also simpler and much more intuitive than using setSeed.

I didn't meant about RNG's specifically, I was just the principal of using a set method for a long piece of code instead of writing all of that code again.

And even is setSeed isn't a good name, what would make for a good name (we can take the debate on whenever you should be able to change the seed of a RGN or not another time)


The same principle still applies for lots of things. You want to reduce the amount of side effects to a minimum in order to make maintenance easier. I can't even remember the last time I have used a setter.


Can you make some clarifications, for those of us in the peanut gallery.

What language do you do most of your programming in? And what are the size of the projects you work on? Also, are you excluding domain objects from outside of your discussions of getters and setters are bad.


I use Ruby mostly, but in certain companies I have had to deal with other languages, and I've worked on both small and behemoth monolithic Java projects.

Basically what phar said. For every time you allow something to change, it means one extra thing you have to think of when debugging.

Setting is best left to initialisers or factories, then the object performs services using those parameters and keeps everything self contained. Instead of using setters, you can create a new object with new parameters.

Think of it as people working within an organisation, you don't set attributes to people, you have different people who are capable of doing different things. "Hire" the people you need, then hand off the work to each of these people to get the work done.

In Ruby I also tend to do a lot of things functionally, which removes the issue of side effects, but you also don't have these hand offs between objects when really you just want something short and sweet.

Its very hard to do right and will take years to figure out how to be able to do it in a way that makes the code look better not worse, let alone figure it out quickly. Just realise that in the long term you are going to be much better off.

The reason people write setters is because they don't realise other patterns exist to allow you to perform operations without them.
Come play Android Netrunner - http://www.teamliquid.net/forum/viewmessage.php?topic_id=409008
Manit0u
Profile Blog Joined August 2004
Poland17490 Posts
Last Edited: 2013-09-18 05:29:58
September 18 2013 04:22 GMT
#7100
On September 17 2013 15:08 rasnj wrote:
Show nested quote +
On September 17 2013 13:34 Amnesty wrote:
Anyone know of a way to put something into a input stream. So there is a default value there.

So instead of


cout >> "Input a number :";
int x;
cin << x;

printing
Input a number :

it would print
Input a number :150_<cursor right here>

For example.


I do not believe there is a standard C++ way of doing this. You can inject data into cin as follows:
std::cin.putback('1');
std::cin.putback('5');
std::cin.putback('0');
But it will not show up on the console and you cannot erase those characters. Generally standard C++ is very limited in what you can do at the console. If you are just using the console because you are still learning and it is more convenient than coding a GUI interface, then I would just settle for something simpler like:
Input a number (default: 150): _

If you actually want to do it properly you will need to do something platform dependent or use a third-party library that abstracts away the platform dependence. On Win32 you could do (this has no error-handling and can only handle digits, but the idea can easily be generalized if you wish):
#include <iostream>
#include <windows.h>

void write_digit_to_stdin(char digit)
{
// Calculate virtual-key code.
// Look up on MSDN for codes for other keys than the digits.
int virtual_key_code = static_cast<int>(digit) + 0x30 - static_cast<int>('0');

// Fill out an input record that simulates pressing the desired key followed
// by releasing the same key.
INPUT_RECORD input_record[2];
for(int i = 0; i < 2; ++i)
{
input_record[i].EventType = KEY_EVENT;
input_record[i].Event.KeyEvent.dwControlKeyState = 0;
input_record[i].Event.KeyEvent.uChar.UnicodeChar = digit;
input_record[i].Event.KeyEvent.wRepeatCount = 1;
input_record[i].Event.KeyEvent.wVirtualKeyCode = virtual_key_code;
input_record[i].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(virtual_key_code, MAPVK_VK_TO_VSC);
}
input_record[0].Event.KeyEvent.bKeyDown = TRUE;
input_record[1].Event.KeyEvent.bKeyDown = FALSE;

DWORD tmp = 0;
WriteConsoleInput( GetStdHandle(STD_INPUT_HANDLE) ,input_record,2,&tmp);
}

int main()
{
int x;
std::cout << "Input a number : ";
write_digit_to_stdin('1');
write_digit_to_stdin('5');
write_digit_to_stdin('0');
std::cin >> x;
std::cout << "Wrote: " << x << std::endl;

return 0;
}


Why make it so complicated?


#include <iostream>
#include <string>
#include <locale>
#include <sstream>
using namespace std;

int main()
{
locale loc;
int x = 150;
string str;

cout << "Input a number (default 150): ";
getline(cin, str);

if(strlen str)
{
if(isdigit(str[0], loc))
{
stringstream(str) >> x;
}
}

cout << "Your chosen number is: " << x << endl;

return 0;
}
Time is precious. Waste it wisely.
Prev 1 353 354 355 356 357 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 1h 50m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
PiGStarcraft619
elazer 272
SpeCial 102
JuggernautJason94
ForJumy 35
CosmosSc2 33
StarCraft: Brood War
Artosis 166
ZZZero.O 163
NaDa 22
Dota 2
capcasts179
Counter-Strike
Foxcn312
minikerr33
Other Games
Grubby6546
tarik_tv4163
FrodaN3545
shahzam682
ceh9667
Liquid`Hasu331
C9.Mang0190
Mew2King123
ViBE46
NarutO 12
Organizations
Other Games
BasetradeTV61
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• musti20045 44
• davetesta26
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Doublelift4305
• TFBlade1027
Other Games
• imaqtpie1589
• Shiphtur139
Upcoming Events
PiGosaur Monday
1h 50m
Wardi Open
12h 50m
StarCraft2.fi
17h 50m
Replay Cast
1d
The PondCast
1d 10h
OSC
1d 16h
Demi vs Mixu
Nicoract vs TBD
Babymarine vs MindelVK
ForJumy vs TBD
Shameless vs Percival
Replay Cast
2 days
Korean StarCraft League
3 days
CranKy Ducklings
3 days
SC Evo League
3 days
[ Show More ]
BSL 21
3 days
Sziky vs OyAji
Gypsy vs eOnzErG
OSC
3 days
Solar vs Creator
ByuN vs Gerald
Percival vs Babymarine
Moja vs Krystianer
EnDerr vs ForJumy
sebesdes vs Nicoract
Sparkling Tuna Cup
4 days
OSC
4 days
BSL 21
4 days
Bonyth vs StRyKeR
Tarson vs Dandy
Replay Cast
5 days
Wardi Open
5 days
StarCraft2.fi
5 days
Replay Cast
6 days
StarCraft2.fi
6 days
Liquipedia Results

Completed

Proleague 2025-11-28
RSL Revival: Season 3
Light HT

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
Acropolis #4 - TS3
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
Kuram Kup
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
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.