Tag Info

New answers tagged

-1

Quick Short Answer Yes, Programmers can apply the Object Oriented Programming without "Classes". Long Boring Extensive Descriptive Answer There are several variations of "Object Orientation", altought, the first concept that comes to the mind of many programmers is "Classes". Yes, Programmers can apply the Object Oriented Programming without "Classes", ...


4

The purpose of a dictionary is to return a value given a specified key, not to serve as a sequential container. Just because other data structures are sequential but dictionaries are not does not mean that the benefits of sequences are invalidated. Sequences are one of the three fundamental logic structures in computing. Every (solvable) computing problem ...


0

Not always: it depends on the language. You've demonstrated the ability to do this in Python but (if your question is meant to be language agnostic despite the Python tag) not all languages can do this. Java, for example, mostly can't. Ignoring the class that contains main, there is no way to define arbitrary methods/fields on an object defined within main ...


5

Of course you can! The Self programming language is a dynamic prototype-based object oriented language in which everything is an object and there is no sense of classes or whatsoever. It's focused in the idea of prototypical objects and the idea of cloning them instead of having classes as templates of how to create objects. You should check ...


33

Congratulations! You rediscovered the well known fact that object orientation can be done without specific programming language support. It is basically the same way objects are introduced in Scheme in this classic text book. Note that Scheme does not have a class keyword or some kind of equivalent, and objects can be created without having even classes. ...


7

I would agree that the first definition satisfies the three points your teacher made. I do not think we need the class keyword for anything. Under the covers, what else is an object but a data structure with with different types of data and functions to work with the data? Of course, the functions are data as well.. I would go even further and say that ...


1

While I generally agree with dan1111's answer, there is one particularly important case it does not cover: the case where smartphone is used concurrently. In that scenario, this sort of pattern is a well known source of hard to find bugs. The problem is that short circuit evaluation is not atomic. Your thread can check that smartphone is null, another ...


2

Some functional languages such as OCaml have built-in mechanisms to implement abstract data types therefore enforcing some invariants. Languages which do not have such mechanisms rely on the user “not looking under the carpet” to enforce the invariants. Abstract data types in OCaml In OCaml, modules are used to structure a program. A module has an ...


4

Encapsulation is not a feature that came with OOP. Any language that supports proper modularization has it. Here's roughly how you do it in Haskell: -- Rational.hs module Rational ( -- This is the export list. Functions not in this list aren't visible to importers. Rational, -- Exports the data type, but not its constructor. ratio, ...


2

You do it the same way: make a constructor that enforces the constraint, and agree to use that constructor whenever you create a new value. multiply lhs rhs = ReducedFraction (lhs.num * rhs.num) (lhs.denom * rhs.denom) But Karl, in OOP you don't have to agree to use the constructor. Oh really? class Fraction: ... Fraction multiply(Fraction lhs, ...


2

One consideration, not mentioned in other answers: sometimes having these checks can hint at a possible refactoring to the Null Object design pattern. For example: if (currentUser && currentUser.isAdministrator()) doSomething(); Could be simplified to just be: if (currentUser.isAdministrator()) doSomething (); If currentUser is defaulted ...


7

You can take this further, and in some languages it's the idiomatic way to do things: you can use short circuit evualuation outside of conditional statements too, and they become a form of conditional statement themselves. E.g. in Perl, it is idiomatic for functions to return something falsy on failure (and something truthy on success), and something like ...


-6

I'm going to be unpopular and say Yes, it is bad practice. IF the functionality of your code is relying on it to implement conditional logic. ie if(quickLogicA && slowLogicB) {doSomething();} is good, but if(logicA && doSomething()) {andAlsoDoSomethingElse();} is bad, and surely no-one would agree with?! if(doSomething() || ...


19

Let's say you were using a C-style langugage with no && and needed to do the equivalent code as in your question. Your code would be: if(smartphone != null) { if(smartphone.GetSignal() > 50) { // Do stuff } } This pattern would turn up a lot. Now imagine version 2.0 of our hypothetical language introduces &&. Think how cool ...


3

There are plenty of situations where I want to check one condition first, and only want to check a second condition if the first condition succeeded. Sometimes purely for efficiency (because there is no point checking the second condition if the first already failed), sometimes because otherwise my program would crash (your check for NULL first), sometimes ...


107

