Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121311 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1864 invoked from network); 14 Oct 2023 17:41:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Oct 2023 17:41:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B00DE1804AC for ; Sat, 14 Oct 2023 10:41:22 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,LOTS_OF_MONEY,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 14 Oct 2023 10:41:22 -0700 (PDT) Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-9b2d64c9307so109897066b.1 for ; Sat, 14 Oct 2023 10:41:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grudl-com.20230601.gappssmtp.com; s=20230601; t=1697305280; x=1697910080; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=S8GcEEsd9o339QduQCFNR0qL980BoGisZQ43tHPMLSE=; b=kv0SDf7fCiD8Zs1ue4S1YTscnC3BUM26ozdE5dRNmfx6DxQYzKLwE70naOaLnzhv3t v0TBW38mXVitUr+yQJ5t3Bglbz6Fxgs2YD+JRhRD9POiE1Bos3B50q4a6Q2vtJiUBlnL Yhl9Gfn9N4dKGjZHtyen/cC9t1iZe/ObfuxlR2q+yr6dyikfmKhGr2Jrkfm8tH2ZfRST I7zP7xqAdM5rSoFA75oR6BN/eqP+mpDagyuxWJZkU3v2a9gORo0dDHZPCbTCY0veDQrC uX3sBqJaqp4TNKdBNuuWkSQCdRkZHY7UWB3gNQjxRDKqJItt1T87evpq2bI3ii23hNGK rcEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697305280; x=1697910080; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=S8GcEEsd9o339QduQCFNR0qL980BoGisZQ43tHPMLSE=; b=rGfj1wxB75q1hME9h86anjms1L2XVEixqPVem1WIf/YWQTaNxJlXgXeKnxBi0Ypgz8 AOAlAsm2aRPnZxphREvT4V6HRsdW62NkSz4pGCOwOAl46mzmNaWKDLjEKGcOY2sEvczp EzoBQ6JZ+JW1nEEIokF6B6agfOAO/QXWXGS1/kKjY8yFKmsyTK8GfwUS92EFlYiLNbFP fAlm3GopFH4TDBNsrsyn9K6Ge0W2mN9Q6t2RFqHZ0B6jA0N0xVs5h9PugVrDtponNxgG WfgGIN+HGmStC23Pkvnf7Ap9wQsObXWZKL7iG7jJzORUfm1NM6OgcWuUBAInANU/YFRQ L4Yg== X-Gm-Message-State: AOJu0YyTWDJAE5b2AUksi2vWT8sM/c6mWI8xFUGwrC2AwJj03A9+zhWJ Npfs9JN3hJVowa+UcpDNlZ49lb5bCbGZhFoOHzcj8+TqTW8U3N5RQzc= X-Google-Smtp-Source: AGHT+IElLwixVXLmDengnMg4cnEz8oHbCibiROpb+Pr1o1GEQnzZ2LS1/osDrfyWCWjFa4PYrtLUTwr3gFnfp4sLVzM= X-Received: by 2002:a17:907:9801:b0:9bf:4f34:c32d with SMTP id ji1-20020a170907980100b009bf4f34c32dmr1120533ejc.2.1697305280204; Sat, 14 Oct 2023 10:41:20 -0700 (PDT) MIME-Version: 1.0 Date: Sat, 14 Oct 2023 19:41:07 +0200 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Better name for method Randomizer::nextFloat() From: david@grudl.com (David Grudl) I would like to suggest changing the name or removing the nextFloat() method, which is coming to PHP 8.3. The Random\Randomizer class will have two new methods for generating random numbers: - getFloat($min, $max [, $boundary]) - nextFloat() Now please try to guess what these methods do and, more importantly, how they differ. I bet no one can guess. Which is, of course, a symptom of poor naming. What is the correct answer? It differs in that nextFloat() returns a random number in the interval 0...1, so it does the same thing as getFloat(0, 1). See https://wiki.php.net/rfc/randomizer_additions#nextfloat A) The problem with nextFloat() is that the name creates a false expectation. Take a look at the following code: ``` $randomizer = new \Random\Randomizer(); $a = $randomizer->getFloat(100, 200); // number between 100..200 $b = $randomizer->nextFloat(); // another number between 100..200 ??? ``` One would expect that in `$b` there would be a number in the interval 100..200 again, but surprisingly not, there will be a number in the interval 0..1. B) When trying to think of a more appropriate name (e.g. `getFloat01()` ?), I find that the simple `getFloat(0, 1) is concise and succinct enough. If it were really useful to have a method that returns numbers in this interval, wouldn't it be better to give the $min and $max parameters the default values of 0 and 1, and thus call `getFloat()` directly? C) PHP 8.3 is still in testing and this change can be made. If it is not done now, it will never be done and programmers will have to use and also read unintuitive APIs for years. Similarly, in the days before PHP 8.0 was released, I suggested changing PhpToken::getAll() to today's tokenize() and there was no problem with that https://bugs.php.net/bug.php?id=80328 D) There are libraries in other languages (Java, ASP.NET, etc.) that also use methods named next() to generate numbers. This is perfectly fine. The point is that they don't also use methods named get() in the same time. The problem I am pointing out is that methods differing in the prefix get/next differ only in the interval from which they return numbers. You won't find this in any other language. Thank for your time DG