7 ms·
>In my Eiffel days, I was encouraged to write "integer", not "int", "character", not "char", and so on. I believe Java encourages this practice too. Supposedly
by Groxx 16y ago
>In my Eiffel days, I was encouraged to write "integer", not "int", "character", not "char", and so on. I believe Java encourages this practice too. Supposedly clarity is maximized. But how can this be if we do the opposite when we speak? Do you say “taxi cab”, or “taximeter cabriolet”? Redundant utterances are tiresome and worse still, obscure the idea being expressed.
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.
- chittis 16y agoGiven that Java doesn't have typedefs how would you reduce the verbosity of : ConcurrentHashMap<String, Integer> foo = new ConcurrentHashMap<String, Integer>();
- jarin 16y agoI would argue that you don't really need to, since auto-completing code editors and displays with enough resolution to support more than 80 columns are pretty ubiquitous.
- Groxx 16y agoThere you're largely screwed, but Java is well-known for being extremely verbose, and of course there will be languages that don't have basic features others have had for decades. 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.
- jedsmith 16y agoI see your Java, and raise you Objective-C: 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.
- Groxx 16y agodisclaimer: I have not tried these. Totally pulling these from a quick googled-refresher of macros. #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.
- Fargren 16y agoI haven't worked a lot with XCode, but I remember using macros without problem.
- jedsmith 16y agoDoing that is probably fine if you are working alone and nobody will ever read your code. Reading Objective-C is already enough of a challenge without having to context switch to a header just to mentally resolve symbols. Instead of using a macro for the selector: NSDictionary *frob = [NSDictionary macroHere:v1, k1, v0, k0, nil]; It would probably be far more efficient to macro the entire call using C99's variadics: NSDictionary *frob = DICT(v1, k1, v0, k0); Still wouldn't walk that road, personally.
- leon_ 16y ago
- mhansen 16y agoDo something like the Google Collections Library does: 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/index.html?com/google/common/collect/Maps.html 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)
- bpodgursky 16y agoThe Collections class built into Java has a lot of timesavers like this too--singleton, singleonList, singletonMap, and Arrays has stuff like Arrays.asList( ... ). 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();
- deleted 16y ago[deleted]
- cabalamat 16y agoI'd write the code in Python (or indeed any sensible modern language). foo = {}
- leon_ 16y agoand then you get an int instead of a string and your python app explodes. let's not start on "sensible modern" languages.
- cabalamat 16y agoYes, you do get bugs in Python due to variables being the wrong type... however the time it takes to fix them is vastly less than the time it takes to write the reams of boilerplate that Java requires (not to mention the cognitive overload caused by said reams of boilerplate). My ideal would be an optionally typed language.
- xyzzyz 16y agoWhat if you want to create an instance of other type of dictionary? {} works only for built-in dicts.
- IDisposableHero 16y agoDoesn't Java have any kind of type inference for variables yet? C# allows you to say var foo = new Dictionary<string, int>(); and foo's type is infered as Dictionary<string, int>
- nradov 16y agoJust declare a new class with a short name that extends ConcurrentHashMap<String, Integer>.
- cdavid 16y agoI don't think it is an issue of technology (typedef, etc...) as much as an application target issue. Using short names in a language without any kind of namespace support can only work for a certain type of applications and developmenent organization. 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.
- dexen 16y ago> 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. 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 ;-(
- cdavid 16y agoI know very well about the use of static within compilation unit, but I was referring to "public API" (more exactly: API between components of your application). This is especially excruciating once you need to use various libraries outside your control.
- leon_ 16y agoI don't know for Java (luckily haven't been using it much) but for C I dislike typedefs. I need to know what data I'm working with. A typedef hides that information from me behind a shiny but uninformative name.
- deleted 16y ago[deleted]
- _delirium 16y agoI mostly agree, except for function pointers. The C idiom of typedefing a function-pointer type results in code that looks more readable to me (e.g. prototypes of functions that take a function-pointer parameter have "typename varname" syntax) compared to the somewhat odd syntax you get if you scatter around function-pointer types inline.
- Confusion 16y agoI also tripped over this argument in the first chapter, but it should be emphasized it's an exception of a bad argument in the book, not the rule. The author would have been wise to avoid this red herring.