LWN.net Logo

PHP: a fractal of bad design (fuzzy notepad)

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 18, 2012 20:09 UTC (Wed) by dskoll (subscriber, #1630)
In reply to: PHP: a fractal of bad design (fuzzy notepad) by nix
Parent article: PHP: a fractal of bad design (fuzzy notepad)

The other WTF in that bit of code is that sizeof(char) is by definition 1.


(Log in to post comments)

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 19, 2012 11:20 UTC (Thu) by rev (guest, #15082) [Link]

Well, that, in my not so humble opinion, they did right.

As a general rule if Y has knowledge about X, and one needs attribute a of X, one needs to query Y for X.a: SPOD.

Here, Y=the compiler, X=char and a=sizeof().

There is really no point in relying on sizeof(char) equaling 1. Demonstrating one's macho-ness knowing all this little details about the language to oneself and ones peers? Optimization? Any decent compiler will optimize sizeof(char) away. Saves typing?

Hardwiring sizeof(char)==1 violates the SPOD principle.

In general, relying on this fact implicitly is even worse: it reduces the maintainability of code. Sooner or later the char in my function is going to be changed. Say to w_char_t. 'malloc((size_t) num)' is likely to be overlooked by me or, somewhat less likely, by my peers, leading to spurious problems. 'malloc(num * sizeof(char))' is likely to be caught on the first pass. Thus, using sizeof(char) makes my code more maintainable.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 19, 2012 14:39 UTC (Thu) by nye (guest, #51576) [Link]

I think that can be summed up as 'avoid magic constants'.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 20, 2012 15:34 UTC (Fri) by rev (guest, #15082) [Link]

Nearly.

Defining a constant for sizeof(char) still leaves us with a SPOD violation: AMC: yes, SPOD: no.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 20, 2012 23:18 UTC (Fri) by mathstuf (subscriber, #69389) [Link]

What do SPOD and AMC stand for here?

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 21, 2012 0:05 UTC (Sat) by neilbrown (subscriber, #359) [Link]

AMC is "Avoid Magic Constants".

I'm guessing SPOD is "Single Point of Departure".

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 21, 2012 13:49 UTC (Sat) by rev (guest, #15082) [Link]

Single Point Of Definition.

Sorry, I thought the meanings of the acronyms where obvious from the context.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 21, 2012 18:26 UTC (Sat) by mathstuf (subscriber, #69389) [Link]

I had just never come across them before (AMC is strongly tied to the car manufacturer in my mind and SPOD is new).

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 20, 2012 17:26 UTC (Fri) by apoelstra (subscriber, #75205) [Link]

If you did

char *str = malloc (LEN * sizeof *str);

You would not need to change anything if you changed the type of str in the future. Explicitly writing a type, is a maintenance hassle, and writing sizeof(char) is pointless. So doing both is doubly bad, and suggests that the
author of the code is not very comfortable with the language. (Which again, increases the mental load on the maintainer.)

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 21, 2012 13:56 UTC (Sat) by rev (guest, #15082) [Link]

Yep. That would be even better idiom.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 21, 2012 14:19 UTC (Sat) by rev (guest, #15082) [Link]

A few side notes. Somewhat tongue in cheek, somewhat provocative, and on the other hand, quite serious.

There's only one programming language that's worse than PHP, and that is C. Make that two languages worse than PHP: Perl and C.

Whenever there's a need in a programming language community to take pride in knowing every quirk and detail of a language, as your remark alludes to, the merits of said language are up for debate.

Whenever programmers feel the urge to impress their peers with their knowledge of a programming language, the quality of these programmers is up for debate.

PHP: a fractal of bad design (fuzzy notepad)

Posted Apr 21, 2012 16:38 UTC (Sat) by apoelstra (subscriber, #75205) [Link]

The use of the 'sizeof' operator is hardly a detail or quirk of the language -- it is absolutely crucial to any C program that uses dynamically allocated memory.

PHP: a fractal of bad design (fuzzy notepad)

Posted May 7, 2012 15:04 UTC (Mon) by rev (guest, #15082) [Link]

Agreed. One of the reasons C should be avoided like the plague.

PHP: a fractal of bad design (fuzzy notepad)

Posted May 8, 2012 8:49 UTC (Tue) by nix (subscriber, #2304) [Link]

It's when you find yourself using addition, subtraction, or (size_t)pow() on the result of sizeof() that you really know you're playing with the big boys.

(I've done the first two, but so far have restrained myself from the insanity required for the third, preferring to multiply by hand. How plebeian.)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds