What is Better Code in Your Opinion?
-
This:
//If the flag is on then if(READBIT(\*pGlobalFlags, uFlag)) { //Value is set to on bstrValue = "ON"; } else { //Value is set to off bstrValue = "OFF"; }
Or this:
bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
:confused:
Matt Gerrans
What happened to if(IsLounge) { Label1.Text="No Programming Questions"; } elseif { Label1.Text="Ask in appropriate forum"; }
A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)
-
This:
//If the flag is on then if(READBIT(\*pGlobalFlags, uFlag)) { //Value is set to on bstrValue = "ON"; } else { //Value is set to off bstrValue = "OFF"; }
Or this:
bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
:confused:
Matt Gerrans
You're all wrong. The correct answer depends on how you're getting paid - by the number of lines written(then the first), or by the functionality implemented (then the second). Yup, thats it;)
:badger:
-
What happened to if(IsLounge) { Label1.Text="No Programming Questions"; } elseif { Label1.Text="Ask in appropriate forum"; }
A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)
:P It's style question. People usually get away with it. IMO its equal to "Do you like code red or pitr-cola more?" kind of question.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
:P It's style question. People usually get away with it. IMO its equal to "Do you like code red or pitr-cola more?" kind of question.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
No, it is an aesthetics question! What programmer has style? ;)
Matt Gerrans
-
What happened to if(IsLounge) { Label1.Text="No Programming Questions"; } elseif { Label1.Text="Ask in appropriate forum"; }
A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)
That's just plain wrong (and I'm not just talking about the superfluous curlies) -- I already pointed out that this is not about programming, per se. I guess some people can't tell the difference between a question and a metaquestion. ;P
Matt Gerrans
-
I started to think about, but I have noticed what is a programming question in the Lounge, then my opinion goes to soapbox... Ok, ok I'm lazy about select another forum... Here goes: The first option is better. Why? The code is easy to maintain. If I wish add some task if is true (by example), I can do this:
//If the flag is on then
if(READBIT(*pGlobalFlags, uFlag))
{
//Value is set to on
bstrValue = "ON";
// another task here
MyAnotherTaskFunction();
}
else
{
//Value is set to off
bstrValue = "OFF";
}Notice what I just added 1 line of code, and in your another style I will need create the supressed if construct. Just my 2 cents Regards
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:
Personally, I would do it like this, kinda taking best of both options (IMO).
// getting string value from flag
strValue = (READBIT(*pGlobalFlags, uFlag))?"ON":"OFF";// do different stuff depending on value of strValue
if(strValue == "ON")
{
MyTaskFunction();
}
else
{
MyAnotherTaskFunction();
}
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
I started to think about, but I have noticed what is a programming question in the Lounge, then my opinion goes to soapbox... Ok, ok I'm lazy about select another forum... Here goes: The first option is better. Why? The code is easy to maintain. If I wish add some task if is true (by example), I can do this:
//If the flag is on then
if(READBIT(*pGlobalFlags, uFlag))
{
//Value is set to on
bstrValue = "ON";
// another task here
MyAnotherTaskFunction();
}
else
{
//Value is set to off
bstrValue = "OFF";
}Notice what I just added 1 line of code, and in your another style I will need create the supressed if construct. Just my 2 cents Regards
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:
Clickok wrote:
Notice what I just added 1 line of code, and in your another style I will need create the supressed if construct.
Yeah, but then you'll have your new line of code to focus on, and won't be as tempted to add useless comments to the code you're modifying...
-
This wasn't really a programming question per se, but a programming aesthetics question, otherwise I would not have posed it here. What you are suggesting there is a nasty form of premature optimization[^] or Premature Generalization[^]. Are you saying you should write the most verbose slop possible, so that it might be easier to insert more stuff into it later? YAGNI[^]!
Matt Gerrans
Matt Gerrans wrote:
This wasn't really a programming question per se, but a programming aesthetics question,
Just kidding with you ;)
Matt Gerrans wrote:
Are you saying you should write the most verbose slop possible
Not, I'm not saying this. I'm saying what I write code what...
Matt Gerrans wrote:
...might be easier to insert more stuff into it later
The comments what you added really are not necessary, because of the code is self-explanatory. But if I can code some snippet what later will be more easy to customize (like never hard-code strings, if possible declarate variables in beginning of procedures, etc), I will do. It's my habit. And really really turn the things more easy to me :)
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:
-
No, it is an aesthetics question! What programmer has style? ;)
Matt Gerrans
OK my bad. What about
bstrValue =
READBIT
(
*pGlobalFlags, uFlag
)
?
"ON"
:
"OFF";:rolleyes:
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
This:
//If the flag is on then if(READBIT(\*pGlobalFlags, uFlag)) { //Value is set to on bstrValue = "ON"; } else { //Value is set to off bstrValue = "OFF"; }
Or this:
bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
:confused:
Matt Gerrans
I generally go with the first if I have more than one statement per condition.
Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon
-
That's just plain wrong (and I'm not just talking about the superfluous curlies) -- I already pointed out that this is not about programming, per se. I guess some people can't tell the difference between a question and a metaquestion. ;P
Matt Gerrans
Matt Gerrans wrote:
some people can't tell the difference between a question and a metaquestion.
Ha! metaquestion is very good! Do you accept metareplies too? :laugh::laugh::laugh:
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:
-
That's just plain wrong (and I'm not just talking about the superfluous curlies) -- I already pointed out that this is not about programming, per se. I guess some people can't tell the difference between a question and a metaquestion. ;P
Matt Gerrans
Rules: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. Do not post programming questions (use the programming forums for that) and please don't post ads. So sorry, you get it, no.
A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)
-
What happened to if(IsLounge) { Label1.Text="No Programming Questions"; } elseif { Label1.Text="Ask in appropriate forum"; }
A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)
-
This:
//If the flag is on then if(READBIT(\*pGlobalFlags, uFlag)) { //Value is set to on bstrValue = "ON"; } else { //Value is set to off bstrValue = "OFF"; }
Or this:
bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
:confused:
Matt Gerrans
They are both the same. The first one has extraneous comments though. The code clearly says it's being set to "OFF or "ON"... why say the same thing in a comment?
"Quality Software since 1983!"
http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps! -
OK my bad. What about
bstrValue =
READBIT
(
*pGlobalFlags, uFlag
)
?
"ON"
:
"OFF";:rolleyes:
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
I've seen "production code" that looks a lot like that. X|
Matt Gerrans
-
Rules: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. Do not post programming questions (use the programming forums for that) and please don't post ads. So sorry, you get it, no.
A room without books is like a body without a soul. - Cicero (106 BC - 43 AD)
I'm not sure I understand your broken sentence rightly, but it sounds like you don't understand the difference between "a programming question" and "a question about programming practice." A programming question would be something like "how do you code the travelling salesman algorithm in Plain English?" * * and the answer would be a lot of dodging about without actually answering the question! :)
Matt Gerrans
-
This:
//If the flag is on then if(READBIT(\*pGlobalFlags, uFlag)) { //Value is set to on bstrValue = "ON"; } else { //Value is set to off bstrValue = "OFF"; }
Or this:
bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
:confused:
Matt Gerrans
For a simple condition like that the later. The only bad part of the first approach are the comments, though I would type the second if I was doing it myself, I wouldn't really care when reading it. I hope that is a CComBSTR or some wrapper like that, or the style isn't going to make much difference.
The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting. The pig rattle. Rattle with dove. The dove angry. The pig leave. The dove produce. Produce is chicken wing. With wing bark. No Quack. - Thedailywtf.com
-
I started to think about, but I have noticed what is a programming question in the Lounge, then my opinion goes to soapbox... Ok, ok I'm lazy about select another forum... Here goes: The first option is better. Why? The code is easy to maintain. If I wish add some task if is true (by example), I can do this:
//If the flag is on then
if(READBIT(*pGlobalFlags, uFlag))
{
//Value is set to on
bstrValue = "ON";
// another task here
MyAnotherTaskFunction();
}
else
{
//Value is set to off
bstrValue = "OFF";
}Notice what I just added 1 line of code, and in your another style I will need create the supressed if construct. Just my 2 cents Regards
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:
Well I think that's a valid point, and sort of why I also always put in the {} even around a single statement (and I think they should be required, and the IDE should insert them automatically, with the else as well). The opposite of this situation would when there are multiple statements and then all but one are removed, would you then remove the {}? Would you rearrange it to the ternary operator? However, I really only use the ternary operator when dealing with parameters (and returns):
System.Console.WriteLine ( "I want {0} piece{1} of candy." , x , x==1?"":"s" ) ; return ( x==-1?0:x ) ;
The main problem with the ternary operator is that (like macroes) some times things can get confused, I don't have an example in front of me, but at times you need to use "extra" parentheses:(int)x==1?y:z // will likely cause trouble; what does the cast apply to? ((int)x==1)?y:z // do you mean this? (int)(x==1?y:z) // or this?
-
Matt Gerrans wrote:
some people can't tell the difference between a question and a metaquestion.
Ha! metaquestion is very good! Do you accept metareplies too? :laugh::laugh::laugh:
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16) :badger:
Hmmm, I may have this backward.... Dicky Smothers: "I asked a rhetorical question." Tommy Smothers: "I gave a rhetorical answer."
-
This:
//If the flag is on then if(READBIT(\*pGlobalFlags, uFlag)) { //Value is set to on bstrValue = "ON"; } else { //Value is set to off bstrValue = "OFF"; }
Or this:
bstrValue = READBIT(*pGlobalFlags, uFlag) ? "ON" : "OFF";
:confused:
Matt Gerrans
I'd go for the if..else construct any time I can. I can't stand the ternary operator. Irrational I suppose, but there it is.
BW
If you're not part of the solution, you're part of the precipitate.
-- Steven Wright