There was a Big difference between what functions does and what class does.
Lets I will explain it from very scratch.🙂 (only about imperative)
Programming history ,we all know started with straight basic commands (eg-:Assembly).
Next Structured programming came with Flow controls (eg-: if,switch,while,for etc)
This paradigm gives programmers to control the flow of program effectively and also its minimize number of code lines by loops.
Next Procedural programming came and which groups instructions into procedures (funcions).
This gave Two major benefits for programmers.
1.Group statements(operations) into separate blocks .
2.Can Reuse these blocks.(functions)
But Above all paradigms did not give a solution for Managing applications.
Procedural programming also can only use for small scale applications.
That cannot be use for develop large web applications(eg-: banking, google,youtube, facebook,stackoverflow etc), cannot create frameworks like android sdk ,flutter sdk and lot more......
So engineers do lot more research to manage programs proper way.
Finally Object Oriented Programming comes with all the solution for managing applications any scale.(from hello world to Trillion of people using system creation eg-google,amazon, and today 90% of applications).
In oop all applications are build around Objects.It means application is a collection of these objects.
so objects are the basic building for any application.
class (object at runtime) group data and functions related to those variables(data).
so object compose of data and their related operations.
[Here i'm not going to explain about oop ]
👉👉👉Ok Now Lets coming for flutter framework.👈👈👈
-Dart support both procedural and oop But ,Flutter framework completely build by using classes(oop). (Because large manageable framework cannot create using procedural)
Here I will create list of reasons they use classes instead functions for making widgets.👇👇👇
1 - Most of times build method (child widget) call number of synchronous and asynchronous functions.
Ex:
- To download network image
- get input from user etc.
so build method need to keep in separate class widget (because all other methods call by build() method can keep in one class)
2 - Using widget class you can create number of another class without writing same code again and again (** Use Of Inheritance** (extends)).
And also using inheritance(extend) and polymorphism (override) you can create own custom class.
(Down below example, In there i will customize (Override) the animation by extending MaterialPageRoute (because its default transition i want to customize).👇
class MyCustomRoute<T> extends MaterialPageRoute<T> {
MyCustomRoute({ WidgetBuilder builder, RouteSettings settings })
: super(builder: builder, settings: settings);
@override //Customize transition
Widget buildTransitions(BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
if (settings.isInitialRoute)
return child;
// Fades between routes. (If you don't want any animation,
// just return child.)
return new FadeTransition(opacity: animation, child: child);
}
}
3 - Functions cannot add conditions for their parameters ,But using class widget's constructor You can do this.
Down below Code example👇 (this feature is heavily used by framework widgets)
const Scaffold({
Key key,
this.bottomNavigationBar,
this.bottomSheet,
this.backgroundColor,
this.resizeToAvoidBottomPadding,
this.resizeToAvoidBottomInset,
this.primary = true,
this.drawerDragStartBehavior = DragStartBehavior.start,
this.extendBody = false,
this.extendBodyBehindAppBar = false,
this.drawerScrimColor,
this.drawerEdgeDragWidth,
}) : assert(primary != null),
assert(extendBody != null),
assert(extendBodyBehindAppBar != null),
assert(drawerDragStartBehavior != null),
super(key: key);
4 - Functions cannot use const and Class widget can use the const for their constructors.
(that affect the performance of the main thread)
5 - You can create any number of independant widgets using same class (instances of a class/objects)
But function cannot create independant widgets(instance), but reusing can.
[each instance has their own instance variable and that completely independant from other widgets(object), But function's local variable is dependant on each function call* (which means ,when you change a value of a local variable it affect for all other parts of the application which use this function)]
There were many Advantages in class over functions..(above are few use cases only)
🤯 My Final Thought
So don't use Functions as building block of your application, use them only for doing Operations.
Otherwise it cause many unhandable problems when your application get scalable.
- Use functions for doing small portion of task
- Use class as building block of a application(Managing application)
Thanks for reading