What is an object and how is it different than a variable? [closed] - oop

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I am a structured programmer and i'm trying to learn OOP by creating an MVC app using Classes.
I still don't understand one thing. What is an object and how is it different than a variable?

A variable holds a single piece of data while an object holds many variables and methods that act on those variables.
Since you are a beginner you can go through this tutorial
Hope this helps

Have you read up on the subject?
http://php.net/manual/en/language.oop5.php

Best way to learn is to see how others do it, check out the online docs for some popular oop php projects:
Zend
Symfony
CakePHP
There are many other examples out there, but these should get you started in the right direction.

Some basic concepts you need to understand:
A variable is a symbolic reference to data stored in memory. In the simplest case it holds a scalar value (a simple value like "12"), but can also an address to an object (see below).
An Object is the actual data stored in memory, but unlike a simple variable it can represent both data and functionality (methods) that acts on that data.
A Class is a template for an object. It contains a definition for the types of data that will be stored and the code for the methods. Think of it like a recipe for an object, but it is not itself an object.
Don't get confused by the other (Accepted) answer, by the distinction of having multiple values. That really isn't the distinction between a class and object. Structured data types (structs), for example, can also hold multiple values, but are not objects by the strictest definition.

Related

OOP: method name classification [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Lately I have been struggling with method names in OOP and I decided to sort that out. For that purpose, I am trying to classify names of methods from natural language point of view. So far, I have figured out these categories:
1] Commands:
elem_list.append('x')
bank_account.deposit(50)
game.get_score()
append, deposit, get_score are commands here. You ask objects to do something (or ask interpreter to do something with them - depends on point of view). These methods contain a verb in various forms: just verb, verb + noun, verb + adjective + noun, sometimes noun + verb (to further clarify meaning of the verb). Commands are probably the most common names.
2] Queries:
connection.is_open()
snake.is_dead()
window.can_hide()
These are not so common. Their form is passive-verb + adjective (this form can be surely described better, I am not a native English guy). Basically, here you query about a state of an object.
The following are categories I am not sure about because I haven't really seen lots of method names like that (it can be my limited experience though):
3] Declarations:
button.widget_selected(event)
window.screen_changed(screen)
Here you notify an object that something has happened and expect it to do its job. The method is basically an event handler. The form is usually something like noun + passed-tense-verb. I am unsure about this category because you can transform it into a command just by prepending a verb e.g. handle: button.handle_widget_selected(event) which seems to be more natural when calling the method.
4] Noun-names
snake.crash_animation()
game.introduction()
I don't really like these because I think nouns should be reserved for data. And they can be transformed into the first category simply.
So my question is if you somehow agree with this classification and whether you consider names in the third and fourth category good or bad with respect to OOP paradigm.
I think you are looking for a style guide or coding standard.
e.g.
PEP-8 (common referred Python style guide)
Google JS styleguide
Having said that, imo it's better to be consistent throughout your coding than mixing in a style guide (stick to what you started with or refactor everything). Having your own scheme is fine, but you'd rather spend time on productivity.

Business logic plus data, or separate the two? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
My understanding is that the fundamental tenant of OO design is that one should focus on modeling a class as the union of code and data. In day to day development, however, I tend to separate all of my business logic into classes of their own. The 'data' ends up being in tightly controlled POCOs/DTOs with basically no real code or logic. I then instantiate a business logic class and pass in POCOs to each method whenever I want an operation to occur.
But this feels like two separate approaches. In fact, the latter approach seems to be at odds with the purpose of OO!
I supposed I've always kept the two things separate since business logic may function on multiple POCOs. Plus, not forcing validation on the data in the POCOs made it easier to unit test since it seemed simpler to prepare the POCO for a test (no need to worry about internal class state, encapsulation, etc). Now that I'm looking back on those reasons, they seem weak.
I'm looking for a comparison/contrast of the two approaches. Specifically, why does it seem that making 'dumb' POCOs is the way to go these days? Why not just stick the data and the business logic into a single class? Are we abandoning the original goals of object oriented design?
Thanks!
Indeed, separating business logic from associated data goes against the principles of OOP, and this is what Martin Fowler refers to as an anemic domain model. Personally, I would always go with a proper domain model that puts data and behaviour together.
Specifically, why does it seem that making 'dumb' POCOs is the way to go these days?
I don't know why you thought this was so, but this is certainly not true. There are many "dumb" models out there, but there are also many well-designed domain models.

