• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 07:22
CEST 13:22
KST 20:22
  • 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
[ASL21] Ro16 Preview Pt2: All Star10Team Liquid Map Contest #22 - The Finalists16[ASL21] Ro16 Preview Pt1: Fresh Flow9[ASL21] Ro24 Preview Pt2: News Flash10[ASL21] Ro24 Preview Pt1: New Chaos0
Community News
2026 GSL Season 1 Qualifiers13Maestros of the Game 2 announced72026 GSL Tour plans announced14Weekly Cups (April 6-12): herO doubles, "Villains" prevail1MaNa leaves Team Liquid24
StarCraft 2
General
Maestros of the Game 2 announced Team Liquid Map Contest #22 - The Finalists MaNa leaves Team Liquid 2026 GSL Tour plans announced Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool
Tourneys
2026 GSL Season 1 Qualifiers GSL CK: More events planned pending crowdfunding RSL Revival: Season 5 - Qualifiers and Main Event Sparkling Tuna Cup - Weekly Open Tournament Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players [M] (2) Frigid Storage
External Content
Mutation # 522 Flip My Base The PondCast: SC2 News & Results Mutation # 521 Memorable Boss Mutation # 520 Moving Fees
Brood War
General
ASL21 General Discussion Data needed BGH Auto Balance -> http://bghmmr.eu/ Any progamer "explanation" videos like this one? ASL21 Strategy, Pimpest Plays Discussions
Tourneys
[ASL21] Ro16 Group D [Megathread] Daily Proleagues [ASL21] Ro16 Group C [ASL21] Ro16 Group B
Strategy
Simple Questions, Simple Answers What's the deal with APM & what's its true value Any training maps people recommend? Fighting Spirit mining rates
Other Games
General Games
Dawn of War IV Nintendo Switch Thread Starcraft Tabletop Miniature Game General RTS Discussion Thread Battle Aces/David Kim RTS Megathread
Dota 2
The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread YouTube Thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [Req][Books] Good Fantasy/SciFi books Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion McBoner: A hockey love story Cricket [SPORT]
World Cup 2022
Tech Support
[G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Sexual Health Of Gamers
TrAiDoS
lurker extra damage testi…
StaticNine
Broowar part 2
qwaykee
Funny Nicknames
LUCKY_NOOB
Iranian anarchists: organize…
XenOsky
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1722 users

The Big Programming Thread - Page 828

Forum Index > General Forum
Post a Reply
Prev 1 826 827 828 829 830 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.
Acrofales
Profile Joined August 2010
Spain18271 Posts
January 13 2017 20:27 GMT
#16541
On January 14 2017 04:32 travis wrote:
Well what's the reason I am not supposed to do this again? Because I do want there to be only one of them and I do want to be able to access it from other classes (geesh I don't want to write a thousand getters).

I guess I can make everything non static, but then none of my GUI can be static either. changes my whole design a lot

http://lmgtfy.com/?q=why are global variables bad

Seriously, there's a whole page of people explaining that for you in any language you prefer.

It's not even the static that I object to, although static has all its own problems. It's the combination of public and static (without final), which makes it particularly egregious.

static private fields are not very nice and have their own issues, but they are sometimes the neatest solution (not often).

But okay, I can go along with the above. Experience is the best teacher. There is nothing technically wrong with global variables. They are a fully functioning part of almost all programming languages, often for good reasons. It's just that actually using them almost always leads to problems (sometimes very big problems) down the road, when you "just" want to add some minor functionality.

Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
January 13 2017 20:29 GMT
#16542
I googled it don't get sassy
WolfintheSheep
Profile Joined June 2011
Canada14127 Posts
Last Edited: 2017-01-13 21:11:24
January 13 2017 21:11 GMT
#16543
On January 14 2017 05:26 travis wrote:
in an unrelated thought, it seems really stupid that java can't cast int to string, I have to use Integer.toString() or some other method, and yet I can simply say that (the string = the int + ""). I'd love to have that one explained to me.

Plenty of reasons why this is terrible.

For one, what is "string = int +'1'" supposed to do? If int = 1, are you getting '11' as the resulting string? '2'? Or what if I want "string = 1+1+'1'"?

Assuming you have a response to those questions which is reasonable and generally accepted by everyone else using your scripting language, how are you going to implement this, and for all other object types you think Strings should account for? Whatever your solution, you're going to add a lot of overhead to the + operator whether it's runtime or compiler.

Obviously with higher level programming languages, there's a lot of shortcuts and behind the scenes thinking that the language does for you. But not wanting to be explicit with your instructions is not a very good reason.
Average means I'm better than half of you.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
January 13 2017 21:34 GMT
#16544
On January 14 2017 06:11 WolfintheSheep wrote:
Show nested quote +
On January 14 2017 05:26 travis wrote:
in an unrelated thought, it seems really stupid that java can't cast int to string, I have to use Integer.toString() or some other method, and yet I can simply say that (the string = the int + ""). I'd love to have that one explained to me.

