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. Other Discussions
  3. The Weird and The Wonderful
  4. Is this a coding horror?

Is this a coding horror?

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialquestionlearning
46 Posts 27 Posters 101 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.
  • B BloodyBaron

    If people maintaining this code are used to the "?" operator, then this code is quite readable, and nearly simple. But if they're not, then they'll find it a coding horror ;P

    G Offline
    G Offline
    GibbleCH
    wrote on last edited by
    #28

    A developer should know the language they work with

    B B R 3 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      Firstly, no, Visual Studio on a 22" monitor, so I don't have that excuse. Secondly, if the two do not optimize to the same code, then someone as Microsoft should be up against the wall. Thirdly, I think the VS compiler will allow single step only on the second version. Finally, no, I did it because it seemed reasonable at the time, after I had removed a pile of code (which is why it has a dumb name - it didn't for long and got deleted completely soon afterwards) When I realized what I had left myself, the initial response was "Yeuch"! So I stuck it here. It realized some interesting responses - from my point of view it is pretty nasty, and not something I would want to leave in production code. Interesting that some people seem to think it is fine, if it is commented!

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

      K Offline
      K Offline
      Kenneth Kasajian
      wrote on last edited by
      #29

      So why do you think some people think it's okay? I've heard programmers tell me that swapping the value of x and y without using a temporary intermediate is somehow better, coding ending up looking something like this: *x^=*y^=*x^=*y eeek

      ken@kasajian.com / www.kasajian.com

      OriginalGriffO B 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        I don't know - it isn't in my code anymore... I wrote this wonder this morning, and realized I could prune it down to just the one line:

        private void SetRole(SMUser user, UserRole userRole, string roleName, List<string> inRoles, List<string> outRoles)
        {
        (((user.Roles & userRole) != 0) ? inRoles : outRoles).Add(roleName);
        }

        I mean, yes it works. But... It's not there anymore: I changed the whole way I handle roles for other reasons. But still, It's a bit impenetrable...

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

        T Offline
        T Offline
        Tech Code Freak
        wrote on last edited by
        #30

        Your code is not a horror in any ways! Instead, its a clever code and must be upvoted as I have given 5up for that!

        1 Reply Last reply
        0
        • K Kenneth Kasajian

          So why do you think some people think it's okay? I've heard programmers tell me that swapping the value of x and y without using a temporary intermediate is somehow better, coding ending up looking something like this: *x^=*y^=*x^=*y eeek

          ken@kasajian.com / www.kasajian.com

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #31

          Kenneth Kasajian wrote:

          So why do you think some people think it's okay?

          http://www.codeproject.com/Messages/4008174/Re-Is-this-a-coding-horror.aspx[^]

          Kenneth Kasajian wrote:

          *x^=*y^=*x^=*y

          In embedded assembler, when you are after every last clock cycle in a limited uProcessor, then swap by XOR can be a real time saver - since it only uses the ALU, there is less external memory access, which can save a lot of time. I don't like to work that close to the wind though - and if I do, it get commented to hell and back. In C++ or C#? Don't go there! :laugh:

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • G GibbleCH

            A developer should know the language they work with

            B Offline
            B Offline
            BloodyBaron
            wrote on last edited by
            #32

            Definitely agree with you. But at work, I'm always surprised to see the number of junior developers who don't understand (or don't want to try to understand) this simple construct.. (the "?" operator, I mean)

            G 1 Reply Last reply
            0
            • K Kenneth Kasajian

              So why do you think some people think it's okay? I've heard programmers tell me that swapping the value of x and y without using a temporary intermediate is somehow better, coding ending up looking something like this: *x^=*y^=*x^=*y eeek

              ken@kasajian.com / www.kasajian.com

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #33

              That's brilliant. Although in the spirit of this thread, it should surely be commented:

              *x^=*y^=*x^=*y; // in place swap

              1 Reply Last reply
              0
              • I IAbstract

                (((user.Roles & userRole) != 0) ? inRoles : outRoles)

                ...this is the part that needs some 'splaining. Maybe something as simple as:

                var rolesList = (user.Roles & userRole) != 0) ? inRoles : outRoles;
                rolesList.Add(roleName);

                S Offline
                S Offline
                Schmuli
                wrote on last edited by
                #34

                This is what I was thinking as well, which is why I looked at all the responses. The only I would change, in .NET 4, is to use the HasFlags method of enum:

                var rolesList = user.Roles.HasFlag(userRole) ? inRoles : outRoles;
                rolesList.Add(roleName);

                This also means no brackets are necessary.

                I 1 Reply Last reply
                0
                • G GibbleCH

                  // Volume by ideal gas law at STP (25C, 1 atm)
                  surfaceVolume = volume * 298 * p / (T * 101325);

                  Or even better...put that formula in a well named function and call it, so your call looks something like

                  surfaceVolume = CalculateVolumeByIdealGasLaw(volume, pressure, temperature);

                  Now you don't need the comment. And your code is easier to test.

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #35

                  Up to a point. Code which contains nothing but several levels of 'well named functions' wrapped around a simple arithmetic calculation are a pain to debug (trying to actually find the places where calculations are done and therefore being able to see whether they're done right). It's kind of like trying to read a book by just looking at the contents page. It's hard to read such long names, too (unless you put in underscores to make it look like a sentence but then the arguments seem to be bound to the last word). This would go in a function for me if I were using it in more than one place (which in this example I almost certainly would be). Your proposed name misses the most important point, by the way. It should be called "STPVolume" or "GetVolumeAtSTP" or something like that.

                  G 1 Reply Last reply
                  0
                  • G GibbleCH

                    A developer should know the language they work with

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #36

                    There seems to be a common opinion that the ternary is pure evil and should never be used, even as far more recent and difficult to understand concepts (anonymous delegates, lambdas, LINQ etc) are accepted. I don't understand why, it's a simple and elegant construction and easy to parse (for a person as well as a computer). I guess it's because it can be horribly abused (see some other posts in this forum), but so can pretty much every language feature.

                    1 Reply Last reply
                    0
                    • S Schmuli

                      This is what I was thinking as well, which is why I looked at all the responses. The only I would change, in .NET 4, is to use the HasFlags method of enum:

                      var rolesList = user.Roles.HasFlag(userRole) ? inRoles : outRoles;
                      rolesList.Add(roleName);

                      This also means no brackets are necessary.

                      I Offline
                      I Offline
                      IAbstract
                      wrote on last edited by
                      #37

                      Good catch ...I was actually unaware of the HasFlag method ...nice!

                      1 Reply Last reply
                      0
                      • B BobJanova

                        Up to a point. Code which contains nothing but several levels of 'well named functions' wrapped around a simple arithmetic calculation are a pain to debug (trying to actually find the places where calculations are done and therefore being able to see whether they're done right). It's kind of like trying to read a book by just looking at the contents page. It's hard to read such long names, too (unless you put in underscores to make it look like a sentence but then the arguments seem to be bound to the last word). This would go in a function for me if I were using it in more than one place (which in this example I almost certainly would be). Your proposed name misses the most important point, by the way. It should be called "STPVolume" or "GetVolumeAtSTP" or something like that.

                        G Offline
                        G Offline
                        GibbleCH
                        wrote on last edited by
                        #38

                        You think debugging is more difficult with more functions? I disagree. My IDE steps into functions, or over them...which makes debugging simpler, not harder. I can step over functions I've already eliminated as the problem, and into those which could be an issue. Rather than stepping on every line of code. And I should know if the bug is in a function or not by writing tests for it. My "STPVolume" or "GetVolumeAtSTP" function should have test methods ensuring it's accurate. As for the name, physics isn't my domain, so I didn't know the proper name, I made my best guess. The essence of my point still stands.

                        B 1 Reply Last reply
                        0
                        • B BloodyBaron

                          Definitely agree with you. But at work, I'm always surprised to see the number of junior developers who don't understand (or don't want to try to understand) this simple construct.. (the "?" operator, I mean)

                          G Offline
                          G Offline
                          GibbleCH
                          wrote on last edited by
                          #39

                          It's sad how many devs don't know about the null coalesce operator either Which reminds me, I have to go teach a fellow "dev" how asynchronous programming works...

                          1 Reply Last reply
                          0
                          • G GibbleCH

                            You think debugging is more difficult with more functions? I disagree. My IDE steps into functions, or over them...which makes debugging simpler, not harder. I can step over functions I've already eliminated as the problem, and into those which could be an issue. Rather than stepping on every line of code. And I should know if the bug is in a function or not by writing tests for it. My "STPVolume" or "GetVolumeAtSTP" function should have test methods ensuring it's accurate. As for the name, physics isn't my domain, so I didn't know the proper name, I made my best guess. The essence of my point still stands.

                            B Offline
                            B Offline
                            BobJanova
                            wrote on last edited by
                            #40

                            Yes. IDE stepping is the last resort of debugging, one should be able to find the suspect piece of code quickly without diving through a big tree of methods. (I'm suffering from this at the moment in a real project, in fact.) Obviously, you don't just want the whole application in one big main(), but taking something which is only one or two lines out of line does not (imo) help readability. (Taking it out to avoid duplicating the calculation is a separate matter and a good reason to do so, because the pain of having duplicate code massively outweighs the problem of having too many functions.) Your tests will tell you if the function is 'accurate', but it won't tell you some things which may be important when you want to use it (that you didn't think of at the time). For example, just what is standard temperature and pressure? ... there are different definitions and to find which one is being used you need to find the calculation. This is more common with more complex methods, or order of event firing or other slightly tricky things like that.

                            G 1 Reply Last reply
                            0
                            • B BobJanova

                              Yes. IDE stepping is the last resort of debugging, one should be able to find the suspect piece of code quickly without diving through a big tree of methods. (I'm suffering from this at the moment in a real project, in fact.) Obviously, you don't just want the whole application in one big main(), but taking something which is only one or two lines out of line does not (imo) help readability. (Taking it out to avoid duplicating the calculation is a separate matter and a good reason to do so, because the pain of having duplicate code massively outweighs the problem of having too many functions.) Your tests will tell you if the function is 'accurate', but it won't tell you some things which may be important when you want to use it (that you didn't think of at the time). For example, just what is standard temperature and pressure? ... there are different definitions and to find which one is being used you need to find the calculation. This is more common with more complex methods, or order of event firing or other slightly tricky things like that.

                              G Offline
                              G Offline
                              GibbleCH
                              wrote on last edited by
                              #41

                              I've never heard of "the problem of having too many functions"... Have you read books like Clean Code or Code Complete, etc?

                              B 1 Reply Last reply
                              0
                              • G GibbleCH

                                I've never heard of "the problem of having too many functions"... Have you read books like Clean Code or Code Complete, etc?

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #42

                                No ... I'd like to get hold of Clean Code, I've heard good things about it. Like all philosophies, though, the idea of making things 'cleaner' by putting things into subfunctions can be taken too far.

                                1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  I don't know - it isn't in my code anymore... I wrote this wonder this morning, and realized I could prune it down to just the one line:

                                  private void SetRole(SMUser user, UserRole userRole, string roleName, List<string> inRoles, List<string> outRoles)
                                  {
                                  (((user.Roles & userRole) != 0) ? inRoles : outRoles).Add(roleName);
                                  }

                                  I mean, yes it works. But... It's not there anymore: I changed the whole way I handle roles for other reasons. But still, It's a bit impenetrable...

                                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                  T Offline
                                  T Offline
                                  Tom Chantler
                                  wrote on last edited by
                                  #43

                                  It looks perfectly simple to me. I'd be very disappointed if a programmer couldn't understand it!

                                  1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    I don't know - it isn't in my code anymore... I wrote this wonder this morning, and realized I could prune it down to just the one line:

                                    private void SetRole(SMUser user, UserRole userRole, string roleName, List<string> inRoles, List<string> outRoles)
                                    {
                                    (((user.Roles & userRole) != 0) ? inRoles : outRoles).Add(roleName);
                                    }

                                    I mean, yes it works. But... It's not there anymore: I changed the whole way I handle roles for other reasons. But still, It's a bit impenetrable...

                                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                    T Offline
                                    T Offline
                                    thoiness
                                    wrote on last edited by
                                    #44

                                    hmm... The only thing I'd argue with is doing all that then adding the adding the "Add" method to the end. Being used to the ternary operator, I could read it fairly quickly, but to make it more human readable, perhaps an assignment a variable should have been assigned to the chosen list, then the second line should have implemented the Add method. In ANSI C in college, they encouraged this type of stacking behavior. In fact, they demanded it, but it was more of a performance issue than simply being "clever."

                                    1 Reply Last reply
                                    0
                                    • A annathor

                                      No, not always, there are some rare cases where cluttering the code with comments are justified. But if you look at some code and thinks "gosh I better comment it, so other people understands it" then it is an indication that the code is a horror, why do you need to explain the system whit a language that is not designed to describe a system, when you allready have described it with a language that is designed to excplain a system? Wouldn't it be better to left click, refactor->rename and clean up the code so it tells the story you want it to tell, insted of trying to excplain the story by including a secondary language into your source files? also comments tends to become misleading and/or misplaced as the system evolves, so instead of excplaining the system it just adds to the confusion. so, hyperboled, I say, a well commented code is a indication that the system is dirty. and commenting in itself can also make clean code dirty by breaking up the logic and lie to you while you read the code. In most cases you could explain what the sysem is supposed to do better whit a programming language than whit english or swahili. And if you, god forbid, uses comments to add todos, changelogs or similar there are plenty of good tools out there for that.

                                      Y Offline
                                      Y Offline
                                      YSLGuru
                                      wrote on last edited by
                                      #45

                                      @annathor I mean no disrespect but you are %100 wrong about good code not needing comments. It is this kind of mindset, that everyone thinks exactly alike, that leads to so many re-writes and problems. I used to work for a software company that employed many developers who had this same approach to commenting anything development related and it lead to numerous hours of unnecessary support simply because the developer assumed that the way they saw some scenario was the only way and therefore the same way all others would see it. This doesn't mean that ALL code must be commented for there are some simple ones that are fairly self-explanatory but never assume code can be left uncommented if its good code.

                                      1 Reply Last reply
                                      0
                                      • G GibbleCH

                                        A developer should know the language they work with

                                        R Offline
                                        R Offline
                                        Rob Grainger
                                        wrote on last edited by
                                        #46

                                        While I mostly agree, this is not always possible. C++ in particular is such a large language that few people know it completely, but its perfectly possible to write excellent programs using a familiar subset. In such cases, use the arcane features of the language can be regarded as a codin horror - as the majority of developers will need to look up the meaning. While programmers favouring other languages may scoff, this seems similar to knowing the libraries. I could learn C# syntax in at most a week for example, becoming familiar with the libraries takes longer, and is actually what I spend of my time doing. I'm absolutely sure I don't know them as well as I'd like, and there's whole areas I leave until I need to. What makes all this work is MS's documentation that (normally) helps see usage conveniently. Most of that is auto-generated from Comments, so comments seem fairly necessary. I kind of hate the "everyone who doesn't know what I know is an idiot" approach to programming - I'd hate to be on a project with someone with that attitude, they tend to leave unmaintainable code in their wake.

                                        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