{ "uuid": [ { "value": "2be6f96b-eecb-4205-9ca9-b364196535ab" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:22+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "Is zero unlimited?" } ], "created": [ { "value": "2024-01-18T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:22+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2024\/01\/18\/is-zero-unlimited", "langcode": "en" } ], "body": [ { "value": "\n
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
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
I imagine the code looks something like this:<\/p>\n\n
if ($limit > 0) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\nIf $limit<\/code> is greater than one, add it to the query.<\/p>\n\nFor me, using 0<\/code> as the unlimited value doesn't seem like the best option.<\/p>\n\nI'd prefer to use a null<\/code> value as the default and only add the limit if it's set - i.e. an integer or not null.<\/p>\n\nIt means the value could be either an integer or null, but I think the intent of the code is more explicit.<\/p>\n\n
This would make the code look like this:<\/p>\n\n
if (is_int($limit)) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\nWhilst this is clearer, it doesn't cover all use cases.<\/p>\n\n
Presumably, the limit should only be a positive integer.<\/p>\n\n
It wouldn't make sense to set a negative number as the limit or, as the unlimited value is null<\/code>, setting it to zero.<\/p>\n\nThis is the end code I'd likely write:<\/p>\n\n
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\nIf the limit is not an integer, nothing happens.<\/p>\n\n
It throws an Exception if the value is invalid - i.e. less than one.<\/p>\n\n
The limit is applied if the limit is greater than or equal to one.<\/p>\n\n
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
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
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
I imagine the code looks something like this:<\/p>\n\n
if ($limit > 0) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\nIf $limit<\/code> is greater than one, add it to the query.<\/p>\n\nFor me, using 0<\/code> as the unlimited value doesn't seem like the best option.<\/p>\n\nI'd prefer to use a null<\/code> value as the default and only add the limit if it's set - i.e. an integer or not null.<\/p>\n\nIt means the value could be either an integer or null, but I think the intent of the code is more explicit.<\/p>\n\n
This would make the code look like this:<\/p>\n\n
if (is_int($limit)) {\n $query->range(0, $limit);\n}\n<\/code><\/pre>\n\nWhilst this is clearer, it doesn't cover all use cases.<\/p>\n\n
Presumably, the limit should only be a positive integer.<\/p>\n\n
It wouldn't make sense to set a negative number as the limit or, as the unlimited value is null<\/code>, setting it to zero.<\/p>\n\nThis is the end code I'd likely write:<\/p>\n\n
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\nIf the limit is not an integer, nothing happens.<\/p>\n\n
It throws an Exception if the value is invalid - i.e. less than one.<\/p>\n\n
The limit is applied if the limit is greater than or equal to one.<\/p>\n\n
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 ",
"summary": null
}
]
}