Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121312 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 4076 invoked from network); 14 Oct 2023 17:48:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Oct 2023 17:48:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CA5C0180041 for ; Sat, 14 Oct 2023 10:48:12 -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-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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:48:12 -0700 (PDT) Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-52f1ece3a76so979254a12.0 for ; Sat, 14 Oct 2023 10:48:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grudl-com.20230601.gappssmtp.com; s=20230601; t=1697305690; x=1697910490; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=6x86aicel6Cx9vzKKIBPUGNYzRowBnArGqUPqEkBor4=; b=zAX00j7x02f6j0ufWoLmEUtRznHDBiAEnLUt+K2AzUoSHzgOS0yBu87YogXnUVqcLC AmL8MT2lfzweVUfziN4VAltaFeYe4DUJdCs0onRpJzfIY87SqcKWFQtjQkB14E49kf0N PmbE61WjJOg+9mCWD+WbRAPVdGZqXzBbNY0fD323PO3NxtAxQBU6a/HIYqIq952G6Xtt yHFqM+bl9u63V2KT8pDJFsTbW2XtULhg8ibffrxtjXEgqe00z9qOJSR9QU/tTOeyg4Uw MgbhoVosBTtmyYMCG1xm8H+/XTafXZK8w9d9rdneXL+boIsI4LuRoC2K+nvcrG4ClQzN 3AEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697305690; x=1697910490; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=6x86aicel6Cx9vzKKIBPUGNYzRowBnArGqUPqEkBor4=; b=JLPfq51APzhYrcIknuljCeacD8aRmpqnW0sm7F3LkF2JJk6cZtpIY4Te9c3KSqCxB8 Ut3nSMzd6YeDpfAE+98YT4Y2HFFkyboE05auIGQE/YOtdEDfQiPGs+QdRtQ8C5EFlDu5 tonbp001t4nvORB5zp37f5g4KXFvi+OpSx9Njc4Cxul+IGia3cVfAsdf83S+QFvNoQNG WPttlU96wj/Hw4a/ga4J3fYtD42mBFDBDQc57MehqiJaU5DY+QmJ+FWBaXaqb818pNRD en/473PZrIb3vE1sYJ2sk3pFACh3LzZg0KDPCyzdswAF/nZ0wQnuynnxNiMr7CQrIdyE j2pA== X-Gm-Message-State: AOJu0YxXU28eVd98h6A9yAT/UBuWreLpUvC0dPJvBSyMBld7CQHj8UBj aBRCeiJENKP5BqqUMcoIDtXX+YSQ+aM5N9POkF3w9BOLX9iQIP3Pw3Q= X-Google-Smtp-Source: AGHT+IGroaH1YPxhyB0IOFzJh7HU6JKtNhyIuJmrH/RzLuyb+YLPRi9H4/tQ+jrZ2aPq0qKX70ax4G+Y0UakuXF/kb4= X-Received: by 2002:a17:906:100c:b0:9ae:6da8:181c with SMTP id 12-20020a170906100c00b009ae6da8181cmr23727160ejm.7.1697305690464; Sat, 14 Oct 2023 10:48:10 -0700 (PDT) MIME-Version: 1.0 Date: Sat, 14 Oct 2023 19:47:58 +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. And as we know, naming along with cache invalidation are the most complicated things :-) 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