Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121328 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 80988 invoked from network); 15 Oct 2023 17:53:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2023 17:53:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E7741180504 for ; Sun, 15 Oct 2023 10:53:37 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 15 Oct 2023 10:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1697392415; bh=Ep8i1nrSE3pG7mExAOGRt08LjWzZhw+/nqUMttR9lRM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=jCPv7+l8WJpYEvhh6YA8l8/2PSgr9/1nhCE9LB0UTRLKJZQo9GpUGFT7ZG7J/Hxmo klkhzE+ebLtb2/g9leMKS+gjU1WjZ4M9r8HNnjVart+q1ki9Rp9+pshfQzmip9CVrQ kRArg76Heqpkb1TL1qDGXF33Bpiqc2e+H0bzLvj5VWsjCIIOcSgcL1JKyKVuTrXdbd 5eFSeO8nTra4wArr9Tq8/qyUBJQYimpTRaQHCuZzTI2cfT5jutxes32dnHe1yF0hmQ K1JXX6eIDk3Wj0AjaUeFWmtYSy7i+lzhV3EomIrHEpVdZhsGJLYUuZNPbqy0lZa9JK HP7cEqo/UalyQ== Message-ID: Date: Sun, 15 Oct 2023 19:53:34 +0200 MIME-Version: 1.0 Content-Language: en-US To: tag Knife , David Grudl Cc: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Better name for method Randomizer::nextFloat() From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 10/15/23 02:04, tag Knife wrote: > I think the major pet peeve i have is that the "Randomizer" class is > encapsulating all random functions. > I would like to see each randomizer have its own class, since they are > under the Random namespace > anyway, I believe they should be Random/StringRandomizer, > Random/IntRandomizer, > Random/FloatRandomizer, so on so forth. Basically Ramdom/{Type}Randomizer. > This would > also allow more granular and specific functionality for each type. > > For example, for this discussion, the class, Random/FloatRandomizer (and > IntRandomizer) > could be instatiated with its boundries. > > ```php > $randomFloat = new Random/FloatRandomizer($lowerBound, $upperBound, > $boundryInterval) > $a = $randomFloat->get(); > $b = $randomFLoat->next(); > ``` > > Is that not more intuitive? > Creating a separate class for each possible distribution would certainly be the cleanest API from an "academic" point of view. I'm not sure if it would be more intuitive, but I'm sure that it would not be easier to use. The API of Random\Randomizer is probably not perfect, but I believe it does quite a few things quite right: - It is secure by default, if you do not provide an engine, it defaults to the CSPRNG. - It is discoverable, your IDE will autocomplete the available methods and all the methods are listed right beneath each other in the documentation. - It is succinct for common cases and (mostly) does what it says on the tin. - It allows you to easily build additional (userland) APIs on top of it, thus serving as a "building block" (to reuse the phrasing from the Randomizer additions RFC). Such a userland API could look like the API you proposed (though it would probably make sense to also include the "Uniform" somewhere within the name, because you could also have a "Normal" distribution, so we're right in bike-shedding territory, API design is hard). To give an example: // Inspired by Rust's API. $engine = new \Random\Engine\Secure(); $oneToHundred = new \Random\Distribution\UniformInt(1, 100); $randomInt = $oneToHundred($engine); $anotherRandomInt = $oneToHundred($engine); vs // The default PHP API. $randomizer = new \Random\Randomizer(); $randomInt = $randomizer->getInt(1, 100); $anotherRandomInt = $randomizer->getInt(1, 100); I would certainly prefer PHP's API for most of my applications, because it's much less boilerplate. It also avoids constructing many short-lived objects [1]: You can just throw both the Engine and Randomizer into your dependency injection container. Best regards Tim Düsterhus [1] I expect the performance impact of constructing and destructing objects to be greater than a regular function call, but might be wrong here.