No, this is not bad practice. Relying on short-circuiting of conditionals is a widely accepted, useful technique--as long as you are using a language that guarantees this behavior (which includes the vast majority of modern languages). Your code example is quite clear and, indeed, that is often the best way to write it. Alternatives (such as nested if ...


1

Student, Subject and Degree are not directly related. A StudentDegree would have the knowledge and reach to know about students, subjects and their degree. A subject should not track what each student scored. A StudentSubject should. A student should have one StudentDegree. A StudentDegree has a Student, and many StudentSubjects and Degree. Now you've got ...


1

First: What is the program supposed to do? You cannot make decision about architecture without knowing the purpose of the program. Also, ignore advice from people whom you haven't told the purpose of the program :) Second: Unless you are writing an actual simulation of a college, you probably don't want objects like Student, Subject etc. to have behavior ...


1

If you want to associate a Student with a Degree, but keep them separate from each other, you can express that relationship with a data structure like a HashMap. something like (untested, treat this as pseudo code): HashMap<Student, Degree> enrolment = new HashMap<Student, Degree>(); Student dave = new Student(); Degree masterOfScience = new ...


2

Modular design is about separation of concerns, NOT about splitting something up into smaller sections (though it tends to have that effect). It's perfectly acceptable (and even very good practice) to have a separate EJB module, web module, etc.. Each is its own deliverable after all within the JEE landscape, with the EJB module creating an EJB jar, the web ...


2

Generally speaking, there is no right way to organize classes. There is only the way that best meets the projects requirements for clarity. The size of the modules is probably the least important organizing principle. – Robert Harvey


2

Asking this question shows that you find your current model not intuitive and I think this is a good feeling. Let me explain an alternative to having only Student, Subject and Degree to you that hopefully feels more intuitive. While a student might seem to know everything about his degrees, he doesn't. Let's illustrate this with a real world example: ...


0

Due to the inexplicable power of rubber duck debugging I have now a solution I find better than all the other approaches I thought of. I will implement a rounding stack. When a subroutine is entered, the last entry will be duplicated and pushed as first element on the stack (It will also be used entry if the stack is completely cleared). The stack is ...


3

I couple of notes: I don't understand your ADD_EXACT problem. If the operation is exact, don't round. Alternatively, if there has to be a rounding step (this includes most FPU operations), it must not introduce error. FMA should probably be a completely separate operation from both addition and multiplication, since its rounding behavior is fundamentally ...


0

Do a "binning" of the initial black box stack into N groups of equal proportions (your A-D, D-K...). At that point you can use any O(n log n) algorithm to sort each group in turn. Actually, you could pop items from the black-box stack directly into a group of N red-black trees. But if you have lots of identical elements... could you not just store a ...


1

There are a lot of ads asking for "language agnostic programmers". They're looking for people who are okay with programming in whatever language is needed to get the work done. They want people who are totally fine with learning another language if that's what the project calls for. They're looking for people who aren't clinging to just one language and ...


0

We were told numbers could be up to about 10^12 and about 20% of numbers would be used. That is 2 * 10^11 numbers actually used. With these numbers, the best you can do is a bitmap of about 125 GBytes, and if you can't afford the RAM, an SSD drive and virtual memory will have to do. You should probably measure whether "normal" file access is faster than ...


-3

Just to add a suggestion to the other solutions. In the biomedical world we often search for specific strings of data (ie genetic patterns). Research has shown that attacking the parent string in the reverse direction, the last entry to the first, is faster that going forward through the string. I guess this is some how related to slurping in all the data ...


1

I would try to go for the bitfield too, but if the data is sparse and the range too great to make this work, a splay tree might be a good data structure to use. Splay trees are modified on each access (so they are not thread-safe, which could be an exclusion reason for you) to optimize repeated access to the same element. Often-used elements bubble to the ...


2

You could generate a binary tree (radix tree aka trie). To prepare for the search, for each number in your array, starting with the least significant bit, if it is 0, you look to the left node. If it is 1, you look to the right node. You follow the tree until you find no more nodes, and from there you create the tree one node at a time. To search, ...


8

If there is a known maximum N, you can use a Bit array for really fast lookup time. Simply keep an array of size N/8 (rounded up) around, with each bit corresponding to a number, 1 if it is in the set, 0 if it isn't. Lookup the relevant bit to check whether a number is in the set. If this is too slow, and you have the megabytes ("millions" doesn't sound ...



Top 50 recent answers are included