by Groxx · 15y ago · 0 comments
I see this argument a lot, and they strike me as people complaining because they don't use tools their language provides.
Typedefs are the answer to excessive name length, and they're nearly everywhere. Just create a couple typedef files, and import them as needed - future programmers get the full names easily, while you can program in your pseudo-K version of C for maximum keyboard efficiency. I have a handful of such files, they're endlessly useful - why write `Pop_froM_lIsT_WhIch_COntaiNs_speCiFik_type_X`, doing battle with naming-scheme-X that only employee-Y uses (and their associated spelling errors) when you can do so once, and write `pop` from then on, unambiguously?
The upside of typedefs for this comparison is that they're precisely what we do with spoken language - nobody knew what a "taxi cab" was until someone told them it the shorter version of "taximeter cabriolet", or until the full phrase was well enough known that it could be inferred accurately by the average person.
HashMap<String, Integer> foo = Maps.newHashMap();
The return type of newHashMap is determined by the type of foo, so you don't need to repeat the generic types in angle brackets. public static <K, V> HashMap<K, V> newHashMap() {
return new HashMap<K, V>();
}
http://google-collections.googlecode.com/svn/trunk/javadoc/i...(I'm not a fan of Java myself, but there are some tricky ways to cut down on its verbosity)
Also it's rarely important that you know the actual type of Map you're using (the interface is what's important), so you can just call
Map<String, Integer> foo = Maps.newHashMap();
The closest you can get in Java is to wrap it in a private, internal class. Which also lets you rename some verbose or frequently-combined operations, say:
foo.someoneCouldntComeUpWithAShorterName() -> wrapped.succinct()
-- or --
foo.store(val);
foo.store(val);
foo.store(val); // I hate void functions...
-> wrapped.store(val,val,val);
But yes, it's a pain. Java's largely a pain. NSString *thing = [NSString stringWithFormat:@"Result: %@", [foo someoneCouldntComeUpWithAShorterNameWithZipcode:08205 name:@"larry" parcels:5]];
NSDictionary *request = [NSDictionary dictionaryWithObjectsAndKeys:@"iphoneapp/0.1", @"User-Agent", @"news.ycombinator.com", @"Host", nil];
This isn't to kick off pet language wars, by the way, just to point out that my OCD tic to wrap to 80 characters has absolutely zero place in Xcode. Or Java. canvas.drawImage(kittens.subImage(vec2(10,10),vec2(20,20)),vec2(30,30))
vs [canvas drawImage:[kittens subImageAt:vec2(10,10) withSize:vec2(20,20)] at:vec2(30,30)] #define withZip someoneCouldntComeUpWithAShorterNameWithZipcode
#define dictWith dictionaryWithObjectsAndKeys
or
#define dictWith NSDictionary dictionaryWithObjectsAndKeys
so you can [dictWith: key, value, key, value]
:) I forget, does XCode intelligently handle macros? Or does using a macro destroy its helpful features, like code completion?I actually have a "hashWith" utility function for my .NET code. Saves me a ton of ugh-induced headaches.
Dude. Are you still using CGA? Unless for your IDE you're running emacs on your wristwatch or phone there's no place for that shit in the real world, and you have no excuse.
Ditch that green cathode ray tube, here, have 50c and go buy yourself a real monitor. :D
The great thing about ConcurrentHashMap<Long,String> isn't writing it, it's reading it. Even if you're not a Java programmer you know exactly what that is (and FWIW it's world-class on implementation). We could do with some syntactical sugar to make that constructor line a little simpler, but declaring the type and calling a specific constructor, not masking either with personal shorthand are both Good Things if someone's going to have to read that code later.
buf[thisVariableIsTheLoopIndex] = foo(...);
is no more obvious than buf[i] = foo(...);
and indeed is more likely to slow the programmer down by killing his train of thought and mental flow. var foo = new Dictionary<string, int>();
and foo's type is infered as Dictionary<string, int> foo = {}My ideal would be an optionally typed language.
C really shines for low-level programming: while something like Linux Kernel is extremely complex, it has relatively simple needs as far as interface goes. The public interface is very small (defined by system calls - I don't know the count, but suffice to say the most trivial python libraries will often have more functions). Same thing can be said for other applications where C has been very successfull for.
But something as fundamental as a lack of any namespace in C really hurts when you need to handle very large applications which cannot be easily split in very well defined components with small interfaces. Complex GUI and browsers come to mind, which is exactly the kind of things where C++ is a well accepted choice.
There is a degree of namespacing in C. Each compilation unit introduces own, anonymous (cannot be addressed/indicated directly) namespace. Symbols defined `static' (``having static linkage'', actually) reside only in it, and are not visible outside of that namespace. Symbols with external linkage are the API of the namespace. It may sound strange, but it's perfectly OK to have symbols with same name in several namespaces, as long as they are static (residing only in it); no conflict because they are not addressible from outside. In a way, a C's compilation unit is similar to C++'s class -- static (C) / private (C++) elements are visible only from this compilation unit (C) / class (C++).
So yes, you can easily split any C application into well defined components with small APIs, without risk of collision between names of private symbols. You only need to ensure unique naming among the `public' API symbols.
Linker use and linkage is a lost art those days, eh ;-(
This is especially excruciating once you need to use various libraries outside your control.