Refactoring by Martin Fowler is one of my favourite technical books. There's so much wisdom in there. I own the first edition, and it was only last week that I learnt about the second edition, published in 2019 😅 A free chapter with an example that showcases several Refactorings can be downloaded here 👇 https://lnkd.in/dY_cMHCZ Emily Bache created a repository with the code for this example, together with some tests. The code it's available in several languages: https://lnkd.in/dEBV9tzg And, since I thought it was a great idea and the example wasn't available in Ruby, I decided to contribute with a PR: https://lnkd.in/dB_zgyT3
Pablo Alonso García’s Post
More Relevant Posts
-
The next code smell I want to highlight is one of my personal pet peeves, Primitive Obsession. This means using a primitive or String for a value just because it sort-of fits, rather than using a well-designed object. Yes, there is effort in creating the object, but that effort pays off many times over. What do I mean? How about using a float to represent money just because it has a decimal point. That *always* causes problems. You always have to deal with imprecision and rounding errors. You need a Money class that can store exact values, handle multiple currencies and perform conversions. Another horrible idea is to use a String (or even worse, an int) to hold a phone number. You often need to parse out a country code or an exchange by splitting the String or dividing the int. You can express these concepts directly in a TelephoneNumber class. The same goes for using String to represent an address. Use an Address object to hold a name (which should also be an object), a house number, street name, apartment number, city, province. Postal code and country. The same goes for dates, times, ranges and other types. Of course, Java has already defined classes for those. Any time you have a value in a primitive and keep having to split it apart to use it, you really need to define an object.The appropriate refactoring is “Replace Data Value With Object”. Sometimes, we use a primitive to represent a type code, which can also cause problems. You should use an enum or a class. If you don’t depend on the type code to affect behavior of the system, use “Replace Type Code With Class.” If you do use it for branching, then you can consider “Replace Type Code With Subclasses”, or “Replace Type Code With State/Strategy”. Another smell is storing parts of a value in an Array, then having to pick out pieces by knowing the indices of those pieces. If you find yourself doing this, you can use “Replace Array With Object”. Sometimes, you might have pieces of a value in individual primitive fields and parameters. In this case, you can put them together with “Extract Class” or “Introduce Parameter Object.” Shoe-horning complex values into a primitive is always a bad idea. You may think it is “easier” or “less work”, but it always comes back to bite you. You can find descriptions of the refactorings at https://lnkd.in/gdJjNQcF
To view or add a comment, sign in
-
🚀 I've just published Part 4 - 'Formatting' of my series, where I share my notes from the book 'Clean Code' by Robert C. Martin. This part highlights importance of code formatting and its impact on readability and maintainability. Discover best practices for structuring your code to make it cleaner and more understandable. Read it here: https://lnkd.in/dD9yGMcG #CleanCode
Clean Code : Part 4— Formatting
medium.com
To view or add a comment, sign in
-
Today's article: 3 Books every programmer should read. These were some of the most influential ones in my library. Check it out and let me know if you've read any of these and what you thought about them.
3 Books Every Programmer Should Read
sethwbarton.substack.com
To view or add a comment, sign in
-
An opportunity to participate at a book club where the author and core VM developer participates, along with many experts. This is not to be missed! Elixir in Action is a great introduction to both concurrent and sequential Elixir. Very well explained and a great learning experience. It’s rare to have industry leaders participate in an introductory book; this is one of those! https://lnkd.in/gupBqeK3 #elixir #erlang #FunctionalProgramming #DistributedSystem #BookClub #ProgrammingBook
🎉 Excited about Elixir? Join the Elixir In Action Book Club on Elixir Forum! 📚 Dive into "Elixir in Action, Third Edition" by Saša Jurić. Engage, learn, and discuss with fellow enthusiasts. Everyone’s welcome! 🔥 Use code elixclub24 for 45% off at checkout 🔥 🔗 Join the book club! https://mng.bz/ngy2 🔗 Get your book here 👉 https://mng.bz/v8G4 👈 #Elixir #Erlang #LearnElixir #ManningBooks #ElixirLang
Elixir In Action Book Club!
elixirforum.com
To view or add a comment, sign in
-
This week’s #FeaturedFriday spotlights Andrew Lombardi, coauthor (with Joseph Ottinger) of “Beginning Spring 6, Second Edition: From Beginner to Pro.” He shared a bit about his writing experience and inspiration: Q: How would you describe the experience of writing your book? “The latest edition of the book was certainly easier than the first given we had an existing structure and codebase to work from. We worked hard to ensure correctness and to inject some new topics that we only lightly covered in the first edition.” Q: Most tech authors write their books alongside work/life responsibilities—how did you find the time to complete your book? “As the head of a consulting company, I manage a team of 10 engineers so the time shows up in small pockets and you have to steal time where you can.” Q: How did you decide on the structure & organization of the book? “Working together with Joe we assessed the landscape of what was new in #Spring and current technologies and usefulness and decided on what we felt would be the best items to cover. As with the previous edition of the book we wanted to write a book that was conversational, fun to read, and not only taught you about Spring but how to think more like a programmer.” Q: What role do you think your own perspective and experiences played in shaping the book? “Hopefully our perspectives and experiences played a large role in shaping the book and pulling together a great set of pages that are informative and fun.” For more on Andrew’s book: https://lnkd.in/eUHxxWBW Want to write a tech book for Apress? Start your own author journey here: https://lnkd.in/gcs-Fmf #Apressauthor #motivation #writing #tech #publishing #Spring6 #SpringFramework #Java
To view or add a comment, sign in
-
DAY 72: #100daysofcode #100daysofcodechallenge #100daysofcodingchallenge. JOURNEY IN REACT ✈ ..... In my exploration of React, I delved into the foundational aspects of CRUD operations, specifically focusing on the creation (C) and retrieval (R) functionalities. As part of this endeavor, I developed a Bookstore form, enabling users to augment a dynamic booklist. This form facilitates the addition of books by inputting essential details such as title, author, cover image, and a brief description. Repo Here: // https://lnkd.in/dZTf_gYS // #womenintech #womenintechafrica #codetrainafrica Codetrain
To view or add a comment, sign in
-
Methods that are too long make code really hard to understand, and are a bad code smell. A good rule of thumb says a method should be no more than 10-25 lines of actual code, not counting comments, white space or curly braces on their own lines. Any method longer than that is a good candidate for breaking up into shorter methods. It matters how you do it, though. I once heard a (possibly apocryphal) story about a developer who cut his long method into arbitrary 20-line chunks named sub1, sub2, etc. This would not have been useful, even if it did satisfy the letter of the rule. It certainly did not clarify the code. A good method reads like a paragraph with the steps clearly named for their purpose. All these steps should be at the same level of abstraction. Don’t mix low-level statements with higher-level method calls. It is fine to extract a single statement into a method if the name of that method makes the code more clear. Loops are often good candidates for extracting into a method of their own. The way to split a method is to read it through, separating out the “things” it is doing. Then extract each of those “things” into a method that is named to describe that “thing”. If some of the extracted methods are still too long, rinse and repeat, breaking them the same way. One sure sign you need to extract a method is when you feel the need to write a comment describing what is happening. Comments can get out of date or out of position, but if you extract a well-named method you are durably describing the code. When breaking down a method like this, you might run into a problem where you have to pass a lot of parameters, which can be hard to follow. Martin Fowler suggests several possible refactorings to help, like “Replace Temp With Query”, “Introduce Parameter Object” and “Preserve Whole Object”. In some cases, you might need “Replace Method With Method Object”. When you have a complicated conditional, you often can simplify it with “Decompose Conditional”. You can find descriptions of all the refactorings mentioned above at Martin Fowler’s site, https://lnkd.in/gdJjNQcF. Everyone should bookmark that site and be familiar with the refactorings he describes. Your code will be better for it.
Encapsulate Collection
refactoring.com
To view or add a comment, sign in
-
The next code small is Refused Bequest. This is when a subclass decides not to use some behavior "bequeathed" from a superclass. Fowler says, and I agree, that this is a *very* faint smell, and very likely not worth addressing. Some OO people insist this means that the class hierarchy is wrong and you need to "Push Down Field" and "Push Down Method" into the subclass that actually uses the behavior. But what if multiple children need it? Then you would need to push it into an intermediate subclass, and make those children inherit from that. You might want to do that, but it complicates the inheritance hierarchy, and might not be worth it. As with a lot of code smells, this is a matter for your own judgment. You can find descriptions of the refactorings at https://lnkd.in/gxmaYiRv
Encapsulate Collection
refactoring.com
To view or add a comment, sign in
-
We have a few code smells left. Today, let's talk about Incomplete Library Class. This isn't a problem with *your* code, but is with a library class you are using. That class is just missing some functionality you need. Sometimes, library developers forget something, or you might have a use case they didn't anticipate. If this were your code, you could just add the method. But you might not be willing or able to modify the library. This is especially true when you update the library frequently and would need to add your method(s) with each update. If the function you need is only called from one client class, you can use "Introduce Foreign Method" to add a method to that client class that adds that function. The example Fowler cites is a Date class, where you *really* need a method to get the next date for the one specified. You would add a private Date getNextDate(Date date) method with that capability to the client class. If you need to add several functions, or even one that is called from several client classes, then you would use "Introduce Local Extension". This involves creating either a subclass or a wrapper for the library class, with all the methods you need. Then you would use the extension class in place of the library class wherever you need it. Descriptions of the refactorings are at https://lnkd.in/gxmaYiRv
Encapsulate Collection
refactoring.com
To view or add a comment, sign in
Software Crafter | Problem-Solver | Pragmatist | LinkedIn Advisor
7moI’m reading the second edition. The jury is still out on whether I’ll like the new JavaScript examples as much as the Java in the original.