Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!

Campbell Ritchie

Marshal
+ Follow
since Oct 13, 2005
Cows and Likes
Cows
Total received
187
In last 30 days
7
Total given
299
Likes
Total received
5175
Received in last 30 days
62
Total given
160
Given in last 30 days
10
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

Surely it is a good practice to write a version control (=VC) message at all. Surely the really bad practice would be not to use VC, or only to use it when something goes wrong.
9 hours ago
Yes. It should normally be an instance field. Passing arguments and getXXX() methods are ways to get such information into and out of an object. The three things are different. If there are three classes each with a length field, don't go thinking that is some sort of re‑declaration. They are separate fields. But make there isn't a field of the same name in a superclass; that would be hiding of fields and can cause no end of confusion.
Don't go saying that “efficiency” is “substance” and class design is  “style”. If somebody has told you that, they are labouring under a severe misconception about programming concepts. Speed of execution is usually the last consideration in your mind when you design a program. No, memory consumption probably ranks even lower than speed much of the time.
16 hours ago

Stephan van Hulst wrote:. . . Alas, your own example violates this good rule. . . .

Damn! I forgot to write static
16 hours ago
I know it is a bit late, but the old Sun Style Guide suggest you avoid constructs like if (b) return x; else return y; It recommends you use the ?: operator instead. Note that the else keyword is redundant in that sort of construct.
The ?: operator can be nested inside itself, but you need to be very careful to indent the code and maintain legibility.
21 hours ago
Bertrand Meyer, writing about 25 years ago in Object‑Oriented Software Construction, which is actually about the Eiffel language, takes it for granted that inheritance is essential, and multiple inheritance to boot. Nowadays I hear people say things like, “I hardly ever use inheritance.”
Have you written anything about inheritance as a bad practice or a good practice?
21 hours ago
To get back to the bit about number of lines:-
  • 1: If you find a book like Object‑Oriented Software Construction by Bertrand Meyer, you find Meyer says that OO programs are shorter than procedural because there is no need to repeat the code in methods. He says that the amount of code to write a complicted algorithm is usually the same in each paradigm however.
  • 2: How much research is there about the benefits of dividing methods up into multiple methods?
  • 21 hours ago

    Randy Too wrote:. . . Based on Alibaba guideline, if statements without bracers is not encourage . . .

    That is because you can cause no end of confusion by trying to add a second statement inside the body of the if. You would probably have a second warning about having the whole statement on one line. You might also have a warning about not handling nulls correctly. Why do you think that variable might point to null ? Is null a likely value, or does it represent an error state? If you receive no error messages, all well and good, but in other circumstances maybe you are better off throwing an Exception straight away.

    BTW: the punctuation mark should only have one “r” in its name: brace(s).
    21 hours ago

    Hassan Syed wrote:. . . Is it a good programming practice to declare a utility method as a final method.

    Yes, and no. You would not want a utility method to be overridden (actually it would usually be static, so you can't override it, only hide it). You are right that you wouldn't want different versions of the same method, so you would usually write a utility method in such a fashion that it cannot be hidden.

    But, you would always write utility methods as part of a utility class. My idea of a utility class is that it looks something like this:-Give it the following features:-
  • 1: Make it uninstantiable: give it a private constructor never called.
  • 2: Make all its members static.
  • 3: It has no variables; if it has any fields at all, they are constants.
  • 4: All its methods are pure functions; not only does that mean they return values (except as in No 5 below), but also for the same input they always return the same output.
  • 5: Collections#sort() is an example of a utility method with void instead of a return type; it takes the List as what the SQL people call an output parameter.
  • Now, if you try to subclass MyUtilities, you won't be able to call super(); in the constructor, so you can never create a subclass of an uninstantiable class, so the method is effectively final. That is why utility classes and utility methods are not usually tagged final; it is unnecessary. But you can do no harm by writing final.

    . . . avoid type method performing some calculation and displaying the result directly. . . .

    I presume there is a spelling error there and you meant to write a void rather than avoid.
    In my opinion that method isn't a utility method. A utility class or a utility method exists only to provide resources for other code. Such a method isn't a resource for anything else, but exists as a standalone program, which does a calculation and displays it. That means it is doing two things; it would be better to have two methods each doing one thing. Let one return that value, and that method might now be a utility method, and have the other method display values. Come to think of it, you already have methods for displaying values, e.g. System.out.println(...).
    21 hours ago
    What you are supposed to do is use the result from filter(...) as a new Stream. After all, Stream#filter(Predicate) is an intermediate operation, which returns a new Stream, so you should use that Stream. As PS says, once you have called a method on a Stream, the Stream is regarded as used, even if the method appears to do nothing.
    Why are you using a Stream<Integer> rather than an IntStream? This is what I would have used:-I shall let you work out why the last line would read better as .forEach(System.out::print);

    I shall also move this discussion to our Streams forum.

    Junshi Gusza wrote:thanks . . .

    That's a pleasure

    i solved it.

    Well done: please show us the finalproduect

    . . . . is it possible to delete this thread?

    No. We only delete threads which have something wrong with them.
    1 day ago
    No, it is a mere teenager. We used to worry about old threads, but this sort of thing shows that they can still be useful.

    And welcome to the Ranch
    1 day ago
    Start by finding the index of the highest score as you had before. Once you have found that, use exactly the same technique for finding the winner from the averages.
    1 day ago
    I would suggest you are better off creating an IntegerTriplet class or similar, with comparison methods. Make that method return a List<Integer>; it is probably a good idea to make that List immutable or read‑only.
    1 day ago

    Norm Radder wrote:I missed where the average must be an int.
    I saw: . . .

    No, I got that wrong sorry.
    1 day ago
    But OP does seem to require an average; that can only be expressed as an int by rounding it.
    1 day ago