When you don’t know the language…

internal static short uiShiftRightBy8(short x)
        {
            short iNew;
            iNew = (short)((x & 0x7FFF) / 256);
            if( (x & 0x8000) != 0)
                iNew = (short)(iNew | 0x80);

            return iNew;
        }

 

AHHHHHHH!!!!*

Nevermind that this was public even though it was used only once by the class in which it was defined. Nevermind that this was not marked static even though it uses no member functions. (Also nevermind that I made it internal instead of private so I could test it and make sure it was as stupid as I thought.)

When you learn a programming language, even when you can get things done in it, it still adds tremendous value to you as a programmer to READ A FRAKKING BOOK on the language. The above code works, absolutely. It does exactly what the writer intended. When the #1 concern when writing software is “do whatever is needed to ship it now, and you have no time to better yourself” you get this code. IMO maintainability should be #2 concern directly following “does it work?”

I’ve entirely removed the above code and replaced it with the operator and value it implements: >> 8

Yes it really is Shift Right by 8. Yes, someone didn’t know about the >> operator. Yes I wasted time reading code that uses this method. Yes I don’t want to think about the performance characteristic of this in a tight loop in critical systems.

*my coworkers laughed when I said AHHHHHHH!!! outloud. At least bad code provides us with solid entertainment.