- Feature Articles
-
CodeSOD
- Most Recent Articles
- What a More And
- Hall of Mirrors
- Magical Bytes
- Contact Us
- Plugin Acrobatics
- Recursive Search
- Objectified
- Secondary Waits
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
When you get around to implementing a Singleton design pattern without something like a static method/variable, be sure to let us know how that worked out for you. And if you've never been in a situation where a Singleton was the right answer, I question the breadth of your experience.
Admin
Subtyping is used to implement abstract parts of an interface, while extension adds functionality to an existing object. Java's "extends" syntax for subtyping is heavily misleading, as is its ability to inherit from concrete classes. ("final" is your friend.)
What I was referring to is something like C#'s extension methods. If you aren't familiar with those, I suggest Google.
Admin
Generally I find that programmers that claim "everything in an OO language must be OO", are also the same programmers who fail to realize the impact of having a million methods on a type, as opposed to having a small number of (frequently-used) methods on the type and the less-frequently-used methods in utility clases.
But hey, what do Sun, Microsoft, et al. know about designing programming languages? I'm sure each of the first-year CompSci students promoting OO purity here, could design a better language than these industry giants. Of course, it would be slow as shit and horrible to use, but hey, it's completely OO, so all other considerations aren't important!
There is a time and a place for everything, OO and utility classes included. A good programmer knows when to use them and when not to; a bad programmer tries to force the same pattern on everything, and their code ends up on this site.
Admin
You're correct. I just double checked- value types only need to be boxed when they're interacting with data structures that aren't generic. Another good reason to use generics.
Basically, in .NET, if you put integers in an ArrayList, they need to be boxed, but if you put them in a List<Int32> they don't.
Admin
OOOOORRR, you could do the right thing and apply one of the many design patterns that retain object purity, avoid spaghetti code, and keep your code maintainable while solving that problem.
Admin
Further proof that C# is just Java with most of the stupid sucked out. (And some stupid added, but you can avoid it if you want to.)
Admin
Autoboxing of primitives was introduced in Java 5. Before then, one would have to call the valueOf method of whatever numeric type was wanted (Float, Integer, etc). These numeric types are still immutable, so a new object would have to be created to hold the result of the operation. Putting the math operations on the individual numbers would have carried a heavy performance hit.
Today I would like to see sqrt and its ilk delegated to the numbers. Can you imagine 4.sqrt()?
Admin
Yes, but it's multivalued.
Admin
And no one writes actual code in lambda calculus, just as no one writes actual code with Turing machines. Various ML derivatives like Haskell have a rich and varied set of meta-types, to include machine types, algebraic types, types themselves, and, yes, functions. And none of them are the red-headed stepchildren the way non-objects are in an OOP environment.
But there is no "truly pure OO language." Turing machines are real. The lambda calculus is real. So there is a solid mathematical basis for imperative languages and functional languages. But attempts at forming a basis for OO languages have all been miserable failures. The Liskov substitution principle can't even get a simple type hierarchy like "A square is-a rectangle" to work.
Without that mathematical basis, you're basically left with a "no true Scotsman" argument.
Oh, please. You're seriously saying that if Sun's people "knew what they were doing" they would have broken backwards compatibility?
Admin
In Ruby, it's quite common to see loops implemented as things like
.Even in .NET and Java, you can call functions on String literals: "Foo".toCharArray()[2] for example.
Admin
Ah, the advantages of having a language developed 7 years later vs. the disadvantages of backwards compatibility.
It seems like many people think that TRWTF here is Java. That is TRWTF.
Admin
And that's why I advocate using functional languages that are provable subsets of lambda calculus. Because they're awesome.
Admin
Whereas your knowledge of Singleton clearly demonstrates that the breadth of your experience extends at least as far as chapter 1 of "Teach Yourself Design Patterns in 30 Days."
Admin
You speak Spanish. Therefore you are a half-breed and must die. You speak French. Therefore you are a half-breed and must die. You speak Italian. You are a half-breed and therefore must die. You are not of the Klan. Therefore, you are a half-breed and must die.
Admin
The square-rectangle argument is irrelevant. It's is an application domain problem, not a language problem; you'd have the same sort of issue in any other paradigm. Furthermore, it's one of the most trivially-solved examples - make the Rectangle and Square classes immutable! (And why shouldn't they be, it's not like mathematics allows squares to change shape.)
Yes. See every language that has ever broken backward compatibility to fix mistakes made early on. Python 3, anyone?Admin
We've got a bunch of ivory tower crap going on here today. To me, programming is a means to an end. Doing it in a simple, pragmatic manner trumps theoretical ideals. Always.
Admin
Admin
I have people code this in Java all the time during interviews. Many people can't get it working at all and some do things that are very complicated. This is one of the worst I've seen but at least it works. I ask interviewees to code it 0-indexed, not 1-indexed like this one is. I'm looking for something like this:
Admin
Admin
Hey! That was on Wikipedia's front page today! The article kind of relates here.
Ok, so they're 3 floors up, and the building is sinking. What would you do?
Oh, man. Starting to sound more and more like a software solution all the time.
There's backwards compatibility for ya. Design flawed from the beginning leads engineers to jump through hoops to keep the building up nearly 1000 years later.
Pictures are funny. Check out the ones where you actually have to walk through the door, which is at an angle.
Admin
Anything worth doing is worth doing in as abstruse a manner as possible.
Admin
eh?! creating a util class is breaking oop?! you are smoking something
Admin
Really!?!? So, how would you get an instance of a singleton?
Admin
Though I suppose Veniam could simply be in favor of prototype-based OOP rather than class-based OOP, or just dislikes OOP in general...
odio - the original code was indeed odious
Admin
Hurrah!
Admin
It is so nice that you have added a constructive comment. I wonder what your argument would be for a non-static get instance on a singleton.
Admin
QFT. Like so many other things, Delphi got OOP right where the C-derivatives screwed it all up.
Admin
Fine, you made me bite.
First off, remember that the distinction between types of numbers (int, double, etc.) is purely man made and mostly implemented for memory reasons.
Hence a pure OO approach to the problem would be to have a number class (who'da thunk it?) which can be typed to behave like any required variable (int, double, long, short, so on...). This means that all numbers have access to the same methods AND can be typed to behave in certain ways for memory reasons or formatting.
To paraphrase Shakespeare: An int by any other numeric variable would add just as sweetly...
Admin
Hint: the code was written separately by two programmers.
Object oriented numbers: cool! Program set inclusion according to http://en.wikipedia.org/wiki/Set-theoretic_definition_of_natural_numbers , and you will then need a math class to use the Math class.
Admin
Admin
Welcome to crazy world of C#. captcha: vindico
Admin
I've never understood why Java arrays are objects but their methods are in a separate library. Assuming there's a reason, would anyone care to point me at some reading material?
On a side note, the overgeneralizing in this thread is getting to me. Obviously pragmatism has value, but sometimes "I used the practical solution" is shorthand for "there's probably a pattern for this, but I was too lazy to learn it. As a result I've reinvented the wheel badly... have fun tracing!"
Admin
Java's "extends" keyword does just that: it extends a class. You can implement an abstract method, or override the behaviour of an existing method, or add a new method.
C#'s extension methods do not, in fact, extend a method: they just pretend that they do, and essentially it's syntactic sugar hiding a utility class with static methods.
What you're complaining about is Java's reluctance for syntactic sugar (the enhanced 'for' loop as one of the few examples).
But on the other hand, if in one piece of code you have a class with a certain method and in another piece of code that class does not have that method, that's not what I would call clarity.
Admin
Nah we stop at every floor. Our buildings make much more sense - it's very confusing working in an American building if you're from the UK!!
Admin
/**
French-speaking programmer by any chance? they regularly leave you with crap like:
public string MethodeGetLangue(string motDuLangue){}
which is about as pleasant as finding a lump.
captcha: gravis, c'est tres gravis
Admin
ok now for my PROPER review of the code.
If we have to use an array here why can't we use an array of char? Not hugely familiar with Java details as to whether you can convert numbers to their ASCII equivalents and use arithmetic on them, thus 'A' is guaranteed to be exactly 25 less than 'Z' etc. If coding in C or C++ I would take advantage of this. Failing that I would create an array but I know in Java that strings are not arrays of characters.
Ok, why the word VALUE? This is Utils::VALUE. Maybe we will have other utils and it does not make sense. Utils::LETTERS_IN_ALPHABET maybe. Maybe needs more typiing later in one place.
Put a comment at the top of the function saying what it is meant to achieve.Does StringBuilder allow inserts at the front? Even if not I would think of whether we should be using StringBuilder.
Don't use a and b here but a meaningful names.
This will happen only if number was originally 0. We have already determined that b and c are not both 0 because that was handled by the previous block, so the last test is redundant. Use --b rather than b = b-1 (with the more meaningful name, of course). We could have managed without doing that in both blocks by opening a brace between the else and the if above (rather than else if).but a could be equal to VALUE which is the only reason we need to make the array 27 in size.
Probably does what we need to concatenate all the strings in our array, all of which were one character long.
This is a good coding exercise I think to give to candidates applying for a job. The sort of thing you can ask them to do in their favourite programming language.
Admin
Answer in two points :
Admin
Time always matters, just depends whether it's developer time, deployment time or execution time that you wish to optimise on.
Admin
Sorry, but no.
It is extremely dangerous for "treated nearly as primitives"-classes to be mutable and non-final.
Sun did get it right for String and the wrapper classes for primitives (which are all immutable and final), but screwed it up e.g. for java.util.Date, which is neither immutable, nor final, but still treated by most developers like a primitive, i.e. they are passing around instances which should (but do not) pose an invariant and are e.g. used as keys in collections. And then such instances get changed their value by some external part of the programm having a reference to the same instance whilst still being used as key - consequently wreaking complete havoc on the internals of the collection.
It is for a reason that all mutators in java.util.Date are deprecated - which unfortunately does not stop anyone from using them anyways, ignoring each and every warning by the compiler - and afterwards complaining about "broken" or "slow" collection implementations...
I also like LISP a lot and did my fare share with it, mostly for fun - but admiring and liking its concepts does not by itself a good OO-developer make...
Admin
Admin
Admin
13th Floor? We don't even have a 13th House. http://www.stuff.co.nz/oddstuff/2441831/No-number-for-witches-in-Palmy/
Admin
But when you want to add functionality to an EXISTING object, you need to use composition. You can't just subclass any random type to add functionality to that type. Well, actually, I take that back - you can, and many programmers do, inherit from classes that were never meant to be inherited from for the sole purpose of adding functionality. It's conceptually nonsensical and practically counterproductive, but that doesn't stop people from doing it.
C#'s extension methods are a bit of a bad example. They're rather limited, in that you can't define an overridable extension method for a superclass and then override it polymorphically with an extension method for a subclass. If this were possible, it would completely obviate the Visitor pattern and allow better separation of concerns. But, in order to do that, it would be necessary to have a concept of types that can contain functionality but not data (sometimes called "mixins" and occasionally and incorrectly "traits"), which Java butchered into types that can contain only abstract functionality without concrete implementation ("interfaces").Admin
Admin
Or rather turning numbers into letters?
Admin
frist +1... or is that FRIST?
captcha: suscipit - wtf?
Admin
Admin
doesn't work in either of the 64-bit browsers I tried; they must still be using 32 bit list numbering internally.
Interestingly you can however encode/decode bigger numbers using Netscape 7.1 (or later) Composer's List Properties dialog, which goes up to about 15 digits, although the resulting numbers do not display properly in the actual list.
Admin
People teaching this should be fired. Out of a cannon into the sun.
Admin
Never use a big word when a diminutive one will do.