|
charlieg wrote: if (bVariable = someothervar)
That's a very ugly one.
I had to try it in JS. It works, always runs.
C# at least gives you an error "cannot implicitly convert 'int' to 'bool'
|
|
|
|
|
That's nothing.
I had this once in JS
if(functionName) //that's a reference, not a call!
{
//Do something
} else
{
//Some other equally poor code
}
It came up at pull request that as it's a function reference, it will always be true, and thus got rejected.
Anyway, the bloke went mental, telling me I couldn't reject it as I hadn't run the code to see if it worked....
He started randomly rejecting my pull requests after that.
|
|
|
|
|
no, it won't always be true. Consider:
var functionName=null;
if (functionName) {
alert('true')
} else {
alert('false')
}
functionName = function() {
}
if (functionName) {
alert('true')
} else {
alert('false')
} There are plenty of use cases where testing a function for existence is perfectly valid. I guess not in that particular case though.
|
|
|
|
|
Fair point!
In this case, it was just poor
|
|
|
|
|
boarderstu wrote: if(functionName)
If (!undefined) { // do some !undefined stuff.}
Very ugly.
It's almost like a preprocessing directive.
#ifndef DEBUG
|
|
|
|
|
charlieg wrote: "if (bVariable = someothervar) I know it's kool and all and allows for compact code, but really this has been the source of so many EFFING bugs, this construct should be disallowed in any language. Curse the inventor of "C" for adding it the language.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
charlieg wrote:
despise assignments in conditionals and that was a simple example.
Yes yes yes.
Also scoped variables are cool but... they still end up on the stack. There is really no need to use them, apart from making the code more difficult to read and to maintain.
And: kids, always initialize your variables. If you are absolutely, positively sure that the variable in question will never be read before being written on and that the data written on it are always meaningful then at least state it in a comment.
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
den2k88 wrote: scoped variables ... apart from making the code more difficult to read and to maintain. I actually find using scoped variables makes code easier to read and maintain. YMMV. And it has fixed and avoided many a bug for me.den2k88 wrote: always initialize your variables Indeed. Yes!
den2k88 wrote: absolutely, positively sure That works until it doesn't. See first rule: initialize!
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
TheGreatAndPowerfulOz wrote: I actually find using scoped variables makes code easier to read and maintain. YMMV. And it has fixed and avoided many a bug for me.
If the code is well written, I concur. When they are used in 300-500 lines jack-of-all-trades functions with dozens of scopes inside and badly named variables on the other hand...
TheGreatAndPowerfulOz wrote: That works until it doesn't. See first rule: initialize!
My experience exactly
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
Scoped vars make so much sense!
They stop sh*tty devs just using those variables and other functions polluting them
|
|
|
|
|
Just how a scoped variable can be polluted by another function? I'm not talking about local variables vs global ones - that would be silly - but of variables declared in nested blocks, as
if (condition){
int xyz;
int abc;
}
abc and xyz will exist only inside the if block and then disappear again. Is that bad? No, if the code is well written (short clear functions) and those variables are positively needed only in that condition statement.
If the code is long and complex jous just end up in a screen of code where there are two "things" named abc and xyz that weren't used anywhere else... it's confusing, to say the least.
Also there is no real benefit since all those variables are stack based so no allocation ever takes place - no need to "save those bytes on the stack" or "save the allocation time".
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
|
The original dev likely thought they were doing cool and advanced stuff with the for statement. This kind of code works, but is the reason that I chose my signature line.
Just because the code works, it doesn't mean that it is good code.
|
|
|
|
|
To borrow from a common aphorism: One man's garbage code is another mans treasure.
and here's another: Good code is in the eye of the beholder.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
In personal code, I begin my object variable names with obj. This allows all the objects I instantiate to be easily located with autocomplete. Plus when I am scanning through my code, its easily recognized as a variable I created. I usually do not do this in shared code as this is one of those things will annoy some developers.
|
|
|
|
|
Stryder_1 wrote: when I am scanning through my code, its easily recognized as a variable I created
Yeah, I used Hungarian notation in the early days of C++ and no intellisense, etc.
It's just that I'm not sure the obj prefix helped in this case (example I provided) because almost everything in that code was an object.
|
|
|
|
|
Why not prefix all your variable, function and class names with stryder_ ? That way everyone would know!
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Is that from a language that hasn't discovered the foreach construct yet?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yeah, I hate Hungarian notation, mostly because everyone, and I do mean everyone, missed the point. Hungarian notation was supposed to indicated the kind of thing it was, not it's type. That is it should say Customers , not objCustList nor objCustDict . Really, you don't need to tell me it's an int or number: nCount or iCount and you don't need to tell me it's an object or a list.
Named properly I can tell what a variable is.
As for the looping, personally don't see the problem with either approach. Whatever floats your boat. It's equivalent code. I like the for approach because I can immediately see what the loop dynamics are. The problem there is when, for some logical reason, you don't want to "moveNext() ," in that case a while statement may serve better.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
The only hungarian notation I really like is sticking a little "p" in front of a variable to indicate it's a pointer. Just makes it easier to understand what I am doing. Microsoft loves to go wonkers with it (UINT32, and yes I know that's a type), and I have seen some code (due to coding standards) look like pStrConst_Variablename.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Personally my favorite is :
LPSTR lpszName
Thos were the good old days* when there were no Internet to look that stuff up.
All you had was The Petzold and windows.h.
*Not really all that great for learning to program.
|
|
|
|
|
First time I saw something like thins in a C++ code base, I thought "That's clever!" It was only the second thought that I remembered how much I bloody hate maintaining such "clever" solutions.
We coders tend to be attracted to logical puzzles. It takes willpower to steer away from something clever in favor of something simple.
Either that or enough experience to know how much of a PITA clever code can be.
|
|
|
|
|
|
That's a very good article. I always like analogies because they help so much.
And this :
char* dest, src;
is a great example of code that once you know, you know, but after not seeing it for years kind of makes you pause and think, "wait, is src a char* too or just a char?
That's exactly like the weird for loop I was displaying.
It just makes you think extra for no reason.
And I liked Hungarian for years. I still do, but I'm kind of one the fence about it. You can tell what the types are, even if there is not intellisense. The point of the obj thing was that it wasn't helpful because I can tell you're calling a method on the thing so I can tell it's an object anyways. I prefixes like n and i did help so I didn't have to always back up the code and look to see what the thing was.
Then on the other hand, go ahead and name vars so I can tell that it will contain numeric values versus strings, etc.
modified 7-Nov-18 10:06am.
|
|
|
|
|
You're right - he totally should have used a while loop
while (objSourceSubFolders.moveNext())
{
if (objSourceSubFolders.atEnd())
goto Ended;
}
Ended:
cheers
Chris Maunder
|
|
|
|