According to https://softwareengineering.stackexchange.com/a/180616/432039 suggested, I know the answer advised "auto" should be used instead of the actual type when declaring variables.
However, the question is about the readability of code before and after using auto, but I found the biggest problem of "auto" is not the readability, instead "auto" hinders me to search the code that I need to fix by using keyword like 'MyClass ' in variable declaration like "MyClass myclass = ...".
For example, suppose I have a c++ mobile game project that needs to load user settings using DTO "UserSettings", and UserSettings may be loaded from game server, mobile device, or edit by user at some pages, eg (ignore .h and class definition for simpler code):
OldUserLoginPage.cpp
OldUserLoginPage::loadUserData(){
//some code call http
UserSettings userSettings=loadUserDataResponse.userSettings;
//some other code
}
UserResumePage.cpp
UserResumePage::viewDidLoaded(){
//some init code
UserSettings userSettings=AppData::getUserSettings();
//some other init code
}
SettingsPage.cpp
SettingsPage::onLoadDefaultButtonPressed(){
//some code
UserSettings userSettings=DefaultUserSettings::getInstance();
//some other code
}
When the game works fine over few months, suddenly a use reports that the user settings seems failed to save, and I'm quite sure I would forget about which .cpp contain UserSettings, and also I would forget about loadUserDataResponse.userSettings, AppData::getUserSettings() and DefaultUserSettings::getInstance(), except I know I can search for keyword "UserSettings " to list all parts of codes that handle user settings.
However, if I just use "auto" instead of UserSettings, I can't search for keyword "UserSettings " to find which part of codes that use UserSettings. Although I may also able to find the related code by browsing each .cpp individually after using auto, search for keyword "UserSettings " helps me a lot faster to find and fix the bug in the code. Also when a new teammate enters the team and start maintaining the code, the teammate may be unable to know which actual page refers to ???Page.cpp immediately, but the new teammate can search for keyword "UserSettings " to find which part of the code in the project that may contain the bug.
Also even in a single .cpp, I find the type in variable declaration may also help me to find the related code quickly, for example, suppose there is a page about buying items and magic stones:
ShopPage.cpp
ShopPage::method1(){
//some code
this->updateTotalCost();
}
ShopPage::method2(){
//some code
this->updateTotalCost();
}
.
.
.
ShopPage::updateTotalCost(){
//some other code
double totalCost=item1.quanity*item1.price+item2.quanity*item2.price+...
}
Also after some day, suppose some users find the cost seems calculate wrongly, I forgot which function and variable name handles the cost calculating, except I remember the cost must be "double" type, then I can search for "double " to jump to the related code to investigate the problem.
While I agree "auto" may help me to write the code faster, code is being read and maintained more often than being written, so I would rather write c++ as if "auto" never exists, so that I can search for the code by variable declaration with specific type in order to find and fix the code quickly. So my question is, is the reason above the rationale to avoid using "auto"?
auto
(implicit typing) is just yet another form of type erasure -- just like parametric polymorphism (templates), subtyping, function overloading, etc. It has nothing to do with the speed of writing code, it's about the use of abstraction and decoupling from dependencies. The same arguments which arise againstauto
are often also levelled against the use of abstract base classes, subtype polymorphism, template parameters and overloaded function names. (Typically covered by developer tooling and the use of unit testing. things like Navigation are usually handled by a decent IDE).