
Code taste: How do you develop the ability to write clean and succinct code? - vladimirfomene
I will like hear your ideas on how to write succinct code. Today I was trying to solve the following problem on    
http:&#x2F;&#x2F;codingbat.com. I solve it and my solution worked but I know it was not clean.
Here is the problem;<p>Given a string, if the first or last chars are &#x27;x&#x27;, return the string without those &#x27;x&#x27; chars, and otherwise return the string unchanged.<p>withoutX(&quot;xHix&quot;) → &quot;Hi&quot;
withoutX(&quot;xHi&quot;) → &quot;Hi&quot;
withoutX(&quot;Hxix&quot;) → &quot;Hxi&quot;<p>Here is my solution(quick code):<p>public String withoutX(String str) {
  &#x2F;&#x2F;handles single x characters
  if(str.length() == 1 &amp;&amp; str.charAt(0) == &#x27;x&#x27;){
    str = &quot;&quot;;
  &#x2F;&#x2F;handles empty strings
  }else if(str.length() == 0){
    str = &quot;&quot;;
    &#x2F;&#x2F;handles all other cases
  }else{
    if(str.charAt(0) == &#x27;x&#x27;){
      str = str.substring(1);
    }<p><pre><code>    if(str.charAt(str.length() - 1) == &#x27;x&#x27;){
      str = str.substring(0, str.length() - 1);
    } 
  }
  
  
  return str;</code></pre>
}<p>Here is the solution from coding bat:
public String withoutX(String str) {
  if (str.length() &gt; 0 &amp;&amp; str.charAt(0) == &#x27;x&#x27;) {
    str = str.substring(1);
  }<p><pre><code>  if (str.length() &gt; 0 &amp;&amp; str.charAt(str.length()-1) == &#x27;x&#x27;) {
    str = str.substring(0, str.length()-1);
  }
  
  return str;
  
  &#x2F;&#x2F; Solution  notes: check for the &#x27;x&#x27; in both spots. If found, use substring()
  &#x2F;&#x2F; to grab the part without the &#x27;x&#x27;. Check that the length is greater than 0
  &#x2F;&#x2F; each time -- the need for the second length check is tricky to see.
  &#x2F;&#x2F; One could .substring() instead of .charAt() to look into the string.
}</code></pre>
======
klibertp
You might want to edit your post prepending _all_ lines of code with an indent
(I use four spaces, but even two I think work), ie.

    
    
        public String withoutX(String str) { //handles single x characters
            if(str.length() == 1 && str.charAt(0) == 'x'){
                str = ""; 
            //handles empty strings
            }else if(str.length() == 0){
                str = ""; 
            //handles all other cases
            }else{
                if(str.charAt(0) == 'x'){
                    str = str.substring(1);
                }
    
                if(str.charAt(str.length() - 1) == 'x'){
                    str = str.substring(0, str.length() - 1);
                } 
          }
          return str;
        }
    
    
        public String withoutX(String str) {
          if (str.length() > 0 && str.charAt(0) == 'x') {
              str = str.substring(1);
          }
    
          if (str.length() > 0 && str.charAt(str.length()-1) == 'x') {
            str = str.substring(0, str.length()-1);
          }
    
          return str;
    
          // Solution  notes: check for the 'x' in both spots. If found, use substring()
          // to grab the part without the 'x'. Check that the length is greater than 0
          // each time -- the need for the second length check is tricky to see.
          // One could .substring() instead of .charAt() to look into the string.
        }

------
brudgers
When I have this sort of question, I like to use
[https://codereview.stackexchange.com/](https://codereview.stackexchange.com/)
. It's like StackOverflow but for code review...with the good and bad that
being like StackOverflow implies. Basically, if the code works, it's likely to
appropriate.

Good luck.

~~~
vladimirfomene
Thanks.

------
ankurdhama
By thinking about the problem and not writing a single line of code until you
have the algorithm ready. When you start to write code, you end up thinking
about the solution of the problem in some sort of piece wise solution (AKA
first do this then that and so on) which inhibits you from thinking about the
problem holistically and hence you end up with lots of complex conditionals
and control flow structures.

~~~
vladimirfomene
Thanks for the feedback.

