Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
Check out the new SourceForge HTML5 internet speed test! No Flash necessary and runs on all devices. ×
Programming

C Code On GitHub Has the Most "Ugly Hacks" 264

itwbennett writes: An analysis of GitHub data shows that C developers are creating the most ugly hacks — or are at least the most willing to admit to it. To answer the question of which programming language produces the most ugly hacks, ITworld's Phil Johnson first used the search feature on GitHub, looking for code files that contained the string 'ugly hack'. In that case, C comes up first by a wide margin, with over 181,000 code files containing that string. The rest of the top ten languages were PHP (79k files), JavaScript (38k), C++ (22k), Python (19k), Text (11k), Makefile (11k), HTML, (10k), Java (7k), and Perl (4k). Even when controlling for the number of repositories, C wins the ugly-hack-athon by a landslide, Johnson found.
This discussion has been archived. No new comments can be posted.

C Code On GitHub Has the Most "Ugly Hacks"

Comments Filter:
  • Perl (Score:5, Funny)

    by mozumder ( 178398 ) on Wednesday May 06, 2015 @11:34PM (#49635533)

    Pretty sure people already assume that every line of Perl code is an ugly hack anyways, so they didn't have to write a comment on it.

  • by angryargus ( 559948 ) on Wednesday May 06, 2015 @11:36PM (#49635541)

    Regardless this seems like a pretty crappy study. There's many other phrases like kludge or XXX to have considered.

    • by Anonymous Coward on Wednesday May 06, 2015 @11:41PM (#49635555)

      C developers are good enough to know when what they're doing is an ugly hack.

      If PHP developers were at the same standard, every line would end with // Ugly Hack.

      • by Dracos ( 107777 )

        Just WordPress developers. No, I take that back... WP devs are so shit that they wouldn't recognize any hack.

      • by dgatwood ( 11270 ) on Thursday May 07, 2015 @01:24AM (#49635891) Homepage Journal

        C developers are good enough to know when what they're doing is an ugly hack.

        If PHP developers were at the same standard, every line would end with // Ugly Hack.

        I think the reason PHP is #2 on the list is that the people who are still writing PHP are mostly pretty good. The ones who were awful have all moved on to Python or Ruby or whatever the scripting language of the week is these days.

        In fact, I'd be willing to bet that a sizable percentage of the folks who are still actively using PHP are C programmers. I use it for all my web programming because it is exceptionally easy for me as a long-time C programmer. I basically write C with dollar signs and a few other minor tweaks, and it works. Even better, if there's some piece of code that has to be blisteringly fast, I can port it from PHP to C faster than you can say sed 's/\$//sg'. Okay, it really isn't quite that trivial, but it is pretty close.

        And yes, I do occasionally take advantage of being able to mix PHP and HTML, but not very often. I mostly just use it as a compile-free web programming language with better string handling and basic support for classes.

        • Another advantage of PHP is that the ugly hacks^H^H^H^H^H^H^H^H^H tricky operations are already written for you* - you just have to Google, and then invoke, the relevant library routine.

          * And in some cases, even tested too!

    • Well, of course - it only counts how many occurences of a certain string you can find. An alternative interpretation could be that since people would have to know what beautiful code looks like in order to decide what ugly code looks like, there must be far more C programmers who know how to write good code. Which makes sense, I think; C is a language that allows the coder to make horrible mistakes, so in order to survive, you have to develop a coding style with strong discipline, which is what makes beauti

    • by delt0r ( 999393 )
      I like to personally use the phrase "A puppy dies every time this is executed"
    • by mwvdlee ( 775178 )

      Or maybe they're simply aware of what is ugly and what is not.

      I develop PHP for a living and a while back I encountered a styleguide which said you should give function arguments. Code like 'function f($a, $a, $a) actually works in PHP. This in itself this as insane enough, but... some developers are retarded enough to require a styleguide to tell them not to do that. You think any of those developers will be able to tell the difference between an ugly hack and good, clean code?

      My guess is that most of this

  • by darkain ( 749283 ) on Wednesday May 06, 2015 @11:42PM (#49635559) Homepage

    Seriously guys. File this one under "NO SHIT" - Of course C is going to have the most ugly hacks. Why? Because it is by design able to access a hell of a lot more than other languages. How many languages have direct hardware access? Or inline ASM code? And does the word "hack" in the code really make it an "ugly" hack? Seriously? I wrote a micro-kernel for an ARM platform about a decade ago, and there was an assload of inline ASM code and direct pointer manipulation to access the underlying hardware, there is no other way to do this. Yeah, I'm sure the word "hack" appeared countless times in my code, because that's the general term we use. That doesn't make it "ugly" or bad by any means.

    • by x0ra ( 1249540 )
      You should check those "hack" really, it's not worth the name "hack".
      • by gstoddart ( 321705 ) on Thursday May 07, 2015 @07:46AM (#49637253) Homepage

        Honestly, for some of us "hack" means anywhere from "an inelegant but necessary workaround", to "a really awesome and unexpected use of something", to "defeating system security", or a clueless person bashing away at something they don't understand, or "something I just whipped up".

        GP is absolutely correct ... for many of us, "hack" is a very generic term.

    • by AmiMoJo ( 196126 ) <mojo.world3@net> on Thursday May 07, 2015 @12:10AM (#49635641) Homepage

      Maybe the reason C has more is that the specific string "ugly hack" is more commonly used by C programmers. Languages like Javascript are really ugly and full of nasty hacks, but that's normal for them so no-one comments on it.

      • by I4ko ( 695382 )
        This one exactly. The non-C developers are in general ignoramuses and for them the ugly hack is modus operandi, not an exception. For the C developer that is an exception, so they can recognize it and mark it as such. .
      • For languages like JavaScript, it's such an easy/accessible/portable language I actually wonder what the breakdown is. Just like every other language there are people who are really proficient in JS and aware of good practices and design, but there are also people who are so used to learning from ugly hacks of others they may feel it's acceptable, or they just have so little background with good code they have no idea that half their program constitutes an ugly hack. C certainly has a higher barrier to entr

    • by jmv ( 93421 )

      I would also add that the most common reason why people write "ugly hacks" is to make their programs fast. If you care enough about speed to put these ugly hacks in your program, you're certainly not going to write it in Java or Python.

    • by someone1234 ( 830754 ) on Thursday May 07, 2015 @12:57AM (#49635785)

      In this study, an ugly hack was determined by the string "ugly hack" appearing in code comments. It was a totally scientific study.

    • You sound incredibly angry. How DARE someone make a script that counts instances of "ugly hacks". IT IS A DISGRACE TO POINT OUT THINGS WE ALREADY KNOW. HAVE THESE PEOPLE NO DECORUM?
  • by hamster_nz ( 656572 ) on Wednesday May 06, 2015 @11:42PM (#49635561)

    C coder know a ugly hack when they see one, and when they write one.

    I would conjecture that nearly every line of Perl scripts is an ugly hack, so nobody bothers to add a comment... 8-)

    • by hughbar ( 579555 )
      I'm a Perl person, so biased. That said, I agree with an associated idea, only realistic, humble and experienced people will mark 'ugly hacks' as such. Many others, less experienced, ignorant or simply 'bad' won't even recognise them.

      I'd love to see standardisation across languages for FIXME and TODO too, then it would be easier to distinguish the two cases, where they ARE distinct.

      Actually there are a lot of very big Perl codebases, well written and commented [and some atrocious ones, like every othe
  • by hamster_nz ( 656572 ) on Wednesday May 06, 2015 @11:46PM (#49635573)

    Fast inverse square root (sometimes referred to as Fast InvSqrt() or by the hexadecimal constant 0x5f3759df) is a method of calculating x1/2, the reciprocal (or multiplicative inverse) of a square root for a 32-bit floating point number in IEEE 754 floating point format.

    http://en.wikipedia.org/wiki/F... [wikipedia.org]

    Anybody got any better Ugly Hacks to share?

    • by Anonymous Coward on Wednesday May 06, 2015 @11:52PM (#49635589)

      Duff's device [wikipedia.org]

      C's default fall-through in case statements has long been one of its most controversial features; Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against."

      • by suutar ( 1860506 )

        Eh. I don't have a problem with the fall-through. Interleaving the switch cases and the do/while makes me shudder.

      • But that's mostly a C implementation of unrolling, something that would be perfectly normal in assembler code. But then there are the programmers who in their effort to banish all forms of Fortran style spaghetti code reject anything that can't be easily reworked with IFs and WHILEs. It probably also horrifies some programmers who think it's premature optimization (if it's not yet the end of the world then it's too soon).

    • I don't know if that counts as a hack.....

      Right now I am working on a project with a 4,000 line function, related classes scattered across multiple projects so they can't compile easily, objects from the messaging library used throughout so it can't be easily un-coupled, objects whose purpose is unclear and don't make sense even when it becomes clear. Not to mention inefficiencies that mean it is several orders of magnitude slower than it would be if written in [any language, really, as long as the design
    • float->int conversion used to be expensive on x86 processors due to default rounding modes from C, and lack of suitable built in rounding functions. Looking back, my code contained this handy function. Ugly hack or elegant performance improvement? I'd suggest that the difference comes down to comments, unit tests, and whether people die if it's got bugs ;-)

      #ifdef WIN32
      #ifdef ASSUME_ROUNDING // This method relies on knowing the rounding mode of the float processor // // It rel

    • IEEE 754 is not magical, and neither is working with the IEEE 754 format at the binary level.

      Those that think that its an "ugly hack" to do so are computer science illiterate. Like that other guy that thinks leveraging twos-complement is an ugly hack.

      Pretenders dont know what ugly hacks are. You are one of them.
    • by abies ( 607076 )

      For me XOR swap was always an example of hack which is extremly ugly if not commented.
      http://en.wikipedia.org/wiki/X... [wikipedia.org]

  • Ugly but beautiful (Score:2, Insightful)

    by Anonymous Coward

    God I love C.

  • I realize the analysis is probably a little tongue-in-cheek, but this is probably the worst analysis I've ever seen. Nothing of use was gained...
    • by tgv ( 254536 )

      You're right. Number of files? Comment language? Size? History length? Just a few parameters that seem to influence the likelihood with which "Ugly hack" appears. Nope, first a good model, then the analysis.

    • Nonsense, it distracted me from my humdrum existence for a few minutes.

  • by Okian Warrior ( 537106 ) on Wednesday May 06, 2015 @11:52PM (#49635591) Homepage Journal

    Perl was last on that list?

    For those people who say that Perl coders only write incomprehensible gibberish, I say:

    BWA HA HA HA HA!

    • Perl was last on that list?

      For those people who say that Perl coders only write incomprehensible gibberish, I say:

      BWA HA HA HA HA!

      (To be fair, a more interesting statistic would be the percent of files in each language that contain ugly hacks.)

  • Nonsense... (Score:5, Insightful)

    by SlovakWakko ( 1025878 ) on Wednesday May 06, 2015 @11:55PM (#49635605)
    The whole C language is one beautiful hack, scary at first but once you get to know it in some really messed up sw project you can't help but love it. The balance between freedom and structure is excellent.
    • Really? Not in my experience. Have been developing embedded software in C for money for 10 years now. The more years have passed, the stronger my dislike of C grew. It was ugly enough for the 1970ies, but using the same tools in 2015 is insane, I think. It is like using a slide rule instead of a TI-84.

  • Low hanging hack... (Score:5, Informative)

    by x0ra ( 1249540 ) on Wednesday May 06, 2015 @11:57PM (#49635611)
    Did any of you actually read the "ugly hacks" this is pretty low hanging stuff, not the evil pointer-arithmetics and language corner undefined behavior case type of hack...

    #ifndef __INCif_etherh /* Quick and ugly hack for VxWorks */

    // ugly hack because we don't have fscanf
    int fscanf(FILE* stream, const char* format, int* value)

    #else /* ugly hack to make it compile on RH 4.2 - WA */
    #include
    #endif

    /* ugly hack GRR */
    #if !defined(__GNUC__) && !defined(__common_include__)
    #define __attribute__(x) /* nothing */

    /* XXX argh, ugly hack to make stuff compile! */
    #define snprintf(BUF, SIZE, ...) sprintf(BUF, __VA_ARGS__)

    • by chiasmus1 ( 654565 ) on Thursday May 07, 2015 @12:21AM (#49635683) Homepage
      After looking at those examples, it seems to me that C programmers can claim something is an "ugly hack" because it was not what they wanted or because someone else's code was messed up. The C code hacks where there because they could not see an elegant solution. Programmers for other languages probably do not even know that the code they are writing is ugly.
      • Yup, they are trying to make portable code which is also efficient, and that's hard to do as pragmatics keep tripping you up. With a lot of other languages, the portability is generally easier because there are no compiler differences, no machine differences, no limited subsets available for smaller machines, or the community generally never worries about portability.

  • I for one would love to see some examples of such "ugly hacks", and also how it should/could be done in a not so ugly manner.

  • Heavy duplication (Score:4, Insightful)

    by x0ra ( 1249540 ) on Thursday May 07, 2015 @12:03AM (#49635627)
    Every single page has many occurence of the same "ugly" hack. If the folks who did the study had an ounce of legitimacy, they would have filtered for all those duplicates. If they had actually been competent, they would have done an in-depth study of all those "ugly hack". Of course, at this point, the article would have been worthless, but hey, they got their first page on /. ...
  • by blind biker ( 1066130 ) on Thursday May 07, 2015 @12:04AM (#49635631) Journal

    These numbers should be weighted to the amounts of code in the various programming languages on GitHub. There may be lots of C "codefiles" with the "ugly hack" string in them, but there probably is a lot of C code overall on GitHub, too.

  • Define Ugly Hack (Score:4, Informative)

    by enter to exit ( 1049190 ) on Thursday May 07, 2015 @12:20AM (#49635675)
    "Ugly Hack" very often means the programmer has done a smart thing, if not an exactly correct thing. Although sometimes an ugly hack is just an ugly hack.
  • it's a C idiom (Score:5, Insightful)

    by bugs2squash ( 1132591 ) on Thursday May 07, 2015 @12:31AM (#49635701)
    "/* ugly hack to... */" is a modest expression of pride describing concise, functional, readable and elegant C code in the same way as the term "//elegant approach to..." in C++ describes some borderline-insane misapplication of the STL with the incomprehensibility of perl and the verbosity of java.
    • in the same way as the term "//elegant approach to..." in C++ describes some borderline-insane misapplication of the STL with the incomprehensibility of perl and the verbosity of java.

      rotfl

  • by Mantrid42 ( 972953 ) on Thursday May 07, 2015 @12:41AM (#49635735)
    If a solution is stupid and it works, then it's not stupid.
    • by swilly ( 24960 )

      Maxim 43: If it's stupid and it works, it's still stupid and you're lucky.

      Credit goes to Schlock Mercenary [schlockmercenary.com].

    • Hmm, a code is supposed to be read and maintained. Using ugly hacks whenever possible is more of a showing off attitude. You know the "I use ugly hacks to show how good I know C, and then I post it on github to reach as many people as possible" attitude.
  • How could "ugly hack" exist in perl code. Every single line ever written is an elegant beautiful hack, by design.

    • Just grep the (locally) installed modules, there are plenty of occurrences of "ugly hack". Even my own Perl code uses this, but rarely.
  • WTF! An autoplay video on frontpage? Slashdot, you have reached a new low ...
    Is it time for me to finally tick the "Disable Advertising" checkbox?
  • Because C is so sparse and clean (or primitive, ymmv!), and people using C tend to be more experienced (almost nobody starts with C anymore - you use it because the job needs it), I find C programmers are a lot more likely to recognize things as ugly hacks and label them. It's partly defensive, because other C programmers are also old and cranky, so you're tagging it with YES I KNOW don't start with me. You don't want to check this in and have, say, Linus think you don't realize compromises were required.

    O

  • Which makes this all subjective. There are already comments by people who say that anything not done in C is ugly, so how to tell that these same people didn't pepper other's code with that statement? Many people think code is an ugly hack merely because it wasn't done the way they would code it.

    I've seen code written with procedures named for Alice in Wonderland characters and activities. Yet, I've seen that kind of thing defended here as 'creative'. 'Ugly hack' in a comment is a worthless indicator.

  • And it has them for the same reason it has beautiful hacks:

    The complete and utter lack of fucking memory management, forcing the development of such hacks.

  • Coming up after the break, new insights into why dogs lick their balls.

  • Do we know that the comments were added by the author? An alternative explanation is that the C code has had fuller peer review than other languages, and the reviewer has marked these areas for future refactoring.
  • Maybe C developers are just honest and experienced and name what it is.
    I won't accuse Java, with it patterns of patterns, when there is such a easy victim like PHP.

    PHP developers start their first line virtually with /* big hack */ and finish the last line with /* this is cruel */.

  • by 140Mandak262Jamuna ( 970587 ) on Thursday May 07, 2015 @07:38AM (#49637181) Journal
    They are just analyzing mostly comments, not code. Dijkstra gave the prescient warning ages ago: "Always debug the code, not the comment".

    C coders know when they are using ugly hacks and would take a moment to comment it or name the function with the term ugly hack. They realize it is not elegant and make a note so that future developers do not think it is a reference implementation worthy of replication and emulation. It is basically "this is probably not worth copy/paste, do a fresh implementation".

    Other language coders might be using these ugly hacks with pride not knowing anything better.

"It doesn't much signify whom one marries for one is sure to find out next morning it was someone else." -- Rogers

Working...