Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Cleverness

Cleverness

Scheduled Pinned Locked Moved The Lounge
csharpphpwpfcomquestion
78 Posts 48 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Josh Smith

    Stuart Dootson wrote:

    Ummmm - define 'clever code' - what your message says to me is that you don't believe you should have to think when reading code - I can't agree with that.

    Clever code is unabashed geeky-ness.  Code which is written for the sake of proving how smart one is, or how knowledgeable one is of abstruse features of a language.  I certainly agree that reading code requires thought, but something which can be simple shouldn't require me to sit there and "figure it out." They don't pay us to prove how smart we are by creating little puzzles, they pay us to create working software on time.

    :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

    S Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #50

    I guess I just have an issue with blanket rejection of 'clever' code, having written some for a specific reason (performance related, in a resource-limited embedded system) and having it rejected because it was 'clever' and replaced with code that was 2 or 3 times slower. In most desktop situations, I would agree with you - clever code isn't desirable or should be stuck in a library so you don't see the implementation, just the API (e.g. the MS implementations of 'strcpy' and 'strcat' in hte C runtime). Trouble is, 'clever' does have many and varying definitions - for example, I wouldn't call use of the C++ standard library collections and algorithms 'clever', but I know people who would.

    1 Reply Last reply
    0
    • J Josh Smith

      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.

      K Offline
      K Offline
      k bl
      wrote on last edited by
      #51

      What about clerverly(:)) simple code? For instance, with the jQuery toolkit for JavaScript there is an array handler called "each" used like this: $(arrayi).each(function(i){ document.write(i) }); I don't normally dig the "clever code", but I love when it is tastefully simple.

      ASP.Net meets jQuery... Imagine the possibilities.

      1 Reply Last reply
      0
      • B Brady Kelly

        El Corazon wrote:

        try imagining a 4 dimensional unit vector

        I read somewhere once, I think in a description of multi-dimensional mathematical stuff, that understanding and applying it requires one to not try and imagine real world examples, of e.g. a tesseract, but to simply accept them as abstract entities and use them.

        MY BLOG

        E Offline
        E Offline
        El Corazon
        wrote on last edited by
        #52

        Brady Kelly wrote:

        I read somewhere once, I think in a description of multi-dimensional mathematical stuff, that understanding and applying it requires one to not try and imagine real world examples, of e.g. a tesseract, but to simply accept them as abstract entities and use them.

        very true, and I try very hard to explain that. Some people can, some people can't just "accept" something as working and use it. In my business change is slow moving, and I push for far too many changes, so I am accused of cleverness, to outright dangerous volumes of change. :) but then, they were still drawing stick figures and line graphs when I came here 15+ years go. :laugh:

        _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

        1 Reply Last reply
        0
        • C Christian Graus

          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 )

          D Offline
          D Offline
          deltalmg
          wrote on last edited by
          #53

          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.

          A 1 Reply Last reply
          0
          • C Christian Graus

            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 )

            G Offline
            G Offline
            Gogza
            wrote on last edited by
            #54

            In that case use VB! ;) Instantally more readable and accessable! gogza

            1 Reply Last reply
            0
            • D Dario Solera

              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

              G Offline
              G Offline
              Gogza
              wrote on last edited by
              #55

              And quad-core processors! gogza

              1 Reply Last reply
              0
              • J Josh Smith

                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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #56

                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

                1 Reply Last reply
                0
                • C Christian Graus

                  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 )

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #57

                  Christian Graus wrote:

                  Yeah, there was a time when I wrote 'clever' C++ code, but I got over myself.

                  It's ok, we've all done it. '*' used to be my favourite keyboard character.:)

                  1 Reply Last reply
                  0
                  • D deltalmg

                    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.

                    A Offline
                    A Offline
                    AKAJamie
                    wrote on last edited by
                    #58

                    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.

                    D 1 Reply Last reply
                    0
                    • A AKAJamie

                      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.

                      D Offline
                      D Offline
                      deltalmg
                      wrote on last edited by
                      #59

                      Yeah, stupid mind fart with the 255 size array, hey I've been coding in VB for a while :) I love that string indexing in .Net (at least for VB) is 0 based, but InStr is 1 based. Makes life fun :)

                      1 Reply Last reply
                      0
                      • R Robert Surtees

                        are you sure !s is the same as s != NULL ?

                        D Offline
                        D Offline
                        DavidNohejl
                        wrote on last edited by
                        #60

                        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

                        1 Reply Last reply
                        0
                        • R Robert Surtees

                          are you sure !s is the same as s != NULL ?

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #61

                          :doh: (Maybe that proves the point?)

                          1 Reply Last reply
                          0
                          • J Josh Smith

                            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.

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #62

                            Hmmm... OOP is pretty clever; all the polymorphism and such. :)

                            1 Reply Last reply
                            0
                            • C Christian Graus

                              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 )

                              C Offline
                              C Offline
                              cpkilekofp
                              wrote on last edited by
                              #63

                              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.

                              1 Reply Last reply
                              0
                              • J Josh Smith

                                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.

                                O Offline
                                O Offline
                                Old Ed
                                wrote on last edited by
                                #64

                                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.

                                J 1 Reply Last reply
                                0
                                • O Old Ed

                                  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.

                                  J Offline
                                  J Offline
                                  Josh Smith
                                  wrote on last edited by
                                  #65

                                  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.

                                  C 1 Reply Last reply
                                  0
                                  • J Josh Smith

                                    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.

                                    G Offline
                                    G Offline
                                    ghle
                                    wrote on last edited by
                                    #66

                                    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

                                    1 Reply Last reply
                                    0
                                    • J Josh Smith

                                      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.

                                      E Offline
                                      E Offline
                                      el delo
                                      wrote on last edited by
                                      #67

                                      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.

                                      1 Reply Last reply
                                      0
                                      • L Leslie Sanford

                                        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.

                                        K Offline
                                        K Offline
                                        Keven M
                                        wrote on last edited by
                                        #68

                                        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.

                                        1 Reply Last reply
                                        0
                                        • J Josh Smith

                                          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.

                                          S Offline
                                          S Offline
                                          scoy6
                                          wrote on last edited by
                                          #69

                                          "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

                                          J C 2 Replies Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups