Warning: Undefined array key " weight" in C:\xampp\htdocs\prueba\add.php on line 30 Warning: Undefined array key " size" in C:\xampp\htdocs\prueba\add.php on line 29 Warning: Undefined array key " productType" in C:\xampp\htdocs\prueba\add.php on line 28 Warning: Undefined array key " price" in C:\xampp\htdocs\prueba\add.php on line 27 Warning: Undefined array key " name" in C:\xampp\htdocs\prueba\add.php on line 26 This makes scaling your script much easier, cleaner, and more concise.Warning: Undefined array key " sku" in C:\xampp\htdocs\prueba\add.php on line 25 In contrast, a switch block will require 3 new lines of code for each new allowance. Now whenever you want to extend your error array-hydrating function to accommodate new types, you ONLY need to add a single line of code to the lookup ( ERROR_LOOKUP) - you never need to touch the processor. IOW, you won't need to pass the lookup into your function as an argument or use global. This will benefit you if plan to write a custom function to encapsulate the processing code. ![]() The processing part will be built "generally" so that it will appropriately handle incoming data based on the "specific" data in the lookup array.īy declaring the lookup as a constant (because it won't vary - it doesn't need to be a variable), the lookup will enjoy a global scope. This will allow you to separate your "processing" code from your "config" code. How can you make your code more manageable? I recommend a lookup array. If you are planning on presenting this to the end user as a means to communicate on a "human-friendly" level, then abandon the array-like structure and write plain English sentences. Also, the idea to separate the configuration from the processor code is a valid one, but for now it only seems to complicate the code (PS: mickmackusa updated his answer and it looks a lot better now).īefore I get started with the script polishing, I just want to voice that I don't think it makes sense to bloat/hydrate your otherwise lean data storage with redundant text. A lookup array can make sense, for instance when you're using multiple languages. I also had a look as "mickmackusa" solution. This might not be to everyones liking, but I think it is acceptable or even slightly easier to read. ![]() Like this: function convertToErrorMessages($errors) One thing I sometimes do when there are many nested braces, is leaving out braces that will never be useful. I also don't like long long lines, so I split those. This prevents code repetition, especially when you have many error types. Note that I also collect all the value messages before assigning them to the error messages array. " - other functions have dependencies on it" $valueMessages = "$value is a required $property". $valueMessages = "$value is not a valid $property" So, I would write this as: function convertToErrorMessages($errors)įoreach ($errors as $errorType => $error) Also $errors does contain errors, but more precisely it contains error messages. Abbreviated variable names don't make your code easier to read. Seen in isolation variable names like $e, $array and $v don't really convey what they contain. Names that make clear what a variable contains. I agree with what "Your Common Sense" said, but I would like to have better variable names. $errors = "$v is a required $key - other functions have dependencies on it" If you initialize it as $errors = you will always know it's an empty array. If you just mention it, as $errors it will keep the previous value. Although in the present code the $errors variable doesn't exist before the loop, the code could evolve over time, and there could be the possibility that a variable with the same name will be used above. If you need an empty array then you must initialize an empty array. Variable interpolation is also more natural to read, in my opinion.Īlso, variables must be explicitly initialized, just a mention is not enough. I would only make a minor brush-up, removing the unnecessary default clause and make the proper indentation. And making it more concise will likely make it more cryptic as well. As any explicit code, these loops are quite straightforward to follow. ![]() Here is a screenshot of the output array: Is there a more concise way to generate the error messages? While this achieves the desired outcome, the nested foreach loops in the generateErrorMessages() method seem onerous and difficult to read. ' - other functions have dependencies on it' I'm working with an input array, which is 3 levels deep, and creating error message strings accordingly in a separate output array.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |