Question

I hear a lot about keeping methods short and I've heard a lot of programmers say...

I hear a lot about keeping methods short and I've heard a lot of programmers say that using #region tags within a method is a sure sign that it is too long and should be refactored into multiple methods. However, it seems to me that there are many cases where separating code with #region tags within a method is the superior solution to refactoring into multiple methods.

Suppose we have a method whose computation can be separated into three rather distinct phases. Furthermore, each of these stages is only relevant to the computation for this method, and so extracting them into new methods gains us no code reuse. What, then, are the benefits of extracting each phase into it's own method? As far as I can tell, all we gain is some readability and a separate variable scope for each phase (which will help prevent modifications of a particular phase from accidentally breaking another phase).

However, both of these can be achieved without extracting each phase into its own method. Region tags allow us to collapse the code into a form which is just as readable (with the added benefit that we no longer have to leave our place in this file should we decide to expand and examine the code), and simply wrapping each phase in {} creates its own scope to work with.

The benefit to doing it this way is that we don't pollute the class level scope with three methods which are actually only relevant to the inner workings of a fourth method. Immediately refactoring a long method into a series of short methods seems to me to be the code-reuse equivalent to premature optimization; you are introducing extra complexity in order to address a problem which in many cases never arises. You can always extract one of the phases into its own method later should the opportunity for code reuse arise.

Thoughts?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All you should ever care about is for your code to be usable, not reusable. A monkey can transform usable code to reusable code, if there are any transformations to be done at all.

The argument "I need this only here" is poor, to put it politely. The technique you're describing is often referred to as the headlines technique and is generally frowned upon.

You can't test regions, but you can test true methods in isolation.
Regions are comments, not syntactic elements. In the worst case the nesting of your regions and your blocks contradict each other. You should always strive to represent the semantics of your structure with the syntactic elements of the language you are using.
After refactoring to methods, one no longer needs folding to read the text. One might be looking at source code in any tool that doesn't have folding, like a terminal, a mail client, a web-view for your VCS or a diff-viewer.
After refactoring to methods, the resulting method is at least as good as with regions, plus it is simpler to move the individual parts around.
The Single Responsibility Principle suggests that any unit should have one task and one task only. The task of the "main" method is to compose the "helper" methods to get the desired result. The "helper" methods solve a discrete, simple problem in isolation. The class containing all these methods should also only fulfill one task and only contain the methods related to that task. So the helper methods either belong into the class scope, or the code shouldn't be in the class in the first place, but at least in some global method or, better yet, should be injected.

Add a comment
Know the answer?
Add Answer to:
I hear a lot about keeping methods short and I've heard a lot of programmers say...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • Breaking it Down Problem One problem that I find in new Java programmers is a tendency...

    Breaking it Down Problem One problem that I find in new Java programmers is a tendency to put too much code in "main()". It is important to learn how to break a program down into methods and classes. To give you practice at this, I have a program that has too much code in main. Your job will be to restructure the code into appropriate methods to make it more readable. I will provide you with lots of coaching on...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot...

    JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot of points. Make sure that the program works because I had someone answer this incorrectly!) The primary goal of the assignment is to develop a Java based primitive editor. We all know what an editor of a text file is. Notepad, Wordpad, TextWrangler, Pages, and Word are all text editors, where you can type text, correct the text in various places by moving the...

  • CAPGEMINI: WHY CENTURY-OLD RFID TECHNOLOGY CAN DISRUPT MODERN RETAIL SUPPLY CHAINS Amazon’s recent announcement of additional...

    CAPGEMINI: WHY CENTURY-OLD RFID TECHNOLOGY CAN DISRUPT MODERN RETAIL SUPPLY CHAINS Amazon’s recent announcement of additional investments in Artificial Intelligence (AI) and drones in the UK should be a wake-up call for all retailers, from supermarkets to apparel stores. UK retailers can learn a lot from how their US counterparts are responding to Amazon’s innovations and the unexpected technology at the heart of their response. Amazon’s investment, paired with its physical store experiment, Go, shows that the technology company has...

  • There was no question about Carl’s genius. Seven years ago he decided to enter the competitive...

    There was no question about Carl’s genius. Seven years ago he decided to enter the competitive nightmare that the personal computer business had become. Although on the surface that appeared to be a rather non-genius-like move, the genius came in the unique designs and features that he developed for his computer. He also figured a way to promise delivery in only two days for the local and regional market. Other computer makers also had rapid production and delivery, but they...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • Hello! Could you please write a 6 paragraph summary (5-6 sentences each paragraph) of the below?...

    Hello! Could you please write a 6 paragraph summary (5-6 sentences each paragraph) of the below? In the overview, if you could please describe the information in detail. Please have completed in 6 days if possible. Thank you! In 50 Words Or LesS .6TOC combines lean Six Sigma (LSS) and the theory of constraints (TOC) for bottom-line benefits . The method's metrics pyramids and communi- cations allow organiza- tions to retain gains and monitor benefits. · 6TOC goes beyond fac-...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • Clinical trials are usually conducted in phases that build on one another. Each phase is designed...

    Clinical trials are usually conducted in phases that build on one another. Each phase is designed to answer certain questions. Knowing the phase of the clinical trial is important because it can give you some idea about how much is known about the treatment being studied. There are pros and cons to taking part in each phase of a clinical trial. Phase I clinical trials: Is the treatment safe? Phase I studies of a new drug are usually the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT