• 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)

    Well, PHP is special. First, switches are internally handled as highly efficient lookup tables, especially for integers, so ironically that was the best solution by far (not taking about this use case, with is a simple range check plus a substraction). Since PHP8 they optimized integer arrays (they have to be ascending integers), but even then switch are a teenzy tiny bit more optimal, but by now it's more a preferance of clearly readable code at the relevant position or hiding it somewhere in a lookup array.

  • (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

    The more generally applicable approach is to look up the number in a string literal containing all the characters in numeric order. (Assuming all languages have a "get the nth character in a string" function.) For EBCDIC, the string would be longer and I think you would need nulls (0000 0000 bytes) for the gaps that don't code any character. (It's been a long time since I used EBCDIC at all.) Probably the most generic way for 8-bit character sets would be to use a 256 character string literal; truncate the number to 8-bits and check for null after the lookup to signal an invalid code.

  • (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)

    I dare say the reason for the $type argument can be found out not by what the various functions do with it, but by when and how it is used.

  • CmdrShepard (unregistered)

    $data = ($value <= 25) ? chr($value + 65) : "XYZ"

    You are welcome.

Leave a comment on “Black Letters”

Log In or post as a guest

Replying to comment #:

« Return to Article