掲示板

Book: "Clean Code" by Robert Cecil Martin

thumbnail
8年前 に Bijan Vakili によって更新されました。

Book: "Clean Code" by Robert Cecil Martin

Expert 投稿: 375 参加年月日: 09/03/10 最新の投稿
I've read about 50 pages of book: "Clean Code", author Robert Cecil Martin.

A tagline for the book reads as follows:
"Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way."

I would recommend another tagline; one that's better targeted for Liferay:
"Your code is not a pile of spaghetti! In fact, it is ordered and pretty clean right now. Did you think you can make it cleaner still? Try these simple suggestions and find out!"

Here are some of the chapters from book:
  • Meaningful names - Use Intention-Revealing Names (check, Liferay does pretty good job of this)
  • Functions - Do One Thing, One Level of Abstraction per Function, Explain Yourself In Code (and "Comments Do Not Make up For Bad Code") - (I think Liferay does a pretty good job)
  • Formatting (I think Liferay has this covered quite well)
  • Objects and Data Structures (book doesn't cover this in much detail; though overall I think Liferay does well)


Anyways it may be worth read for some; since I know Liferay cares about the code; and some folks (like me) found this quite useful.
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Book: "Clean Code" by Robert Cecil Martin

Liferay Legend 投稿: 14914 参加年月日: 06/09/02 最新の投稿
Having spent a lot of time in the bowels of the Liferay code, I've developed the position that it's not as bad as many might think.

Many of the choices they've made will seem as poor or crazy or just plain bad from an outsiders perspective but often times it is forced on them by the nature of delivering a portal that must operate across the class loader boundary enforced by application containers.

This is the biggest contributor to the complexity of the Liferay code, IMHO, and expects use of static access classes (all of those XxxUtil classes) and the large amounts of redirects layered into the code.

With the coming of LR7 and OSGi and all portlet code running within the Liferay container, much of this layering and complexity can be eliminated.
thumbnail
8年前 に Andrew Jardine によって更新されました。

RE: Book: "Clean Code" by Robert Cecil Martin

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
I've also engaged in MANY conversations about the use of scriptlets in the JSPs that Liferay uses. The academic opinion on the subject I thnk is well know "BAD BAD BAD! WORST PROGRAMMING EVER!" .. and while there is some validity in the concerns, I think, like the class loader stuff, it is a necessary evil. From where I stand, the scriptlet allow the JSP hooking mechanism to be widely used to customize core features. Personally, I would rather some scriptlets in the JSP but have the facility to modify the 10% I need, rather than build the whole 100 from scratch every time!

As for the naming conventions? I think Liferay does an excellent job on the semantics front. In fact, I have adopted their naming convention almost entirely in my own projects -- for two reasons. One, I think the naming is logical and well understood, and two, I find it makes it easier to move between Liferay's core source itself and the extensions I write. From a rampup perspective, for new team members, it's a two birds one stone.

Most of the "bad code" I have seen in Liferay engagements has been from the work others have done to extend or alter the portal -- not the Liferay source itself.
thumbnail
8年前 に Bijan Vakili によって更新されました。

RE: Book: "Clean Code" by Robert Cecil Martin

Expert 投稿: 375 参加年月日: 09/03/10 最新の投稿
Having spent a lot of time in the bowels of the Liferay code, I've developed the position that it's not as bad as many might think.


Agreed; I've seen postings stating it's so. However, I enjoy reading the code; most of the time I read it easily. Some suggestions from Clean Code book are tough; they're really not practical in some parts; and yet, if done, rewards are tremendous. Here are some suggestions from the book (albeit recalling them from memory; and original text may be garbled; since I read the book with my biased mind):
  • Write short methods; 3 - 4 lines for each!
    There is a lot of discussion out there; that states LoC is useless measure; and others stating that makes readable; I think there is legitimate reasons; to have more than 3-4 lines in method; though I recently tried to refactor (a Java utility that calls git - to automate certain code review tasks); and found that it can be done easily; though I had a surge in number methods (and found ways to better group the functions); and some method names became rather long.

    I first wasn't keen on the short methods; though book suggests also the following:
  • One level of abstraction per function
    And this made a lot of sense to my mind; it just makes the code much more readable.
  • No more than 2 params to a function! Rarely should have 3 params for function!


I would love to write a lot more on this; though have to get going with some more chores.

With the coming of LR7 and OSGi and all portlet code running within the Liferay container, much of this layering and complexity can be eliminated.


Though I have to say that this made my day; since that is baggage that could really shed.

I'll reply more when I get the chance to; thanks for insightful comment to you both.
thumbnail
8年前 に Andrew Jardine によって更新されました。

RE: Book: "Clean Code" by Robert Cecil Martin

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
4 lines of code in a method! Is that even possible (except for scenarios where you are providing overloaded versions)?

I read once many many years ago that "a good guide" was to keep it to 20 lines. That seemed pretty reasonable to me at the time. I also tried at one point to keep the methods tight enough that you could read the entire thing on one screen without scrolling.

Sometimes there are just cases where I don't think any of these guiding principles will work and really you are left with just common sense to rule the roost. I think as long as the naming convention is clear, simple and obvious then the code should be acceptable. For example, instead of --


public static void saveFileToDisk(String path, File fileToSave);
public static void saveFileToURL(URI uri, File fileToSave);


.. which is the sort of thing I see often, I would be more inclined to push for ...


public static void save(String location, File file);
public static void save(URI location, File file);


The type of save you want to perform should be obvious from the parameters, not necessarily the method name. Prior to speding a lot of time deep in Liferay's code I might have been inclined to do something similar to the first example, but Liferay, I think, does it more like the second 90% of the time. To me, that makes the code easily understood and easier to read.

Anyway, I suppose this thread could go one forever. The clean coding series is definitely great though and certainly worth a read (or watch -- they have videos as well I believe). No doubt any programmer can find some value in it.
thumbnail
8年前 に Jack Bakker によって更新されました。

RE: Book: "Clean Code" by Robert Cecil Martin

Liferay Master 投稿: 978 参加年月日: 10/01/03 最新の投稿
wrtie clean code... got it