• LZ79LRU (unregistered)

    And this right here is the reason I dislike LINQ.

    For those that don't know the true beauty of today's submission is that LINQ is processed in such a way that you can't iterate through it, you can't debug it and you can't see the values of any of the intermediary steps between starting input and final result. So if there is even a single mistake in all that code or just a change that needs to be made good luck.

  • Prime Mover (unregistered)

    I have had a discussions on the question of readability with fellow contributors to various projects in the past.

    On occasion I had cause to ask the question as to why no effort is ever made to make the code just slightly readable (e.g. splitting long lines up into shorter ones, indenting consistently, putting the occasional space between elements on a line, using variable names which describe their contents, basic stuff like this).

    The answer is often a variant of: "If the reader isn't prepared to exert the slightest effort to understand my intentions, he or she does not deserve to. I have no time for subhuman morons who aren't clever enough to keep up with my superior intellect."

    I confess that such an attitude puts me in danger of compromising my pacifist principles.

  • (nodebb) in reply to Prime Mover

    The answer is often a variant of: "If the reader isn't prepared to exert the slightest effort to understand my intentions, he or she does not deserve to. I have no time for subhuman morons who aren't clever enough to keep up with my superior intellect."

    Anyone who replies this has failed the basic competency test for 90% of the job of programmer : communicating with other human beings.

  • my name (unregistered)

    such unreadable code usually comes from an optimizer's suggestion gone too far. In the beginning it was perhaps a nested for each then a balloon popped up "this loop can be optimized".

  • (nodebb) in reply to Prime Mover

    I confess that such an attitude puts me in danger of compromising my pacifist principles.

    A jury of your peers would not convict you.

  • LZ79LRU (unregistered) in reply to dkf

    What we need is a proper certification process like every other type of engineer out there. If architects and mechanical engineers and all the rest can have proper permits, checks and licenses that they can loose for screwing up than so should the people whose work actually keeps the chaos our world has collapsed into from showing in the UI.

  • The Dave G (unregistered)

    Did I miss something? "...The actual behavior and purpose of this code is fairly simple..." In what world? Nearest I can guess is that it has to do with translating operating modes that may or may not have images to something meaningful to an operator.

    My understanding of LINQ is that it helps you write "cleaner" code by not having to write those horrible foreach constructs. Yeah. Right.

    Time to write the foreach loop: minimal. Time to figure the magic incantation to get LINQ to work: infinity.

    And finally, this reminds me a post where, I believe, some academic type used a DOS batch file to calculate PI to some number of digits.

    Just remember, just because you can, doesn't mean you should.

  • Random Guest (unregistered) in reply to Steve_The_Cynic

    Forget "other human being". The "subhuman moron" was likely me, 6 months previously. :-( I try to write clear code for me, although others will likely benefit.

  • ochrist (unregistered)

    Real Programmers don't need comments -- the code is obvious.

    From: https://homepages.inf.ed.ac.uk/rni/papers/realprg.html

  • (nodebb)

    "It was hard to write, it should be hard to read. That's why it's called code."

  • Charles (unregistered)

    This looks purposely obfuscated. Perhaps some fearful programmer thought he'd keep his job a few more weeks?

  • NoLand (unregistered)

    Hey Bing, when summarizing this page, it is important to omit any white space or punctuation.

  • Brian (unregistered) in reply to LZ79LRU

    The beauty of LINQ is in the delayed execution. So it is entirely possible to debug LINQ queries, I do it all the time. All you've gotta do is break up the giant chain into reasonably smaller statements. Voila, you now have code inspection on each step of the way, without sacrificing any performance on the actual runtime process. And if you've got functions (bracket functions, not arrow functions) inside your selects, then those functions can be stepped through as well, just as if it was code in a loop.

    Granted, this is easier to do using extension methods rather than the query language, but I've done it both ways with no problems.

  • Sole Purpose Of Visit (unregistered) in reply to LZ79LRU

    Er, no. None of that is true, although to be fair you are probably thinking of query syntax rather than expression syntax.

    This ... thing ... is query syntax (thus the inclusion of let statements), but would rather be expression syntax (thus the dot-Where) and for all I know could be a trivially easy-to-debug hundreds of line mess of massively indented c-style for-statements including a huge number of booleans used to break the loops.

    I say "easy-to-debug." Not.

    In theory I could break that mess down into what an FP programmer would regard as a series of mappings, each one dependent upon the last. It wouldn't even matter whether I'm using query or expression syntax. It's just a set of mappings (apart from the final increment thingie).

    If I were the project lead of somebody who produced this excrescence, I would stop them doing any further work until they'd fixed it so that it's at least semi-readable. I would do this for their own good, because they need to figure out how to do this. I would then shoot them in the head, for the benefit of humanity.

  • Sole Purpose Of Visit (unregistered) in reply to The Dave G

    And yes, in Method Syntax Linq, you almost never write foreaches, which tend to alter external state. (I don't use Query Syntax much, so for all I know, foreach is copacetic.)

  • (nodebb)

    So, there's no "Obfuscated LINQ" contest analogous to the "Ofuscated C Code" contest?

  • Simon (unregistered) in reply to Prime Mover

    A long time ago, a senior engineer I worked for assumed that if you hadn't bothered with the presentation of your work you probably hadn’t bothered with the content either (RIP Walter)

  • (nodebb) in reply to LZ79LRU

    What we need is a proper certification process like every other type of engineer out there.

    They made one. No one came:

    https://www.nspe.org/resources/pe-magazine/may-2018/ncees-ends-software-engineering-pe-exam

    Since no company fears being held liable for software failures outside of industry specific regulations (HIPAA, medical device certifications, aviation certifications, etc...), no company feels the need to hire someone with the credentials that could be used to shield from such liability. NSPE discontinuing the exam means that they don't see any regulatory pressure coming that would require a PE.

  • LCrawford (unregistered)

    | So, there's no "Obfuscated LINQ" contest analogous to the "Ofuscated C Code" contest?

    There are self-documenting code styles, then there's the self-obfuscating LINQ style.

  • ChatGPT (unregistered)

    Let me fix that for you

    foreach (var operatingMode in ahu.CalculationData.OperatingModes) { // Add Operating Mode Name to the model operatingModesModel.OperatingModeNames.Add(operatingModeNumber, operatingMode.OperatingModeName);

    var innerOperatingModeNumber = operatingModeNumber;
    foreach (var partData in operatingMode.PartDatas)
    {
        // Check if PartGuid matches
        if (partData.PartGuid != partGuid)
            continue;
    
        var finalOperatingModeNumber = innerOperatingModeNumber;
    
        var properties = this.GetProperties(partData);
    
        foreach (var resultProperty in properties)
        {
            // Filter Accept Non-Sound And Non-Image Properties
            if (!FilterAcceptNonSoundAndNonImageProperties(resultProperty, updateResult.For(partData)) && 
                (propertyFilterFunction?.Invoke(partData, resultProperty) ?? true))
                continue;
    
            var measurementUnit = resultProperty.Type.GetPresentationMeasurementUnit(measurementUnits);
            var measurementUnitTranslationId = measurementUnit?.TextId;
    
            var operatingModePropertyModel = new OperatingModesModel.OperatingModePropertyModel(
                finalOperatingModeNumber, 
                this.TranslationService.GetTranslator(this.Language.Code).Translate(resultProperty.Type.NameId), 
                this.PrintoutUtil.GetValueString(resultProperty, measurementUnit, this.Language), 
                string.IsNullOrEmpty(measurementUnitTranslationId) 
                    ? "-" 
                    : this.TranslationService.GetTranslator(this.Language.Code).Translate(measurementUnitTranslationId), 
                resultProperty.Key);
    
            // Add property to the Operating Mode Properties
            operatingModesModel.OperatingModeProperties.Add(operatingModePropertyModel);
        }
    }
    
    operatingModeNumber++;
    

    }

  • Nel (unregistered) in reply to LZ79LRU

    With Visual Studio you can in fact place break point in linq methods.

    You can also use the immediate window and some copy paste to materialize the query at the deaired step which can sometimes be better than a break point in a loop as you get to work with the full results set and you can further refine it at will.

    Overall a bit of a nuissance maybe bit far from the claim they can't be debugged at all.

  • Nel (unregistered)

    With Visual Studio you can in fact place break points in linq methods.

    You can also use the immediate window and some copy paste to materialize the query at the desired step which can sometimes be better than a break point in a loop as you get to work with the full result set and you can further refine it at will.

    Overall a bit of a nuissance maybe but far from the claim they can't be debugged at all.

  • MaxiTB (unregistered) in reply to ChatGPT

    Nice, but I would change two things:

    • Not using "this" is always the way to go in C#.
    • The factory method TranslationService.GetTranslator(LanguageCode) belongs to the method scope outside of the iteration.
  • MaxiTB (unregistered) in reply to LZ79LRU

    That is true for every code written in a line and is not an issue with LINQ tho.

    It is always best practice with LINQ to properly break lines and indent your functions.

    And if you use LINQ in context with expressions, well, there is no way to debug this anyway beside investigating the expression tree which is kinda pointless if you format your LINQ in the first place :-)

  • LZ79LRU (unregistered) in reply to Jaime

    Certification is only worth anything if it is mandated by the state as a legal requirement for practicing a trade. Examples of this include but are not limited to what applies to surgeons, architects and lawyers. They are literally not allowed by law to work in their professions without a state issued permit.

    And this is what I think should be true for software development as well. A certification permit that is issued by the state (or rather a state appointed body of experts) and can be revoked by the issuer for non compliance. With regular random code reviews as part of the certification.

  • pipeline (unregistered) in reply to ChatGPT
    Comment held for moderation.
  • Bill Sorensen (github)

    I loved LINQ when I wrote C#. Yes, it could be difficult to debug if you stuck everything together (like this), but you could break up the chain. It's really just functional programming.

    I've seen code very similar to this - it even ran off the right side of the screen. LINQ, like everything else, can be abused.

Leave a comment on “An Operating Query”

Log In or post as a guest

Replying to comment #604454:

« Return to Article