Tag Info

Hot answers tagged

114

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 ...


43

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. ...


20

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 ...


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 ...


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 ...


5

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 ...


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 ...


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, ...


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 ...


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 ...


3

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

In term of performance, there will be virtually no difference between the two considered options. Theoretically, the Single Responsibility Principle suggests that the solution with several small functions called by the big one is preferable if the small functions make sense. Are there semantically well defined? When you read the function names and the ...


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 ...


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 ...


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: ...


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


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 ...


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 ...



Only top voted, non community-wiki answers of a minimum length are eligible