• Sauron (unregistered)

    They could have replaced it all with 2-3 lines: https://stackoverflow.com/a/33334781

    Also, frist =)

  • Old lurker (unregistered)

    On a positive note, we discovered this code monkey can count up to 25 and recite the 26 letters in order. I wouldn't believe someone with so extensive brain damage would be capable of that.

  • (nodebb)

    I keep the ASCII numeric value for a capital letter A in my head. It takes up space in my memory that could be available for useful stuff. But it was useful 45 years ago. The author of this code is younger than me. :-)

  • (nodebb)

    This code is not my type.

  • (nodebb)

    I wouldn't be surprised if "type" is used in some kind of inventory way. Like, letter size printer paper might be value 7 (stationery), and because it's an inventory item (company orders 100 boxes and keeps them in storage counts as "inventory") it would be type 0, so we know it lives in storage bay H. But .... ledger size printer paper is also value 7 (stationery) but it's not an inventory item, so it's type 1 -- and there is none in storage (empty string returned).

    Or, it could "preparation" for expansion of the application.

    Or, it could just be another WTF that was copypasta'd from CrappyCode.Com with the bits they didn't need pulled out?

  • Gavin (unregistered)

    Compared to some of the absolute shite I've had to deal with, this code is clear, legible and logical and its purpose is immediately apparent.

    And yet it still deserves derision.

  • the cow (not the robot) (unregistered)

    In good ol' C...

    data='A'+value; // Add some boundary checks only if needed...

  • MaxiTB (unregistered)
    Comment held for moderation.
  • (nodebb) in reply to Rick

    65!

  • (nodebb)

    I already commented on the switch statement above, now lets get a bit more into the meat of the article.

    Empty else: This is a stupid pattern that was taught with the rise of Java in the 90s onwards. For some reason some developers thought every if statement needs an else statement to be considered good code. In reality its just verbose nonsense similar to the false doctrine about if statements never have to have negated conditions (so basically you have an empty if block with everything in the else block). This code smells heavily to made by someone fresh you got that bad outdated schooling.

    Exceptions: I disagree with that idea, not because of performance (PHP has bigger hotspots like the mentioned switch statement over arrays) but because PHP is rightfully used in the view section of an MVC or MVVM application. In other words the use case comes usually from users (and all their nonsense requirements and odd solutions to known best practices) and exceptions at that point are no longer actively wanted. So I really can't fault the dev here by default, even though the best devs know how to say NO and stick to it as long as possible when BS requirements arise.

    Finally here the proper implementation already hinted in the arcticle:

    <?php
    private function getLetter($value, $type = 0)
    {
    	if($type != 0) return '';
    	$data = Ord($value);
    	return $data >= 0x41 && $data <= 0x5a ? $data : 'XYZ'; 
    }
    ?>
    
  • giammin (unregistered)

    getLetter return string lol

  • Yazeran (unregistered) in reply to MaxiTB

    I think you forgot to add 0x41 to the input value before running it through Ord()...

    Yazeran

  • Officer Johnny Holzkopf (unregistered) in reply to MaxiTB

    The fun... ahem... "fun" begins when you use the "arithmetic approach" with text files coming from a system that encodes characters in EBCDIC...

  • Tinkle (unregistered)

    Hmm, PHP?

    HACK: strtoupper(base_convert($value + 10, 10, 36))

    And there you have a different WTF

    Not sure if the strtoupper is required or not. I do not program in PHP

  • (nodebb) in reply to MaxiTB

    Ooops, forgot to substract the offset: $data + 0x4a

  • (nodebb) in reply to Erik

    I also have the numeric 8086 opcode for a NOP instruction in my head. Your turn..

    We used to refer to a coworker using this number.

  • (nodebb) in reply to Officer Johnny Holzkopf

    Well, EBCDIC is ... extra special. I pitty everyone who has to deal with old IBM glory, like my younger self :-)

  • markm (unregistered) in reply to Officer Johnny Holzkopf
    Comment held for moderation.
  • (nodebb)

    I think $type was meant as future-proofing for them someday needing to convert [0-25] to [a-z].

    Or to some other insane set of codes created by Old Crazy Sam in Accounting.

  • (nodebb) in reply to MaxiTB

    Ooops, forgot to substract the offset: $data + 0x4a

    WTF?

    You did an addition and you added on the ASCII for J.

    Also, in your original code you used ord() when you probably meant chr()

  • RLB (unregistered)
    Comment held for moderation.
  • CmdrShepard (unregistered)
    Comment held for moderation.

Leave a comment on “Black Letters”

Log In or post as a guest

Replying to comment #:

« Return to Article