Tag Info

Hot answers tagged

67

It's sort of a myth that programmers don't have to worry about types in dynamically typed languages. In dynamically typed languages: You still have to know if you're working with an array, an integer, a string, a hash table, a function reference, a dictionary, an object, or whatever. If it's an object, you have to know what class it belongs to. ...


63

As the types get stronger, they can help you more — if you use them correctly instead of fighting them. Design your types to reflect your problem space and logic errors are more likely to become compile-time type mismatches instead of runtime crashes or nonsense results.


44

There has been a lot of dispute over the use of var. My general rules are the following. When the type is obvious such as when the right hand of the assignment is a constructor, use var. When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var. For ambivalent types (your Decimal being an example) where you ...


44

Disclaimer: I am a type-lover ;) Your question is difficult to answer: What are those trade-offs ? I'll take an extreme example: Haskell, it is statically typed. Perhaps one of the most strongly typed languages that exist, in fact. However, Haskell supports Generic Programming, it the sense that you write methods that work with any type conforming to a ...


32

Technically speaking, Java does have type inferencing when using generics. With a generic method like public <T> T foo(T t) { return t; } The compiler will analyze and understand that when you write // String foo("bar"); // Integer foo(new Integer(42)); A String is going to be returned for the first call and an Integer for the second call based ...


29

You are comparing variable declarations to #defines, which is incorrect. With a #define, you create a mapping between an identifier and a snippet of source code. The C preprocessor will then literally substitute any occurrences of that identifier with the provided snippet. Writing #define FOO 40 + 2 int foos = FOO + FOO * FOO; ends up being the same thing ...


28

Your argumentation against floating point numbers is very fragile, probably because of naivety. (No offense here, I find your question is actually very interesting, I hope my answer will also be.) A classic argument is that floats provide a greater range, but high precision integers can meet this challenge now. For example: with modern 64-bit ...


24

Yes, definitely. Functions/methods that take too many arguments is a code smell, and indicates at least one of the following: The function/method is doing too many things at once The function/method requires access to that many things because it's asking, not telling or violating some OO design law The arguments are actually closely related If the last ...


23

It kind of depends on the language. For example, in languages like C and C++, you have a number of built-in scalar types - int, float, double, char, etc. These are "primitive" in the sense that they cannot be decomposed into simpler components. From these basic types you can define new types - pointer types, array types, struct types, union types, etc. ...


21

Because switching to integers doesn't solve anything. The problem with floats isn't that they have inaccuracies, it's that half the people using them don't pay any attention to what's going on. Those same people aren't going to pay proper attention to the units they are using when they use an integer, and a different set of screw ups will happen. Repeat ...


21

The same reason I don't drive a truck when going to work. I don't use something that I won't use the features of. First of all an array is a primitive construct so an array is faster and more efficient than a List<> for sure, so your argument is not true. Array is also available everywhere and known by developers using different languages and platforms. ...


19

Personally, I find that type safety helps me develop faster in my current job. The compiler does a lot of the sanity checking for me almost as I type, allowing me to focus more on the business logic that I'm implementing. Bottom line for me is that although I lose some flexibility, I gain some time that would otherwise be spent tracking down type issues.


19

Physical characteristics of the universe (like the number of atoms in it) are not useful to determine the boundaries of number sizes, because useful calculations exist using numbers having wider ranges. Floating point numbers are a tradeoff between accuracy and range. They deliberately give up some accuracy to achieve greater range.


15

I am perennially fond of void *. It's probably a symptom of something deeply flawed in me.


15

I'll be short: Maybe a in Haskell. With this simple construct, the language solves the issue of crashes or NullPointerException, it neatly sidesteps the "One Million Mistake" of Tony Hoare :) Frankly, an optional presence checked at compile-time ? It's dreamlike...


15

I like both statically-typed and dynamically-typed languages. The two biggest advantages of type safety to me are: 1) You can often pretty much deduce what a function does purely from its type signature (this is particularly true in functional languages like Haskell). 2) When you do significant refactor, the compiler automatically tells you everything you ...


14

Because most of the processors that you use in your day to day life are not modern day 64 bit processors with crazy fast integer calculations or an over abundance of space. Most of your processors are 8-16 bit devices which run things like your car, microwave, or watch. Besides, what happens when you need to talk about a half of a unit, like a half of a ...


13

From the Java perspective: In Java, there is a very clear distinction between primitive and non-primitive types. A variable of a primitive type directly contains the value of that type (in other words, they are value types). A variable of a non-primitive type doesn't contain the value directly; instead, it is a reference (similar to a pointer) to an ...


13

So why would I ever want to use an array? Rarely, you will have a scenario where you know that you need a fixed number of elements. From a design perspective, this should be avoided. If you need 3 things, the nature of business means that you'll very often need 4 in the next release. Still, when this rare scenario actually occurs, using an array to ...


12

Lua has a built-in table that is most impressive. It has a built-in hashtable and a vector, and with the use of metatables can be the fundamental base for object-oriented programming in a procedural language. Each index of a table can receive any of the basic language structures (number, boolean, string, function -yes, functions are types on lua -, and ...


12

It'd be defined by the architecture you were using. On a Zilog z80 chip (common embedded chip) they'd be one size while they could be an entirely different size on a x86 chipset. However, the sizes themselves are fixed ratios to each other. Essentially short and long aren't types but qualifies for the int type. Short ints will be one order of magnitude ...


11

In the first case, the compiler knows that you're facing a potential loss of precision, so it can stop you. In the second case the overflow happens during a runtime calculation - though your example trivialy causes an overflow, there is no way for the compiler to check such occurrences in general case so it doesn't.


11

The problem is the line byte q1 = keyboard.nextByte() * 10;. There are no arithmetic operations on byte or short. The value of keyboard.nextByte() is casted up to an int prior to multiplication with 10, which is also an int. The result of the multiplication is an int, which can not be stored into q1 if it's defined as a byte. Possible solutions would be to ...


10

Sometimes you need full control of the size a number takes in memory. Or you may want to directly process binary data you read from a file, or copy from video memory if you process a screenshot or grab from some port, network connection, whatever. In theory you could add more classes that are specialized in processing binary data, but depending on the ...


10

Using a float instead of a high precision integer (with conversions!) is simply easier and faster. I can type in float myVar = 0.15; //my value... and move on to the rest of the logic of my simulation. I don't have to spend extra time thinking about converting to int and making sure that all of my scales are correct. And the results end up being good ...


10

I'm working on a report as I type this. One of the fields is a long milliseconds of duration that I got from somewhere else. This is going to be sent to Microsoft Excel and the duration units it uses is decimal days (1.25 = 1 day, 6 hours). Sure, you can subdivide a range from the lowest possible value to the largest and have integer units stepping ...


9

I'm surprised nobody has mentioned Monads or Algebraic Datatypes yet.


9

When to use var is a programming "holy war". There is precisely one place where it's required: when the result of an operation creates an anonymous type, such as: var result = new { Name = "John", Age = 35 }; Anywhere else, it's optional and really up to your coding standard to use it or not in the other situations. And yes, you will need the special ...


9

IPv4 is a very good example where a limited spec size caused a very expensive problem down the line. 4.3 billion addresses just aren't enough anymore. Now ISPs around the world are desparately rolling out IPv6 with a 128-bit address space which translates into an address for every atom in your body or something like that.


9

As Amon pointed out, this is a good application for the visitor pattern. Using it, your AI classes will end up looking something like this: void decide(HomeSquare square); void decide(WorkSquare square); void decide(ShopSquare square); And your squares have an accept function that looks like: void accept(AI ai) { ai.decide(this); } That lets you ...



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