|
would probably say that indicating a type explicitly is is old people coding. I utterly disagree, but there are a lot of people who take that approach. Having recently been going through a large chunk of someone else's code, I've found that having to hover over everything to get the IDE to show me what it is (and sometimes the IDE doesn't show me what it is for whatever reason) slows me down a lot. Even after I've hovered, I can't remember the types of more than a small number of things at once when digging into code I don't know, so I have to hover some more.
Leaving aside that being unexplicit is the opposite of what we should be doing, it's just not good for readability.
Explorans limites defectum
|
|
|
|
|
well at least until I run into a language that doesn't allow initialization with the declaration
anyone remember the old versions of C where the all the vars had to be declared at the top of the function? What a pain:
void nox(){
int i;
for(i=0;i<5;i++){
}
}
as soon as this was legal, the code felt so much cleaner and easier to read IMHO
void nox(){
for(int i=0;i<5;i++){
}
}
as some of my old code needed refactoring, you can guess what was at the first thing attacked.
|
|
|
|
|
|
A$ A! A% - and the others that I don’t remember.
|
|
|
|
|
The boot?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sniff, I am beset by haters.
MS Access & .asp pages - we made things happen.
|
|
|
|
|
Yep. Bad things, mostly!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I find 'var a useful time-saver, and, with ReSharper, I can quickly have every use of 'var in the solution/project/file replaced by the explicit Type.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Having fought run time errors that result from dynamic typed variables, I'll take any option that tells me the data type for the variable when it's declared. This means I don't use the C# "var" keyword or its equivalent in other languages.
|
|
|
|
|
What about "Depends on the language I am using" ?
I use both VB6 and C#.
VB haters relax! I am converting legacy programs that began life in 1978 while converting them to C#.
I have to maintain the VB 6 programs during the conversion.
And yes, I am NOT part of a team, I work alone.
Also, I am certain I am not the only programmer out there using more than one language.
Anyone that thinks C# is less "Verbose" than VB6 is kidding themselves (or not working in business programs).
|
|
|
|
|
Yeah, but that's the point. If you could pick any language, or make up your own language, how would you declare variables? Or does it just not matter?
I wrote this because I'm switching between C# and TypeScript and Javascript and SQL and bash and Powershell and VBScript and I'm all over the shop and I am constantly tripping up on declaring variables. You'd think I'd have that skill sorted by now...
cheers
Chris Maunder
|
|
|
|
|
It's a solid question.
I, like many others, appear to prefer the C style.
One think I liked about the C style was
int a,b,c,d[12]; // so concise, and it emphasizes the types
I use pascal/Delphi a lot, so I have to do:
a,b,c: Integer;
Which honestly makes it harder to split and document at the end of the line.
But with most of todays editors, I am surprised you did not commonize Ctrl-D,V (Define Variable),
to be dynamic depending on the language you are using.
As an Oracle person, I trip up on MSSQL syntax, so my MSSQL editor has Oracle keywords defined to inject comments (TO_DATE(), NVL -> Coalesce(), etc)
And the same for SQLite, and MySQL. Otherwise I lose my sanity.
Finally it looks like it would fit on a 3x5 card
Oh, and I use AHK for date formatting file names with todays date, with a popup.
And I have another popup that reminds me all of the hotkeys, LOL
Screenshot
So I only have 1 hot key to remember (Win-H, for Help, LOL)
Reminds me of the old TSRs in DOS days!
|
|
|
|
|
I feel your pain. I like the C#, the most I guess.
Yep!
|
|
|
|
|
I've been switching Android over to Kotlin and it uses that new syntax (as does Swift) like TypeScript and I've been stumbling around with it too.
It really looks odd to me when you initialize the var in these languages bec it looks like you are setting the type to a value.
var thing : String = "this is the thing"
I sat down and really tried to think about why the new languages made this change. It seems far more cryptic than the old way. I think I had decided on one reason why they'd done it and then I forgot again.
Oh, now I remember what I thought it might be:
They are moving toward dynamically typed languages (of course, laziness rules today ) so now when you leave the type off you'll just have:
var [name] = Thing;
So this is probably some middle-ground syntax and the middle-ground is always weak!!
|
|
|
|
|
Variable declaration isn't the problem, it's the rest of the coding.
Nit pick time, it REALLY bothered me that you didn't terminate the statements.
int count;<-where were the semi-colons?
count: Integer;<-where were the semi-colons?
|
|
|
|
|
MarkTJohnson wrote: where were the semi-colons?
Actually, that is going by the way-side too.
In Swift, they are extraneous.
In Kotlin too.
I don't think Python uses them.
I come from C/C++/C# and now doing a lot of Swift (iOS) and now Kotlin (Android) and it gets really weird because sometimes the compiler doesn't know where the line ends and complains about the wrong things.
I type in semi-colons all the time in Swift and Kotlin though. Just can't help it.
|
|
|
|
|
Count DD 0 MASM, how we've missed ye.
Software Zen: delete this;
|
|
|
|
|
Nullable? Maybe a DB int? Maybe a nullable DB int? Maybe a ranged int? Maybe an int that's the imaginary part of an imaginary number?
Life is no longer as simple as int count = 0; !!!
Nowadays, and int might look like this (mind you, this is fictional code, but probably exists somewhere):
var count = new Nullable<int>(
min: 0,
max: 999,
rangeException: OutOfRangeException,
DBNullDefaultValue = 0);
|
|
|
|
|
Marc Clifton wrote: Maybe an int that's the imaginary part of an imaginary number?
in an imaginary planet, in an imaginary universe?
|
|
|
|
|
var number = 0;
var result = method();
Wake up! The Singularity is coming.
|
|
|
|
|
nmarcel wrote: var number = 0;
The type inferred there is specifically a 32 bit signed integer.
If we intended "number" to be an Int64 for example, we'd have to either default it to a value that's too large for a 32 bit int and reset it to 0 or just take the simple route and declare it explicitly as an Int64.
What is the actual benefit of "var" here? "int number = 0;" is both more readable (we can see what type of int it is without resorting to tool-tips) and we're explicitly telling the compiler what to do rather than hoping it infers our intended type.
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
PeejayAdams wrote: If we intended "number" to be an Int64 for example, we'd have to either default it to a value that's too large for a 32 bit int and reset it to 0 or just take the simple route and declare it explicitly as an Int64.
Or you could use:
var number = 0L;
Not that I would use it, it's just another way.
Although I would probably use:
long number = 0;
|
|
|
|
|
Absolutely!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
PeejayAdams wrote: What is the actual benefit of "var" here?
There is none.
We would do int number = 0 or decimal number = 0 , but never var number = 0 .
|
|
|
|
|
C# is essenially C++ as far as BNF concerned
|
|
|
|