G
G
GrAl2017-07-06 13:23:10
OOP
GrAl, 2017-07-06 13:23:10

What to read and what to practice on, I can’t move from procedural to oop?

I started learning languages ​​from ZX Basic and ASM, then Pascal and Delphi.
Then I went to the administration and I write all the working scripts, even on PowerShell / PowerCli, according to the principle of procedural programming, I use OOP only when I work with the interface, unless of course it can be called OOP.
Now I'm studying python and ruby, and so far the basics, everything seems to be easy and understandable, but how it comes to objects is all a stupor.
Tell me where to start?

Answer the question

In order to leave comments, you need to log in

12 answer(s)
M
Maxim Fedorov, 2017-07-06
@qonand

Bertrand Meyer - Object-Oriented Design of Software Systems
Matt Weisfeld - Object-Oriented Thinking
Grady Booch - Object-Oriented Analysis and Design with Sample Applications

A
Alexander Filippenko, 2017-07-06
@alexfilus

I really understood OOP when I stopped understanding the code written an hour ago during the completion of the program.
I realized that without it in any way, I refactored what was at that time. Later, for another year and a half, I sawed that program, but there were no problems with readability. This is the question of why it is needed.
Only practice will help you learn. Try not to write interfaces and other high-level stuff for now. Take something simple. For example, write your own library for working with linear algebra.
2 classes - matrix and vector. And work with them. Input, output, multiplication, transposition, search for matrix determinant.
Add static methods, such as identity matrix generation.
Then you can write the solutions of the SLE in different ways. Think about how to reuse ready-made methods.
There will be inheritance.
Try writing your own simple ORM. A set of basic CRUD methods as an abstract class, and classes inheriting from it to work with specific tables.
If you do not get carried away with perfectionism, and the universality of the use of these classes, these 2 bicycles will be quite enough to figure out what's what with OOP in any language you choose.

I
Igor Kalashnikov, 2017-07-07
@zo0m

Read five pages from Java Philosophy, where at the beginning Eckel, as far as I remember, correctly analyzed this issue. I remember it helped me about 8 years ago)
Read from the second page:
www.rulit.me/books/filosofiya-java-read-180907-2.html
Do not be afraid of the word Java, first explanations in isolation from languages.

A
Adamos, 2017-07-06
@Adamos

Practice. Solve problems without letting yourself write a line outside of objects. The theory is dry...

L
legoboy90, 2017-07-06
@legoboy90

It's OK. I will say this - just try to solve the problem in a different way. If we take the C language as an example, there is a defined data type, a structure (in Psacal, this is sort of like a "record" is called).
That is, there is a data type with which we can put data about some subject of discussion - say, films: year, rating, name of the main actor, etc. It is important to understand the point itself here - just like you create an integer, you describe a data structure/records and wrap it in your derived type, but you say - "Hey compiler, I need not a number, a character, I need to create my new data type, inside it will be 3 integers and 1 string; then create 3 data object of my type.". T e you spawned a new data type. This is more convenient than running after a bunch of variables and figuring out how they relate to each other, now the information about !each! The movie is stored in its arranged location.
And let's go further - in languages ​​where there is coop, you can make a class - data + functions. This is a kind of new data type containing not only values, but also code (the same functions). In other words, we created a structure and stuffed functions into each object in order to store code in each object that can work with data. This is the class, on the basis of it you create new objects. What is the advantage? Let's take our films. For example, it will be very convenient to call a function from the object that leads the dialog for entering information about the movie and we will not miss anything - title, year, actor, rating. The object will not be created until we enter full information about it. Or let's say it will have a function where it will consider the rating as a failure if it is below 5. We don't need to pull the object every time, it analyzes the data itself and writes information to it.
I think that the theory of OOP does not make sense to read. Here is the same Python - Bill Lubanovich "Python. Modern programming style". Try it, it's written simply. In Python, everything is an object (in the book, from the first chapters, all this is shown and there is nothing terrible there). The most practical task is to connect to zabbix and try to get data out of it. Feel the OOP right away. The scheme is simple and stupid - you import the module for working with zabbix, then you initialize the object (zabbix host, login, password). And EVERYTHING! you have an object, you work through it, everything is already in it (if authentication failed or it is impossible to connect to the host, then the object will not be created, here is an OOP example for you, that the object has a code in addition to data). Or another example - the same Lubanovich has an example with creating a config file. (Config Parser). little has been written about him, stupidly googling. In short - the import of the module (some guy wrote it), and you just import the module where this "data type" (class) already exists, and on the basis of it you create objects and work with them. And then add the task to yourself - to collect data from objects into a heap and process them. This is where you will write the classes =)

S
Sergey Mirchenko, 2017-07-06
@KM-Brothers

