Tag Info

Hot answers tagged

39

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 ...


28

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 ...


24

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 ...


22

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. ...


22

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 ...


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.


19

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 ...


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 ...


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

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 ...


8

One reason why I don't use unsigned integer types all that much in Delphi is that they can create problems when mixed with signed integers. Here's one that bit me once: for i := 0 to List.Count - 1 do //do something here I had i declared as an unsigned integer, (after all, it's an index into a list that starts at 0, it never has to be negative, ...


8

I always liked defining a rectangle as a point + width and height, where the point is the upper-left corner of the rectangle. class Rect { float x, y; float width, height; } And then add whatever methods you need to fetch the other metrics. Like the Java version


8

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 ...


7

Memory usage is way down the list of the benefits of data typing. Take dates for example. If you are accepting a date in a form field, you likely want to be at least somewhat permissive about accepting the data in whatever format the user wants to enter it (i.e. 7/1/77 or 7-1-1977 or 7-77 if you're designing for an American audience might all be acceptable ...


7

Well, first off, type inference has nothing to do with the maturity of the runtime, whether that runtime is a 30 year old CPU or a VM that is so new the bits are still shiny. it's all about the compiler. That said, it is allowed for generics, the reason why it's not allowed for non-generic types seems to be because of philosophy -- there's nothing ...


6

From MSDN: However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required. I really, really don't like implicit typing. On the surface it tends to make code more readable, but can lead to lots of ...


6

I am not so sure the explanation is all that excellent. Algebraic Data Types are used to create data structures, such as lists and trees. For example parse trees are easily represented with algebraic data structures. data BinOperator = Add | Subtr | Div | Mult | Mod | Eq ...


6

Well p1: Point and p2: Point are each going to have two int coordinates in them anyway, so doesn't your class amount to the same thing? And if you store those two points as first-class Point objects, don't you get a little more utility from them? In most graphical coordinate systems that I know of, points are subclassed in this way to create a hierarchy of ...


6

Have you considered that it is less error prone? If you use (Point1, Point2) it is then very clear what you are specifying. If you provide 2 points, then the only possible error is that the user has mixed up their x and y when constructing the points as the order of the points doesn't matter. If you supply 4 integers, then if someone isn't paying ...


6

A primitive is a basic data type that's not built out of other data types. It can only represent one single value. All primitives are built-in data types by necessity, (the compiler has to know about them,) but not all built-in data types are primitives. In some languages, the compiler has built-in knowledge of certain types that are built out of other ...


6

Is there anything special about those datatypes that is necessary for the completeness of the language? Nope. Many languages don't have hashes as a fundamental data structure in the language. And indeed, there are examples of languages that don't have arrays or lists either. (BCPL for instance). And many languages have other fundamental data ...


6

Because 90% or more of the time you use primitive objects with primitive semantics anyway, not typical object semantics. The compiler has to carve out special privileges and restrictions for primitive objects, like: They are automatically instantiated for a literal. They must be immutable, in order to allow it to optimize down to a computer architecture's ...


5

Actually, a rectagle isn't defined by 2 points. A rectangle can only be defined by two points if it is parallel to the axes. There are several ways to represent rectangles that are parallel to the axes: Two diagonally opposite points One corner point, height and width Centre point, half height and width (uncommon, but sometimes useful). As two X ...



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