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

    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
        • S scoy6

          "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 Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #70

          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.

          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 )

            J Offline
            J Offline
            James Lonero
            wrote on last edited by
            #71

            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.

            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
              Gerqi
              wrote on last edited by
              #72

              Then why is there competitions for 'clever code'? such as: http://softwarecontests.intel.com/threadingchallenge/

              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 )

                V Offline
                V Offline
                VSpike
                wrote on last edited by
                #73

                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!

                1 Reply Last reply
                0
                • R Rei Miyasaka

                  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)

                  C Offline
                  C Offline
                  Chris Kaiser
                  wrote on last edited by
                  #74

                  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.

                  1 Reply Last reply
                  0
                  • 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.

                    C Offline
                    C Offline
                    Chris Kaiser
                    wrote on last edited by
                    #75

                    Examples?!

                    This statement was never false.

                    1 Reply Last reply
                    0
                    • J Josh Smith

                      rastaVnuce wrote:

                      int newInt = myInts[index++]

                      That's not clever.  By "clever" I mean things like depending on some abstruse side-effect of a method call with a certain parameter value to make something work, etc.

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

                      C Offline
                      C Offline
                      Chris Kaiser
                      wrote on last edited by
                      #76

                      Ahhh... that's a bit different. That's: How to write unmaintainable code.[^]

                      This statement was never false.

                      1 Reply Last reply
                      0
                      • J Josh Smith

                        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 Offline
                        C Offline
                        Chris Kaiser
                        wrote on last edited by
                        #77

                        Not always. I think you are confusing clever with unmaintainable. If a method does more than it declares then its unmaintainable. If its compacting 5 lines of code into one, it might just be efficient. But, what's your take on the use of interfaces and dynamic plug-in type code which depends on a bit of cleverness?

                        This statement was never false.

                        1 Reply Last reply
                        0
                        • S scoy6

                          "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

                          C Offline
                          C Offline
                          Chris Kaiser
                          wrote on last edited by
                          #78

                          And 5! again!

                          This statement was never false.

                          1 Reply 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