Plenty of reasons why this is terrible.

For one, what is "string = int +'1'" supposed to do? If int = 1, are you getting '11' as the resulting string? '2'? Or what if I want "string = 1+1+'1'"?

Assuming you have a response to those questions which is reasonable and generally accepted by everyone else using your scripting language, how are you going to implement this, and for all other object types you think Strings should account for? Whatever your solution, you're going to add a lot of overhead to the + operator whether it's runtime or compiler.

Obviously with higher level programming languages, there's a lot of shortcuts and behind the scenes thinking that the language does for you. But not wanting to be explicit with your instructions is not a very good reason.


I don't think you are understanding what I am saying. I am saying that java lets you turn an integer into a string by calling it a string and appending "" to the end of it. But it doesn't simply let you call an int a string or even cast an int to a string. To me, this is very silly. Why should I use String = Integer.toString(int), when I can use String = int + "".

I mean, I am sure there are specific times I might want to. But in general, I don't get it. It seems to automatically cast in this specific instance but it won't cast if you actually ask it to cast for you.
Acrofales
Profile Joined August 2010
Spain18271 Posts
Last Edited: 2017-01-13 21:50:58
January 13 2017 21:50 GMT
#16545

String a = "" + 5;

works. It's fugly, but it works. The reason it works is because internally some magic happens. That same magic doesn't happen if you simply say:


String a = 5;

and that's a good thing. For that matter, you cannot cast an int to a string in any language I know of. Try in Python:


foo = "hello"
bar = foo + 5


And what your conversion is doing is not casting. It works differently.
RoomOfMush
Profile Joined March 2015
1296 Posts
January 13 2017 21:57 GMT
#16546
@travis:
The only reason why you can do one but not the other is that the Java devs decided this way. There is no technical reason. No really convincing arguments. It just is because somebody said so. That will be the answer to many many questions about certain quirks of certain programming languages. Each language has some odd property which can only be explained with personal taste of the devs.
If there is one language you dont think the above rule applies to then you simply think the same way as the devs of that particular language do.
WolfintheSheep
Profile Joined June 2011
Canada14127 Posts
January 13 2017 21:58 GMT
#16547
On January 14 2017 06:34 travis wrote:
Show nested quote +
On January 14 2017 06:11 WolfintheSheep wrote:
On January 14 2017 05:26 travis wrote:
in an unrelated thought, it seems really stupid that java can't cast int to string, I have to use Integer.toString() or some other method, and yet I can simply say that (the string = the int + ""). I'd love to have that one explained to me.

Plenty of reasons why this is terrible.

For one, what is "string = int +'1'" supposed to do? If int = 1, are you getting '11' as the resulting string? '2'? Or what if I want "string = 1+1+'1'"?

Assuming you have a response to those questions which is reasonable and generally accepted by everyone else using your scripting language, how are you going to implement this, and for all other object types you think Strings should account for? Whatever your solution, you're going to add a lot of overhead to the + operator whether it's runtime or compiler.

Obviously with higher level programming languages, there's a lot of shortcuts and behind the scenes thinking that the language does for you. But not wanting to be explicit with your instructions is not a very good reason.


I don't think you are understanding what I am saying. I am saying that java lets you turn an integer into a string by calling it a string and appending "" to the end of it. But it doesn't simply let you call an int a string or even cast an int to a string. To me, this is very silly. Why should I use String = Integer.toString(int), when I can use String = int + "".

I mean, I am sure there are specific times I might want to. But in general, I don't get it. It seems to automatically cast in this specific instance but it won't cast if you actually ask it to cast for you.

Ah.

Okay, in that case apparently it's because Java uses a StringBuilder with the + operator. So it's basically a hack using Java's backend magick.
Average means I'm better than half of you.
Manit0u
Profile Blog Joined August 2004
Poland17731 Posts
January 13 2017 22:47 GMT
#16548
@travis: If you're concerned about code verbosity (type conversion, not wanting to write getters and setters etc.) then perhaps Java isn't the language for you

Also, go learn some C (just the basics). It'll shed some light on certain problems and you'll understand some higher-level concepts better (and appreciate/or not how much higher level languages are doing for you).
Time is precious. Waste it wisely.
tofucake
Profile Blog Joined October 2009
Hyrule19203 Posts
January 14 2017 00:50 GMT
#16549
On January 14 2017 06:50 Acrofales wrote:

String a = "" + 5;

works. It's fugly, but it works. The reason it works is because internally some magic happens. That same magic doesn't happen if you simply say:


String a = 5;

and that's a good thing. For that matter, you cannot cast an int to a string in any language I know of. Try in Python:


