What is the code supposed to be doing? Some code is obviously missing, since you're assigning to variables that aren't declared in the code snippet (url, lManga). Are they class members? We need more context to provide any help. It can't be reorganized without a more complete picture.d[-_-]b;43055737 said:need a little help with refactoring, essentially killing my ugly/spaghetti code
What techniques do you guys use...Code:lManga = new Manga(); url = new URL(ele.get(i).attr("abs:href")); lManga.setTitle(ele.get(i).text().replaceAll("\"", "").replace("'", "")); lManga.setIndexLink(url.toString()); System.out.println(lManga.getTitle()); Document docdata2 = Jsoup.parse(url, 5000); String ogImage = getMetaTag(docdata2, "og:image"); Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(ogImage); if (m.find()) { lManga.setID(Integer.valueOf(m.group())); }
As for your question, just follow the SOLID principles. Basically, don't just throw a bunch of code into random places. A class should be responsible for one logical thing. A method should only do one thing, and its name should clearly describe what that one thing is. You should make use of composition when using other classes. This just means that if a class needs to do something that is the responsibility of another class, it should take an instance of that class in its constructor. For example, a ConfigurationParser class needs to validate the configuration, via a ConfigurationValidator class. It should just take an instance of ConfigurationValidator in its constructor.
A lot of this is overkill for very small projects. It's mostly about separating your concerns and maintaining a well-organized project. Some rules of thumb are like, is your method more than ~7-10 lines? (this number varies depending on who you ask), then it's probably doing more than one thing. Are you having trouble naming a method without using the word "and"? It's probably doing more than one thing.
Sorry if I rambled a bit. But if you post more of your code I'm sure people would be happy to give their opinions on how it could be refactored.