Hello guys! I like to use switch and case, I find it to be cleaner. I quite understand if and else if exist for a reason and they can be used just like switch and case(Although this rather defeats the purpose of switch and case). In my case I use if and else if's like so:
Code if(X > Y && IsReady) {
Start ();
} else if(!IsReady) {
Abort();
} else {
Load();
}
However if I have something like this:
Code if(X == 0) {
Abort ();
} else if(X == 1) {
Load ();
} else if(X == 2) {
Start ();
} else {
Abort();
}
I might convert it to a switch and case:
Code switch(X) {
case 0:
Abort();
break;
case 1:
Load();
break;
case 2:
Start();
break;
default:
Abort();
break;
}
I guess I follow a rule system and I'm wondering if that's okay. My first rule is that switch and cases should be used when there is more than two else ifs and it makes sense to do so. Second rule: else ifs that are two or greater in number can be kept if they are different from only checking one value and are relevant to the code in the original if statement and makes logical sense to keep it that way so as not to break the cleanliness of the code or if the statement cannot be evaluated in a switch statement.
I'm not sure if I should be using these "rules" or whether they are applicable. Switch and case, as to my knowledge, was implemented for the purpose of making else ifs cleaner and make more sense. In my eyes they serve the purpose of "brute checking" a value when needed and can be a very clunky concept. I'm also aware that before C# 7.0 switch and case couldn't handle all evaluations hence where else if came in handy. I'd like a clear definition as to when one over the other should be used.
OT: Where did you learn to place the curly brackets like this? Are you coming from java? Please, this is C#...
Dec 1, 2016 12:14 PM
(This post was last modified: Dec 1, 2016 12:15 PM by video_error.)
(Dec 1, 2016 12:01 PM)abratar Wrote: OT: Where did you learn to place the curly brackets like this? Are you coming from java? Please, this is C#...
1. Brackets are a personal style. 2. I found it less wasteful that dumping the whole bracket on its own line. Can waste hundreds of lines of code. 3. Indeed I do come from Java. That being said I've seen many Java programmers use the newline style. I personally find it unpreferable. To each his own. Furthermore the compiler doesn't care and C# system classes even use the same line style last time I looked(Could be wrong). C# has nothing to do with it. But that wasn't even my question.
It's not just about being cleaner..
Switches require constants as evaluators and they might compile differently depending on what is being tested.
(Dec 1, 2016 12:27 PM)Owl Wrote: It's not just about being cleaner..
Switches require constants as evaluators and they might compile differently depending on what is being tested.
Ah thank you. I was trying to look for a fault in my reasoning, as I feared, I was dead wrong. I'll keep this in mind, thank you. 🤝
Posting here for "saving bookmarks"
#1:
http://stackoverflow.com/questions/44506...itch-speed
#2: (MUST READ / Bookmark)
http://stackoverflow.com/questions/39561...-case-in-c
TL;DR: "compiler optimize switch"
And for the { war 🤣
http://softwareengineering.stackexchange...g-brackets
Quote:Ritchie invented the C language, and Kernighan wrote the first tutorial, when computer displays only showed a few lines of text. In fact, UNICS (later UNIX) development started on a DEC PDP-7, which used a typewriter, printer and paper tape for a user interface. UNIX and C were finished on the PDP-11, with 24-line text terminals. So vertical space was indeed at a premium. We all have slightly better displays and higher resolution printers today, right? I mean, I don't know about you, but I have three (3) 24" 1080p displays in front of me right now. :-)
Also, so much of that little book The C Programming Language is code samples that putting the braces at the ends of the lines instead of on their own lines allegedly saved an appreciable amount of money on printing.
What is truly important is consistency throughout a project, or at least within a given source code file.
There are also scientific studies showing that the brace on its own line (indented to the same level as the code, in fact) improves code comprehension despite what people think they think of the aesthetics. It makes it very clear to the reader, visually and instinctively, which code runs in which context.
TL;DR: Conventions, Conventions everywhere
Peace 👍
Dec 1, 2016 01:36 PM
(This post was last modified: Dec 1, 2016 01:41 PM by video_error.)
(Dec 1, 2016 01:21 PM)Zilog80 Wrote: Posting here for "saving bookmarks"
#1:
http://stackoverflow.com/questions/44506...itch-speed
#2: (MUST READ / Bookmark)
http://stackoverflow.com/questions/39561...-case-in-c
TL;DR: "compiler optimize switch"
And for the { war 🤣
http://softwareengineering.stackexchange...g-brackets
TL;DR: Conventions, Conventions everywhere
Peace 👍
1# and 2#: Thanks a lot mate, wanted some input from HackForums as I thought it was merely about readability not performance. I didn't even think to look up the performance costs. Thanks mate! I'll be more careful next time. 🎉
3#: First off I definitely have respect for you and what you like, it's fine. :P I've never seen the blocking thing you C developers talk about. I always found it just as easy to see the blocking taking place with the same line. I'm pretty sure I already said this but I find it wasteful for my purposes. Only serves to confuse me. I'd also be curious to read the study mentioned in those articles. I'm fully aware it is a matter of opnion and this is mine :P I have changed for others, used their brace style for work even though I didn't like it. Didn't matter, he got to decide because he came first and I wasn't going to make him change for me. When it comes down to it, it is only a matter of preference and wasn't trying to attack anyone. I was merely trying to understand others' styles (like your own :P). Ninja ended up giving me negative rep for questioning, it appears for no other reason than he disagreed with me or i somhow cane acrosd as a dick(which was not my intention in the slightest). I understand now this is a rather touchy subject and most would rather not talk about it. I am not trying to flame in any way and could not make that more clear. 🤝
DO NOT DEAL WITH THIS MEMBER
(Dec 1, 2016 12:14 PM)video_error Wrote: 1. Brackets are a personal style. 2. I found it less wasteful that dumping the whole bracket on its own line. Can waste hundreds of lines of code. 3. Indeed I do come from Java. That being said I've seen many Java programmers use the newline style. I personally find it unpreferable. To each his own. Furthermore the compiler doesn't care and C# system classes even use the same line style last time I looked(Could be wrong). C# has nothing to do with it. But that wasn't even my question.
How many posts are you going to make bitching about fucking brackets.
Really.
IT IS ALL PREFERENCE. LET IT GO.
DO NOT DEAL WITH THIS MEMBER
I'm by no means an expert but my "general rule" is to use switch/case when the variables are known or limited to X number of possibilities and if/else if/else when the variables are potentially unknown or unlimited possibilities.
That's just how do it...
Well, the game is changing, soon enough half of the answers here will be obsolete because C# 7.0 is coming out, and it comes with new addition to switch statements.
DO NOT DEAL WITH THIS MEMBER
Dec 1, 2016 04:37 PM
(This post was last modified: Dec 1, 2016 04:39 PM by Gt403cyl.)
(Dec 1, 2016 04:34 PM)+Haze Wrote: Well, the game is changing, soon enough half of the answers here will be obsolete because C# 7.0 is coming out, and it comes with new addition to switch statements.
Yeah I saw this:
You can switch on any type (not just primitive types)
Patterns can be used in case clauses
Case clauses can have additional conditions on them
Tuples seem very interesting too!
(Dec 1, 2016 04:27 PM)Gt403cyl Wrote: I'm by no means an expert but my "general rule" is to use switch/case when the variables are known or limited to X number of possibilities and if/else if/else when the variables are potentially unknown or unlimited possibilities.
That's just how do it...
Hmm okay thanks for the input. I'll also keep that in mind as a rule.
(Dec 1, 2016 04:01 PM)Ninja Wrote: How many posts are you going to make bitching about fucking brackets.
Really.
IT IS ALL PREFERENCE. LET IT GO.
I never had anything to let go? I was genuinely curious as to why people used what they did. I'm not trying to "bitch" about it. I'm still a fairly humble programmer trying to learn what I can. I'm nothing too special and just want to learn from others, I gave my reasons and wondered about others'. That's literally all. Preference seems to be a big thing and so I wanted to make sure I take everything into consideration when thinking about styles and how to work better with others. I'm a genuinely curious person. I just wanted to know why people did what. I have literally no beef. And I suppose I'll drop it now that I got the helpful reasons I did. I'm quite glad for the help I got, it was very enlightening and learned some things. I was wondering if there's more to it than preference. I can now see otherwise. It seems to be solely preference. Sorry you took it the wrong way, mate. 😧
DO NOT DEAL WITH THIS MEMBER
(Dec 1, 2016 04:45 PM)video_error Wrote: I never had anything to let go? I was genuinely curious as to why people used what they did. I'm not trying to "bitch" about it. I'm still a fairly humble programmer trying to learn what I can. I'm nothing too special and just want to learn from others, I gave my reasons and wondered about others'. That's literally all. Preference seems to be a big thing and so I wanted to make sure I take everything into consideration when thinking about styles and how to work better with others. I'm a genuinely curious person. I just wanted to know why people did what. I have literally no beef. And I suppose I'll drop it now that I got the helpful reasons I did. I'm quite glad for the help I got, it was very enlightening and learned some things. I was wondering if there's more to it than preference. I can now see otherwise. It seems to be solely preference. Sorry you took it the wrong way, mate. 😧
If you plan to do team projects, you probably want to follow conventions as strictly as possible. C# is pretty much always written with brackets being on the next line.
There's really nothing to talk about. It's personal preference unless the language you're using has conventions regarding it. In that case, you better conform to conventions when you do a team project, unless you want to be an asshole.
(Dec 1, 2016 04:01 PM)Ninja Wrote: How many posts are you going to make bitching about fucking brackets.
Really.
IT IS ALL PREFERENCE. LET IT GO. 1. It's his own thread
2. He was questioned.
Stop being so butthurt. Brackets are the future of coding. Brackets are the future of our generation. Bracket placement is important to me and my family.
Bracket's belong inline, not on their own line like pussy's put them.
Je t'aime
(Apr 22, 2022 04:39 PM)-Scrub Wrote: Jokes on you, I'm not smart enough to know what he's talking about.
(Dec 1, 2016 05:20 PM)Nachfolger Wrote: 1. It's his own thread
2. He was questioned.
Stop being so butthurt. Brackets are the future of coding. Brackets are the future of our generation. Bracket placement is important to me and my family.
Bracket's belong inline, not on their own line like pussy's put them.
The reason he got so butthurt was because I already asked in another thread and he got pussy. He carried it over here for some reaaon. I was merely having a constructive conversation trying to be more open. So I he's not entirely talking about this thread. Also thanks mate, I also put mine on the same line. ✌ But I wouldn't refer to him as a "pussy" for having an opposing opnion lol. :P
(Dec 1, 2016 04:50 PM)clrlslwi Wrote: If you plan to do team projects, you probably want to follow conventions as strictly as possible. C# is pretty much always written with brackets being on the next line.
There's really nothing to talk about. It's personal preference unless the language you're using has conventions regarding it. In that case, you better conform to conventions when you do a team project, unless you want to be an asshole.
Yes, I quite understand but C# has no bracket convention as to my knowledge. I already work with another programmer and have conformed to his wishes quite happily. He a great friendly dude and he got there first so he called the shots. It's only fair.
|