foo = "hello"
bar = foo + 5


And what your conversion is doing is not casting. It works differently.

I mean...you can in PHP
Liquipediaasante sana squash banana
Mr. Wiggles
Profile Blog Joined August 2010
Canada5894 Posts
January 14 2017 01:07 GMT
#16550
Reminds me of this video:

https://www.destroyallsoftware.com/talks/wat

Implicit type conversions can lead to some funny stuff!
you gotta dance
Manit0u
Profile Blog Joined August 2004
Poland17731 Posts
January 14 2017 01:42 GMT
#16551
On January 14 2017 09:50 tofucake wrote:
Show nested quote +
On January 14 2017 06:50 Acrofales wrote:

String a = "" + 5;

works. It's fugly, but it works. The reason it works is because internally some magic happens. That same magic doesn't happen if you simply say:


String a = 5;

and that's a good thing. For that matter, you cannot cast an int to a string in any language I know of. Try in Python:


foo = "hello"
bar = foo + 5


And what your conversion is doing is not casting. It works differently.

I mean...you can in PHP


It works funny in PHP too


'hello' + 5 = 5
'2' + 5 = 7
'a2' + 5 = 7
'2a' + 5 = 7
'a2a' + 5 = 5
Time is precious. Waste it wisely.
mantequilla
Profile Blog Joined June 2012
Turkey781 Posts
January 14 2017 12:34 GMT
#16552
https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism

I sometimes try to do the refactoring stated above (replacing if-else or switch with polymorphism) but often one problem remains

when you have different Bird classes like in the example, when appropriate Bird objects are being created, someone has to decide what kind of bird to create. To do it, the creator must use a conditional, like:


if (bla bla bla)
bird = new NorwegianBlue()
else if (bla bla)
bird = new Swallow()

//etc...


it seems like you are just relocating the complexity to somewhere else by doing this refactoring. It is still an improvement though, since you will likely create them in 1-2 place, but use them in many places, you don't have to check the type every time you use them.

A layer has to take the responsibility of creating the correct type of object. Maybe those birds are stored in a db so data access layer can do it.

am I missing something here? any advice to do it better?
Age of Mythology forever!
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
January 14 2017 12:54 GMT
#16553
Yes, at some point you need to differentiate and its usually through an if block or something that is secretly equivalent to that (alternatives usually involve some kind of reflection shenanigans). That piece of code is usually in some kind of factory class or method. You don't repeat it anywhere. Ideally you can even hide that there are multiple subclasses.

If you have the if block in one method of your class, you're likely to have it in a couple of other methods as well. And you don't want the same if block in 10 places.
If you have a good reason to disagree with the above, please tell me. Thank you.
mantequilla
Profile Blog Joined June 2012
Turkey781 Posts
January 14 2017 15:01 GMT
#16554
On January 14 2017 21:54 spinesheath wrote:
Yes, at some point you need to differentiate and its usually through an if block or something that is secretly equivalent to that (alternatives usually involve some kind of reflection shenanigans). That piece of code is usually in some kind of factory class or method. You don't repeat it anywhere. Ideally you can even hide that there are multiple subclasses.

If you have the if block in one method of your class, you're likely to have it in a couple of other methods as well. And you don't want the same if block in 10 places.


Cool. I was thinking that I was missing something or doing it wrong.
Age of Mythology forever!
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
January 14 2017 16:52 GMT
#16555
--- Nuked ---
RoomOfMush
Profile Joined March 2015
1296 Posts
January 14 2017 20:15 GMT
#16556
Really depends on your conditions but you can often use maps with factory objects. Java example:

private final Map<Key, BirdFactory> factMap = new HashMap();
{
factMap.put(keyNo1, () -> new Swallow());
factMap.put(keyNo2, () -> new Raven());
// ...
}

public Bird createBird(Key key) {
BirdFactory fact = factMap.get(key);
if (fact == null) {
return null; //or some default bird implementation perhaps
}
return fact.create();
}

Then, if you need to create birds for different birdcages you can assign a key to each cage (in a database, read from a file, fetch over the internet, whatever) and use that to create the bird completely without any if's.
Cyx.
Profile Joined November 2010
Canada806 Posts
January 17 2017 18:57 GMT
#16557
On January 14 2017 21:34 mantequilla wrote:

if (bla bla bla)
bird = new NorwegianBlue()
else if (bla bla)
bird = new Swallow()

//etc...


it seems like you are just relocating the complexity to somewhere else by doing this refactoring. It is still an improvement though, since you will likely create them in 1-2 place, but use them in many places, you don't have to check the type every time you use them.

I think this actually reduces two conditionals to one. The caller already had to decide which type of bird to create:


if (bla bla bla)
bird = new Bird(NORWEGIAN_BLUE)
else if (bla bla)
bird = new Bird(SWALLOW)


