Hot answers tagged data-types
88
If you find it difficult to read 0s and 1s below, scroll to the bottom of the post and hover over the empty looking "spoiler" gray box.
01010100 01101000 01100101 00100000 01110010 01100101 01100001 01110011 01101111 01101110 00100000 01110100 01101000 01100001 01110100 00100000 01110000 01110010 01101111 01100111 01110010 01100001 01101101 01101101 ...
34
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 ...
22
Data types exist to give bits some "shape." Without that shape, bits would be just so many ones and zeroes.
Everything has a shape. Imagine asking someone to look at your raw bits, instead of the picture these bit embody in the JPG format. Imagine if your arrangement of bits that comprises a picture were different from everyone else's. You would have to ...
21
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
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.
...
17
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(Integer(42));
A String is going to be returned for the first call and an Integer for the second call based on ...
17
Because you need to know the encoding.
Let me put it this way. An integer is a sequence of ones and zeroes- but you don't know what value that integer is, unless you know in advance it's two's complement or whatever. If you have a pointer to a sequence of bits, how many of those bits makes the integer you're looking at? If you don't know, how can you ...
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 ...
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
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 ...
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
7
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, ...
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 ...
6
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 ...
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 ...
5
I'm assuming that an "external message" is something that's coming over a network protocol, e.g. RPC, SOAP, Protocol Buffers, etc.
In that case, it's absolutely a good thing to maintain two separate models. One for messaging, one for domain. It's not unusual to have yet another model for presentation and another model for data.
It seems awfully repetitive, ...
5
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
...
5
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 ...
5
Structures are groups of data, typically typed. COBOL saw the first widespread usage of them. Though analogs to that existed beforehand. The design issue here is grouping multiple sets of data together like a name, a occupation, and a phone number. The idea of a "record" is something much older than computing and shows up in mathematics and a host other ...
5
The idea of a structure is to group together a collection of variables into a single container. E.g. if you have three coordinates
int x;
int y;
int z;
you might want to see them as a unit of data and group them together:
struct point
{
int x;
int y;
int z;
};
You can see this as a way to modularize your data.
Now you can define variables ...
5
Yes, at the lowest level, it's all a sequence of bits. But people--even programmers--have a very hard time thinking about sequences of bits. The basic reason why these things exist is to make them easier for programmers to think about.
Computer software is the most complicated class of engineering ever undertaken in the history of mankind. Programs ...
5
This is to some extent a matter of style, but I really believe your single-line option is much, much less readable, and is not a common approach.
The first classic if is very easy to read and check. There's no complex nesting, just a plain, "boring", chain of conditions that is well understood in all languages that have an if/else if/else construct.
The ...
Only top voted, non community-wiki answers of a minimum length are eligible

