The debate On Ruby On Rails in the Java Community is already fierce and there will be no signs of stopping in the future. Rails Supporters praised it for its amazing efficiency, which is about compared with Java development. As a Java programmer, your subconscious reaction is that you do not trust the high efficiency of any publicity, because you may have heard of this before, but you are very disappointed.
Java advocates are increasingly insisting that Ruby on Rails is a toy and cannot be scaled. It will generate bad code and can only develop simple applications. But with the increasing praise for Rails, it is usually from a trusted source), a more cautious task is to understand what Rails can do and bring its ideas back to the Java platform. In this article, we will explore the core feature that brings great efficiency to Rails, that is, the secret ).
Basic Principles of Rails
The Ruby on Rails framework is not a typical application development framework. David Heinemeier Hansson, the founder of Rails, often calls the framework a stubborn software and he prefers to break long-standing conventions. David made very philosophical decisions and strictly followed them throughout the framework. Core Ideas distributed across Rails include:
◆ Seamless integration: Rails uses the best features of the Ruby language intelligently. It extends Ruby, but it is hard to tell where Ruby ends and where Rails starts. You can also see that the persistence engine of Active RecordRails is well integrated with the Model-View-controller MVC framework. For example, you can write three lines of code to create a table and then generate a user interface for the model immediately.
◆ Conventions are better than configurations: to maintain good flexibility, the Java framework maintains a large number of common configuration files. Rails does not adopt this policy. It uses the common project directory structure and simple naming conventions for methods, classes, tables, and columns to infer which configurations have been configured in Java applications. The result is that the Rails application only needs a small part of the configuration code corresponding to the Java application, which is generally a tenth or more.
◆ Low repetition: Do not Repeat Yourself Don't Repeat Yourself, DRY) is a common term in the Rails community. The Rails Framework Committee uses methods that normally look like extensions of the Ruby language to abstract repetitive tasks. As you can see in the third article in this series, Rails's metaprogramming policy enables each line of code to execute more tasks.
◆ Instant feedback: Using Rails gives instant feedback on most of your work. After writing a line of code and saving it, your changes will be activated when you load the next Web page. After updating your database, the migration can instantly display changes to you.
Basic practice
The underlying concepts of Ruby on Rails are all based on practical experience. The Rails framework has grown from practical experience when developing popular Basecamp project management applications.
Focus on a certain field
The argument against its claim of high productivity is usually similar to this: if a good hammer is obtained, it is hard to find another hammer with a productivity of two times, not to mention increasing productivity by 5 to 10 times, because hammers have evolved for thousands of years. However, comparing Ruby on Rails with Java frameworks for various general purposes is not essential.
By fundamentally changing the nature of tools, we can increase productivity by 10 times in some aspects. Nowadays, professional manufacturers can use a nail gun to pin a lot of nails within the time when a hammer is inserted into a nail. Like a nail gun, Rails also has a special purpose. It is a framework specially written for a single field: A New Web application that supports databases.
I guess half of today's applications are database-based and Web-based applications. Therefore, Rails is a product specific to a specific field, but this field is very big and important. The specialization in this field has made Rails a great advantage and caused a great sensation. By focusing on projects in this field, Rails designers can choose shortcuts that are not available or should not be used by other frameworks. This specialization often loses flexibility for simplicity.
Web-based applications allow similar optimizations. When you know that an application is Web-based, you can understand the general structure of the application and the main components that may be needed. Because Rails focuses on Web-based applications, the following features are enhanced in Rails:
◆ Model-View-Controller: the MVC Framework of Rails is called Action Pack. It is customized for Web-based access and implements a well-known design strategy called Model 2. The Rails version has optimized integration between the Controller and view. This integration minimizes the configuration file) and automatically makes the Controller instance variables available to the view.
◆ Project directory structure: All Rails Applications have the same project structure. The directory is used to store application code, database configuration, and public static files, and scripts used to manage Web servers and perform Web-based functional tests.
◆ Architecture: by providing components used to generate application components, these components comply with common architecture objectives, such as page-level and fragment-level caches; two-layer design; for testing, development, and production environments) the out-of-the-box script of the Rails framework simplifies the architecture.
◆ Tool: Rails tool is specially used for Web. Log support, breakpointer, profiler) and test framework are all trimmed for Web-based applications and enabled for two-layer operations. But the nail gun will never replace the hammer, but we are stupid enough to completely replace it. A hammer can always do things that a nail gun cannot do. Rails will never be a tool for enterprise integration, object relationship ing, or full-stack Web Services. What you can expect best for Rails is that it is a specialized tool that can well meet its specific fields.