from Hacker News

Code taste: How do you develop the ability to write clean and succinct code?

by vladimirfomene on 4/18/17, 8:04 AM with 5 comments

I will like hear your ideas on how to write succinct code. Today I was trying to solve the following problem on http://codingbat.com. I solve it and my solution worked but I know it was not clean. Here is the problem;

Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.

withoutX("xHix") → "Hi" withoutX("xHi") → "Hi" withoutX("Hxix") → "Hxi"

Here is my solution(quick code):

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;
}

Here is the solution from coding bat: 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.
}
  • by klibertp on 4/18/17, 8:34 AM

    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.
        }
  • by brudgers on 4/18/17, 4:02 PM

    When I have this sort of question, I like to use 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.

  • by ankurdhama on 4/18/17, 8:46 AM

    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.