Cleverness
-
Paul Conrad wrote:
Though it is tough to beat an optimizing compiler these days.
And hardware optimization.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
Can't be any of my code.... I am too lazy to write clever code.
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns
-
Yeah, there was a time when I wrote 'clever' C++ code, but I got over myself.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Christian Graus wrote:
I know that in my early days of STL I delighted in writing code that in hindsight was far more obscure than this, on the basis that I knew how. But, you quickly realise that readable counts for more than clever, it's part of being a team player instead of trying to show off, IMO.
Not only that, 9/10 times the code will compile to exactly the same assembly, or at least the performance won't be much different. So ... if you aren't coding in assembly, why make your code look like that junk ;P I realize the CPU has a 'add contents pointed to by this register, to the contents pointed to by that register and store it here and add 5 to it while your not busy', but do I really need to read that on each line? My favorite was during a job interview I was asked on the fly to code a program that would get the count of all values of an 8 bit integer, for a large set (several million). I wasn't thinking so I originally had a STL list, and a one dimensional array the size of the original input. Then after a couple seconds thought it became: int count[255]; int i; for (i = 0; i < 255; i++) count[i] = 0; for (i = 0; i < list.size; i++) count[list[i].Value]++; for (i = 0; i < 255; i++) cout << count[i]; Quick and dirty, and only n + 2*255 interations :) I thought it was beautiful using the value of the data to offset the array, and then increment it all on one line, hehe. I think I curled their eyebrows, I don't think they had thought of that and thought their would be more code required.
In your example, when list[i].Value == 255 your code will take an index out of range exception. Although your count array is 255 items in size it's index range is zero to 254. You can use 256 as the size of the count array, and change the for loop comparand instructions from i < 255 to i < 256 and then your code will not cause any exceptions when the list[i].Value == 255. Also, you may want to consider creating a local variable like this "int SizeOfList = list.size" just before your second for loop instruction and changing the comparand instruction from "i < list.size" to "i < SizeOfList". This way your loop won't cause the system to push the stack and branch to the list.size property during each iteration of the loop. Overall, I enjoyed reading your post.
-
In your example, when list[i].Value == 255 your code will take an index out of range exception. Although your count array is 255 items in size it's index range is zero to 254. You can use 256 as the size of the count array, and change the for loop comparand instructions from i < 255 to i < 256 and then your code will not cause any exceptions when the list[i].Value == 255. Also, you may want to consider creating a local variable like this "int SizeOfList = list.size" just before your second for loop instruction and changing the comparand instruction from "i < list.size" to "i < SizeOfList". This way your loop won't cause the system to push the stack and branch to the list.size property during each iteration of the loop. Overall, I enjoyed reading your post.
-
are you sure !s is the same as s != NULL ?
Wow. I admit I was going to post explanation (did I mention I am bored?) why it is so, and then it struck me! Of course it's the opposite. :)
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
are you sure !s is the same as s != NULL ?
:doh: (Maybe that proves the point?)
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
Hmmm... OOP is pretty clever; all the polymorphism and such. :)
-
He means if you can see a way to make 5 lines work in one line of code, a simple example is int n = myInts[index++]; that's not *terribly* clever, but as a rough off the top of my head example, incrementing the index in the next line of code makes clear that you want it to increment after the operation. I know that in my early days of STL I delighted in writing code that in hindsight was far more obscure than this, on the basis that I knew how. But, you quickly realise that readable counts for more than clever, it's part of being a team player instead of trying to show off, IMO.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
int n = myInts[index++]; For an assignment to a new variable, I'd consider this a questionable practice, UNLESS it was in the following context: index = 0 while (index < indexLimit) { int n = myInts[index++]; // do something with n // ... } Many moons ago we (at that time) had a job candidate who rated himself a 9 out of 10 in terms of his C skills. During the interview, it turned out he never used the for (init;cond;incr) {} construct; instead, whenever he encountered one, he converted it into either a while {} or a do {} while. Naturally, we rejected him - dumbing down others' code to your own standards is presumptuous and dangerous. Some of the most elegant and readable code I've ever seen used such shortcuts and side effects. Some of the worst code I've ever dealt with avoided all such. C/C++ is a dangerous language family, and those uncomfortable with its conventions are well advised to seek employment using Visual Basic, Java, or other, "safer" environments. I don't mean that all code should be written as if it were an entry in the Obscure C competition; I do mean that if the expression I cite above gives you a headache, you should consider another development environment.
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
I totally agree. Do programmers actually intentionally write code that's difficult for others to understand? What amazing, colossal egos! In my mind, being clever is quickly designing and coding a solution that actually works! And if after a few months the code a) Still works; b) I can understand what I did and why; then I was clever and smart.
-
I totally agree. Do programmers actually intentionally write code that's difficult for others to understand? What amazing, colossal egos! In my mind, being clever is quickly designing and coding a solution that actually works! And if after a few months the code a) Still works; b) I can understand what I did and why; then I was clever and smart.
Old Ed wrote:
Do programmers actually intentionally write code that's difficult for others to understand? What amazing, colossal egos!
Yep, egos plays into it. Also job security. "If only I can decipher this code, then they can't replace me." I think that's the mentality. Of course, any decent shop would let such a person go asap! :)
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
Fortunately, my early reading materials included "The C Puzzle Book - Puzzles for the C Programming Language" by Alan R. Feuer (Bell Labs). Many brain cramps occurred whilst studying that one! 74 pages of puzzles. Example from page 3: What does the following print?
int x; x = - 3 * 4 % - 6 / 5; printf("%d\n", x);
Gary
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
I used to use/maintain/extend an inhouse set of data acq and math libs. The @#@# who'd written them was too lazy to type meaningful variables names and in general liked compact code. The end result was tens of thousands of lines of dense, nearly indecipherable code wherein almost all the variable and function names were one or a few characters (he often started at a and went through z, then A through Z, etc), function calls with side effects nested N deep, etc, something like: z = a(bB((c ? f : gg)) * Z(c + x); /* Note the embedded side effects */ It was a mess. The guy had been promoted because the libs *did* work well, but they caused me one bad review because it took me a month or more to even begin to understand that mess of obscure code. I finally transferred out of that group, and it was only after the second person to take over that role nearly failed that management started listening to the hapless victim, by then months late and way over budget. Morons. Why is it that so many managers reward naked self-promotion and ambition, whilst often punishing the very people who are saving their bacon??? I think a big part of this is left-over from the days when C and later C++ compilers were not much more than thin layers over assemblers, and tricky programmming tricks could equate to significant perf gains. Those days are long gone and optimizers are in general far "smarter" than most programmers ever will be. There are things to avoid that trip up the optimizers, but most of those are structural and have little to do with using cute, complex, obscure, or fancy statement constructs.
-
Christian Graus wrote:
int n = myInts[index++];
Unfortunately, that sort of statement is in a lot of C and C++ code. I dislike embedded expressions that have side-effects. It makes the code unclear and difficult to debug.
The above statement is easily parsed IN THIS CONTEXT, but if it is buried in some bit of an algorithm it can easily be missed. Also, there is a sort of elegance in simple code that cannot be duplicated by any amount of cleverness. After 24 years of this stuff I have a real appreciation for any kind of elegance and clarity. Simple code is the savior of the maintenance folks.
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
-
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -- Brian Kernighan
sKoi wrote:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -- Brian Kernighan
5!
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
-
Yeah, there was a time when I wrote 'clever' C++ code, but I got over myself.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Oh yes, like the old C days. Cleverness was the hallmark of a good programmer. (Now its a bunch of crap.) I was one of those who liked making long macros that allowed me to a whole lot in one line. Sometimes, those macros got rather convoluted. Six months later, I couldn' remember what I was doing and cursed myself. Many cases, I would right code to spare using extra variables, often reusing the few I did declare. My functions were concise, but difficult to understand. Back in college, this was preferred, since the instructor didn't want to spend time looking at long listing. And, it was "elegant". Of course, now I prefer using as many variable that I need to describe the operations that I am doing. Same with function names. I get very verbose, sometimes with names as long as 25 characters. But it is descriptive. But not like TranslateOneTo...FortySeven(). Live and learn. Some one else or you may at a later date find yourself figuring out some old code that hasn't been modified in years. So, comment and document your code and design well.
-
I abhor reading code which is intentionally clever. Clever code which functions properly is garbage. If you're smart enough to write clever code which works, stop being a jerk and write smart code which works. Please. Clever code requires me to spend time figuring out how some pompous smart person thinks. Smart code allows me to just read it and move on. Agreed?
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
Then why is there competitions for 'clever code'? such as: http://softwarecontests.intel.com/threadingchallenge/
-
He means if you can see a way to make 5 lines work in one line of code, a simple example is int n = myInts[index++]; that's not *terribly* clever, but as a rough off the top of my head example, incrementing the index in the next line of code makes clear that you want it to increment after the operation. I know that in my early days of STL I delighted in writing code that in hindsight was far more obscure than this, on the basis that I knew how. But, you quickly realise that readable counts for more than clever, it's part of being a team player instead of trying to show off, IMO.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
We used to have a project where if you logged in as a developer you got different tips of the day, which were a collection of found stuff and unix fortune gems. One that is relevant here is:- "Debugging is at least twice as hard as programming. If your code is as clever as you can possibly make it, then by definition you're not smart enough to debug it." - Brian Kernighan When I started that job I was into clever code too, but the guy I was working with had been programming a long time and soon taught me that readability and maintainability is (almost) all. As he said "'Clever' in coding terms is usual a derogatory term". He also used to say that "premature optimisation is the root of much evil" which is another lesson I needed to learn as a rookie, to avoid me spending time and adding complexity trying to pointlessly optimise some non-essential bit of code. All rookie programmers should spend some time working with a good, experienced mentor IMO. It may be painful to the ego at the time but it's good for you in the long term!
-
Amen. "Everything should be made as simple as possible, but not simpler." -- A. Einstein And now, allow me to be a jerk:
#define Swap(x, y) (x^=y^=x^=y)
That's actually illegal C/C++ as you aren't allowed to assign to a variable in more than one sequence point. But compiler's support it as its used so heavily.
This statement was never false.