"value":"\n <p>Something I've seen in code is the unclear use of zero when adding limits, such as loading items from a database.<\/p>\n\n<p>In some instances, setting zero will return all items - essentially, an 'unlimited' value in disguise - rather than returning no results, which is what I'd expect.<\/p>\n\n<p>I imagine the code looks something like this:<\/p>\n\n<pre><code class=\"language-php\">if ($limit > 0) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\n<p>If <code>$limit<\/code> is greater than one, add it to the query.<\/p>\n\n<p>For me, using <code>0<\/code> as the unlimited value doesn't seem like the best option.<\/p>\n\n<p>I'd prefer to use a <code>null<\/code> value as the default and only add the limit if it's set - i.e. an integer or not null.<\/p>\n\n<p>It means the value could be either an integer or null, but I think the intent of the code is more explicit.<\/p>\n\n<p>This would make the code look like this:<\/p>\n\n<pre><code class=\"language-php\">if (is_int($limit)) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\n<p>Whilst this is clearer, it doesn't cover all use cases.<\/p>\n\n<p>Presumably, the limit should only be a positive integer.<\/p>\n\n<p>It wouldn't make sense to set a negative number as the limit or, as the unlimited value is <code>null<\/code>, setting it to zero.<\/p>\n\n<p>This is the end code I'd likely write:<\/p>\n\n<pre><code class=\"language-php\">if (is_int($limit)) {\n if ($limit < 1) {\n throw new InvalidArgumentException('A limit must be a positive integer.');\n }\n\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\n<p>If the limit is not an integer, nothing happens.<\/p>\n\n<p>It throws an Exception if the value is invalid - i.e. less than one.<\/p>\n\n<p>The limit is applied if the limit is greater than or equal to one.<\/p>\n\n<p>While it's more complex as there are more checks to perform and different types in use, I think this is clearer and easier for someone reading or implementing the code to understand what it does and use it correctly.<\/p>\n\n ",
"format":"full_html",
"processed":"\n <p>Something I've seen in code is the unclear use of zero when adding limits, such as loading items from a database.<\/p>\n\n<p>In some instances, setting zero will return all items - essentially, an 'unlimited' value in disguise - rather than returning no results, which is what I'd expect.<\/p>\n\n<p>I imagine the code looks something like this:<\/p>\n\n<pre><code class=\"language-php\">if ($limit > 0) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\n<p>If <code>$limit<\/code> is greater than one, add it to the query.<\/p>\n\n<p>For me, using <code>0<\/code> as the unlimited value doesn't seem like the best option.<\/p>\n\n<p>I'd prefer to use a <code>null<\/code> value as the default and only add the limit if it's set - i.e. an integer or not null.<\/p>\n\n<p>It means the value could be either an integer or null, but I think the intent of the code is more explicit.<\/p>\n\n<p>This would make the code look like this:<\/p>\n\n<pre><code class=\"language-php\">if (is_int($limit)) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\n<p>Whilst this is clearer, it doesn't cover all use cases.<\/p>\n\n<p>Presumably, the limit should only be a positive integer.<\/p>\n\n<p>It wouldn't make sense to set a negative number as the limit or, as the unlimited value is <code>null<\/code>, setting it to zero.<\/p>\n\n<p>This is the end code I'd likely write:<\/p>\n\n<pre><code class=\"language-php\">if (is_int($limit)) {\n if ($limit < 1) {\n throw new InvalidArgumentException('A limit must be a positive integer.');\n }\n\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\n<p>If the limit is not an integer, nothing happens.<\/p>\n\n<p>It throws an Exception if the value is invalid - i.e. less than one.<\/p>\n\n<p>The limit is applied if the limit is greater than or equal to one.<\/p>\n\n<p>While it's more complex as there are more checks to perform and different types in use, I think this is clearer and easier for someone reading or implementing the code to understand what it does and use it correctly.<\/p>\n\n ",