So all the refactor does is remove that conditional from the class, which is a Good Thing(tm). (Actually, it doesn't get rid of the conditional - the compiler still has to do the virtual dispatch which is probably a little slower. But it makes the compiler do it for you, which is almost always good.)
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
January 17 2017 19:59 GMT
#16558
On January 18 2017 03:57 Cyx. wrote:
So all the refactor does is remove that conditional from the class, which is a Good Thing(tm). (Actually, it doesn't get rid of the conditional - the compiler still has to do the virtual dispatch which is probably a little slower. But it makes the compiler do it for you, which is almost always good.)

Virtual dispatch is pretty damn fast. Especially compared to a conditional with more than 2 branches. Also a conditional messes with branch prediction.
If you have a good reason to disagree with the above, please tell me. Thank you.
Manit0u
Profile Blog Joined August 2004
Poland17731 Posts
January 18 2017 14:41 GMT
#16559


LOL
Time is precious. Waste it wisely.
Cyx.
Profile Joined November 2010
Canada806 Posts
January 18 2017 22:24 GMT
#16560
On January 18 2017 04:59 spinesheath wrote:
Virtual dispatch is pretty damn fast. Especially compared to a conditional with more than 2 branches. Also a conditional messes with branch prediction.

Yeah you're right actually, I was thinking there was a test involved in virtual dispatch for some reason but it's just a lookup in an array which is a lot faster than the test every time.
Prev 1 826 827 828 829 830 1032 Next
Please log in or register to reply.
Live Events Refresh
RSL Revival
10:00
Season 5 Korea Qualifier
Solar vs SHINLIVE!
Classic vs Percival
Ryung 793
CranKy Ducklings SOOP194
CranKy Ducklings182
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Ryung 793
Lowko246
SortOf 140
StarCraft: Brood War
Britney 22467
Calm 6175
Sea 2827
Jaedong 1342
Horang2 958
Hyuk 778
Leta 313
BeSt 294
Mini 236
Soma 230
[ Show more ]
Stork 203
actioN 201
Rush 152
Last 128
Light 128
Larva 121
Snow 106
Dewaltoss 91
ZerO 87
Soulkey 77
Pusan 72
ToSsGirL 68
Sacsri 65
Killer 56
JYJ 53
Hyun 47
Mind 40
Sharp 38
sSak 35
Backho 32
ggaemo 26
sorry 26
[sc1f]eonzerg 25
IntoTheRainbow 24
hero 22
Bale 20
HiyA 19
zelot 17
yabsab 17
soO 16
Hm[arnc] 14
scan(afreeca) 12
Shine 8
Shinee 6
Movie 3
Dota 2
Gorgc3016
Counter-Strike
SPUNJ248
Super Smash Bros
Mew2King146
Other Games
singsing1583
B2W.Neo425
XaKoH 224
Pyrionflax177
Trikslyr128
DeMusliM82
KnowMe71
RotterdaM56
QueenE11
ZerO(Twitch)7
Organizations
Dota 2
PGL Dota 2 - Main Stream9713
Other Games
gamesdonequick645
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 12 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• TFBlade1393
• Jankos1149
Upcoming Events
Replay Cast
12h 38m
The PondCast
22h 38m
KCM Race Survival
22h 38m
WardiTV Map Contest Tou…
23h 38m
Gerald vs herO
Clem vs Cure
ByuN vs Solar
Rogue vs MaxPax
ShoWTimE vs TBD
OSC
1d 3h
CranKy Ducklings
1d 12h
Escore
1d 22h
RSL Revival
2 days
Replay Cast
2 days
WardiTV Map Contest Tou…
2 days
[ Show More ]
Universe Titan Cup
2 days
Rogue vs Percival
Ladder Legends
3 days
uThermal 2v2 Circuit
3 days
BSL
3 days
Sparkling Tuna Cup
3 days
WardiTV Map Contest Tou…
3 days
Ladder Legends
4 days
BSL
4 days
Replay Cast
4 days
Replay Cast
4 days
Wardi Open
4 days
Afreeca Starleague
4 days
Soma vs TBD
Monday Night Weeklies
5 days
Replay Cast
5 days
Afreeca Starleague
5 days
TBD vs YSC
Replay Cast
6 days
Liquipedia Results

Completed

Proleague 2026-04-20
RSL Revival: Season 4
NationLESS Cup

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
KCM Race Survival 2026 Season 2
StarCraft2 Community Team League 2026 Spring
WardiTV TLMC #16
Nations Cup 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026

Upcoming

Escore Tournament S2: W4
Acropolis #4
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
2026 GSL S2
RSL Revival: Season 5
2026 GSL S1
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
BLAST Rivals Spring 2026
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 © 2026 TLnet. All Rights Reserved.