I, at one time, was helped by the study of design patterns (patterns). I liked how Matt Zandstra presented this topic in the book "PHP: Objects, Patterns and Programming Techniques", starting from chapter 7.

T
ThunderCat, 2017-07-06
@ThunderCat

but as it comes to objects, everything is a stupor. Tell me where to start?

To begin with, understand what is wrong with objects for you?
In general, from the point of view of a proceduralist, an object is a set of functions and variables, simply combined into a single code (for example, a separate file with all variables and processing functions), related to a specific real world object. Instead of loading this file, the code will call the creation of an object (for example, vasea = new User()), and when creating it, you can automatically do something, for example, pull a user from the database with certain given data (for example, vasea = new User('vasea', 'pupkin')), through the constructor.
In addition, because the code gets into memory only when an object is created, the rule "we pay only for what we use" is observed, and not as with global variables and functions - we figure everything out, then we use 1-2 functions.
And the structure - ooh, it's such a thrill - all functions are tied to specific objects, if you are looking for a user login - you go to the user and only there, edit 1 line - and everything works everywhere at once! In short - you need to delve into and feel)

D
Dmitry, 2017-07-07
@Dit81

Now OOP is everywhere, it is better to look at the implementation of OOP in Python and Ruby ... You can also capture Java, there without objects there is nowhere at all ...

X
xfg, 2017-07-07
@xfg

Well, it's very simple. An object is an entity that has some state and behavior through which this state can be changed. An object can contain other objects, in which case it is called an aggregate.
Let's say you're writing software for a trucking company. The company probably at least has a car and a load. The cargo can be placed in the car. It is impossible to place the volume of cargo more than the capacity of the car.
You can model this example in php (the syntax may be incorrect!) in this way
(it is desirable to make the properties of objects private with access through getters so that the state of the object cannot be changed outside of it. Omitted, for the sake of simplicity).

class Car {
  public $id;
  public $capacity;
  public $payload;

  public function __construct(int $id, int $capacity, array $payload) {
    $this->id = $id;
    $this->capacity = $capacity;
    $this->payload = $payload;
  }
  public function assignCargo(Cargo $cargo) {
    if ($cargo->volume + $this->calculateOccupiedCapacity() > $this->capacity) {
      throw new DomainException('The capacity has been exceeded.');
    }
    $this->payload[] = $cargo;
  }
  private function calculateOccupiedCapacity() {
    return array_reduce($this->payload, function($accumulator, $cargo) {
      return $accumulator += $cargo->volume
    });
  }
}

class Cargo {
  public $volume;
  
  public function __construct($volume) {
    $this->volume = $volume;
  }
}

It should be understood what is happening even to a person from business. We take the car and try to load it. If the car is filled to the limit, the system informs about it. This is how a real business model is created using object-oriented design.
Much more difficult with infrastructure. I would like to save the car in storage, retrieve it from there and load it before loading it. This is where problems like impedance mismatch start.. Then, for sure, a change in one object will affect the state of another, and it will be necessary to build a system to work with domain events. And a lot of things like that. But this is about infrastructure. Designing a business model as an object-oriented one is not at all something prohibitively complicated. It is enough to remember that these should be, if possible, plain php objects that do not depend on third-party code of frameworks and libraries. In this case, the business model can be run on any framework/environment.
It is possible to esteem something about multilevel architecture. Much has been written about this in the context of Java. In the book Implementing domain-driven design , the author talks well about all application levels and how they interact with each other.

S
Saboteur, 2017-07-07
@saboteur_kiev

An object is some data (first of all), and methods that work with this data.
We are moving away from the fact that any data is in any global variables and come to the conclusion that all data is in variables inside the object. So access to them outside - through methods. Change is through methods. This is where the object comes in.
You need to understand why OOP is needed. It is not needed for small projects. In projects on which more than 1-3 people are working, it is already useful. And in large ones, it is necessary, otherwise, in principle, it will not be possible to organize the work of a large number of people on one program, without encapsulation, for which OOP was invented.

V
Vjatcheslav3345, 2017-07-07
@Vjatcheslav3345

You can try to show the "principle of maximum harm" in training - deliberately put yourself somewhere in strict conditions that require the use of OOP.
For example, you can start writing very short - from a dozen lines - useful at work programs on smalltalk (or self or Io ).
For example - to write something like simple log analyzers for calculating statistics - in the Smoltok library, for example, there are regular expressions and, in general, classes for working with text - which will become the basis of many of their text bicycles.
It's also useful to remember that OOP came out of procedural programming and you can look at modern implementations of various procedural languages ​​- for example, openeuphoria .

E
Egor, 2017-07-19
@egormmm

To understand something, you need to start with history.
History for OOP is Systems Theory. It is also the theoretical basis.
Start with her.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question