patternphpMinor
Improving the readability and efficiency of parser code
Viewed 0 times
theparserreadabilitycodeandefficiencyimproving
Problem
Recently, I had a test and I wrote this code, but they didn't like my code because it was "deplete and nested".
In order to improve my skills, what would be good ways to make this more readable and less deplete and nested?
Exercise
The
titleCase
A string is considered to be in title case if each word in the string is either:
The
Examples
This is my code:
```
class StringUtil {
/**
* Verify a string of braces
*
* Ta
In order to improve my skills, what would be good ways to make this more readable and less deplete and nested?
Exercise
The
validBraces method takes a string of braces as a single parameter and determines if the order of the braces is valid. All input strings will only consist of open curly braces {, closed curly braces }, open parentheses (, closed parentheses ), and open brackets [ and closed brackets ].- A string of braces is considered valid if all open braces are matched with their corresponding closed brace. For example:
(){}[]and([{}])would be considered valid, while(},[(]), and[({})](]would be considered invalid.
- An empty string is considered valid.
- A non-string input is considered an invalid input. The method should throw an
InvalidArgumentExceptionin this case.
titleCase
A string is considered to be in title case if each word in the string is either:
- capitalised: that is, only the first letter of the word is in upper case; or
- considered to be an exception and put entirely into lower case unless it is the first word, which is always capitalised.
The
titleCase method will convert a string into titleCase given an optional list of exceptions. The list of exception words will be given as an array of strings. The method should ignore the case of the exception word strings—it should behave in the same way even if the case of the exception word strings changes.Examples
php
// should return: 'Do Androids Dream of Electric Sheep?'
->titleCase('do androids dream of electric SHEEP?', array('do', 'an', 'of'));
php
//should return: 'Rendezvous with Rama'
->titleCase('RENDEZVOUS WITH RAMA', array('With'));
php
// should return: 'Stranger In A Strange Land'
->titleCase('stranger in a strange land');This is my code:
```
class StringUtil {
/**
* Verify a string of braces
*
* Ta
Solution
The code doesn't exactly match the question as stated.
A non-string input is considered an invalid input. The method should
throw an
The code does not throw an
All input strings will only consist of open curly braces
curly braces
brackets
The code checks for other characters in the string, which is unnecessary given this guarantee.
An empty string is considered valid.
But this comment makes it seem like the correct result for an empty string was unclear:
There is a canonical solution to this problem that makes one pass through the string, and uses a stack. A search for "balanced parentheses" will turn up hints/explanations, if you get stuck.
We don't need to test if
I think there is a simpler approach:
I don't write PHP, but this is my attempt:
A non-string input is considered an invalid input. The method should
throw an
InvalidArgumentException in this case.The code does not throw an
InvalidArgumentException for non-string inputs. For example, validBraces(array()) does not throw.All input strings will only consist of open curly braces
{, closedcurly braces
}, open parentheses (, closed parentheses ), and openbrackets
[ and closed brackets ].The code checks for other characters in the string, which is unnecessary given this guarantee.
An empty string is considered valid.
But this comment makes it seem like the correct result for an empty string was unclear:
/**
* I assume that empty string is valid because
* is the default value
* and you have in the test as valid
*/There is a canonical solution to this problem that makes one pass through the string, and uses a stack. A search for "balanced parentheses" will turn up hints/explanations, if you get stuck.
newExecption is misspelled. exceptionV and new are not great names.We don't need to test if
newExecption is empty.I think there is a simpler approach:
- convert the exceptions to lowercase
- convert the string to lowercase
- for each word in the string:
- if it's the first word or not an exception, convert the first letter of the word to uppercase
I don't write PHP, but this is my attempt:
$caseExceptions = array_map(strtolower, $caseExceptions);
$words = explode(' ', strtolower($input));
foreach ($words as $i => $word) {
if ($i == 0 || !in_array($word, $caseExceptions)) {
$words[$i] = ucfirst($word);
}
}
return implode(' ', $words);Code Snippets
/**
* I assume that empty string is valid because
* is the default value
* and you have in the test as valid
*/$caseExceptions = array_map(strtolower, $caseExceptions);
$words = explode(' ', strtolower($input));
foreach ($words as $i => $word) {
if ($i == 0 || !in_array($word, $caseExceptions)) {
$words[$i] = ucfirst($word);
}
}
return implode(' ', $words);Context
StackExchange Code Review Q#64324, answer score: 6
Revisions (0)
No revisions yet.