Why is data flow programming not the norm? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I wrote a simple genetic algorithm to evolve the string "helloworld". I wrote it twice. The first time was written using classes. And the second time was written using just functions where the entire state of the genetic world is passed from one function to the next...to mimic the data flow paradigm. Surprisingly, the code worked well for both the implementations. However, I managed to get it working only after painstakingly removing each and every bug, which was quite a laborious process.
And I asked myself.. there has got to be a better way. Write the code using classes was comparatively difficult than writing the same code using simple functions and I believe writing the same code visually, using something like labview for example would be a lot more easier than writing it only using simple functions.
To that extent, I read about data flow programming and visual programming and quite frankly it seems like it is more natural and intuitive to program in a visual, data oriented manner than in a statement-wise manner, which is what most programming languages enable us to do today. My question is.. if this is the case, why hasn't data flow, visual programming like "labview" become the standard?
I do not believe that data-flow / "Visual Programming" has nearly the performance of well-designed code.
Text-based code can express far more complex and subtle data structures and flows than anything graphical. It gives programmers detailed control over what gets copied, what gets accessed, and precise control over sequences of steps. I have a hard time seeing how data-flow could be that expressive.
Ultimately, data-flow /visual programming can only describe things that are already known. Text-programming (for lack of a better term) actually lets you express more. Programmers can create entirely new data structures and algorithms that simply haven't been represented visually yet.
It is dangerous to use a single problem as the basis for how programming languages should be designed. I'm not sure how the data-flow paradigm would improve GUI framework design, for instance.

I spend way too time trying to think up variable, class, function, etc... names. Is there a tool or method or trick I can use to cut this time down? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Currently my best tool for this is a thesaurus, but I would like to expand my horizons on the matter.
Invest in a tool that makes refactoring easier. Like in NetBeans, you can easily rename classes and variables. If you use that, you can make a mistake without much problems.
But it is wise to think about the structure of names.
A class I think of as an entity, so its name is noun, like Customer is the name for a class respresenting a customer.
A function (and method too) is a verb. It describes what it does, like getName() returns the name. get and set are common prefixes for functions that return or set a property. That's why they're even called 'getters' and 'setters'. A function name could also be saveCustomer to save a customer, or just save, if it is a method of the customer class.
With some basic rules like that, it should be easy to come up with a reasonable name, which you can always change using the refactory tools found in many editors.
Sometimes it is hard to find a good name. I'm not a native english speaker myself, while the code I write (and the comments) is in english, because it conforms more to existing libraries. Sometimes I find it hard to find the right word. In that case I use a disctionary or just Google to find a translation. Usually googling for 'WordInYourLanguage translation' will give you a list of entries on dictionary sites, from which you probably will recognize the right term.
Sounds a bit OCD to me...
Most of my code does not face a 2nd pair of eyes, so I sometimes name my variables things such as "BigBoobs"... At the end of the day, the only people that need to know the variable are you and anyone else who may be looking at the code...
As for seriously addressing your question:
I don't think there would really be a variable name generator out there, because that would involve it knowing what your program is actually doing as you do it. If technology like that exists, I guess I will be out of a job.

Common design by obfuscation practices? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What are some common practices you have seen used in the design by obfuscation crowd? I find it interesting to be on projects that are not allowed to be rewritten while, that would be the faster and most efficient solution to the problem.
My favorites always revolve around variables...leaving ones in the code that are no longer used, then giving them all meaningless names. Of course, you have to be careful to avoid nearly all convention if you really want to obfuscate. So, a perfect one would be to have two similarly used variables, one named myVar1, and another named myVarOne. Stuff like that...
Another one is to include un-used controls that are only visible within the code. I stared at one ASP.NET site for a good hour trying to figure out why a FormView was dropped into it..(there was no answer to that).
I once worked on perl code where the author decided to have most of the subs receive a single hash as a variable and returned that same hash with data added or removed. Basically one global hash used to pass data through the different code paths.
It looked something like this:
my $hash = ();
$hash->{'CUSTID'} = 1001;
$hash = GetAccounts($hash);
if ($hash->{'AccountTotal'} > 100) {
$hash = getTotals($hash);
$hash->{'Acct_Sbkt_Marker'} = 'R1';
$hash->{'Acct_Invr_Marker'} = 'BT';
$hash = removeInvalidAccount($hash);
}
To this day I can't figure out what design pattern he was trying to implement with this.
I remember the $hash would be lined up nicely.
We had one person we worked with store files in a folder call /kensington in order to "hide" them. It just contained some xml files that he didnt want seen and figured people wouldn't look in there.
No or useless comments in the code along with no useful documentation.
I worked with a programmer that used to write hugely complex conditions that when met would call a method that simply did a system out. He did this dozens of times throughout the entire app. Still not sure why....
And there I was thinking that well designed code should stand up on its own to be read, not deciphered.
I understand that people who care about obfuscation are encouraged to use tools like dotfuscator and its equivalents in other environments. Obfuscation in the sense of making the code harder to decompile though, not just making it a pain to work with.
Why anyone would deliberately design horrible code (except to demonstrate the gotchas) is beyond me.