-
mmmm C#
I have to admit my first reaction to C# was hostile. Being a card carrying member of the 'Java haters' club the syntax was a big turnoff for me at first glance. Now that I've studied a bit of C# I'm warming up to it, and what I first believed to be a wasteful syntax isn't quite as wasteful as I first thought ( Believing C# to be a Java ripoff ).
Here are two quickie programs that show size differences between C# and my beloved C.
Code:
// C# code
using System;
class Print
{
public static void printNumber ( int n )
{
while ( n != 0 ) {
Console.Write ( n % 10 );
n /= 10;
}
Console.WriteLine();
}
}
class ProgramDriver
{
public static void Main()
{
Print.printNumber ( 45678 );
}
}
Code:
/* C code */
#include <stdio.h>
#include <stdlib.h>
void printNumber ( int n )
{
while ( n != 0 ) {
printf ( "%d", n % 10 );
n /= 10;
}
printf ( "\n" );
}
int main ( void )
{
printNumber ( 45678 );
return EXIT_SUCCESS;
}
I still don't like how the functions ( methods ) float in space while C's functions are completely left justified. It forces me to use a GNU bracing style to maintain somewhat decent readability.
Does anyone have ideas for a C# bracing style that works well? This textbook code just doesn't do it for me ( I hate textbook code )
Code:
// Yucky yucky
class Print
{
public static void printNumber ( int n )
{
while ( n != 0 )
{
Console.Write ( n % 10 );
n /= 10;
}
Console.WriteLine();
}
}
At the moment I'm using three bracing styles according to indention level. Class level indention ( fully left justified ) gets an Allman style, method level ( first level in, very floaty ) practically demands GNU to look good, and for the processing level ( if..else's, loops, switches, etc.. ) I maintain my K&R bracing.
-Prelude
-
This looks ok, (quasi C++ style)
Code:
class Print{
public static void printNumber ( int n ){
while ( n != 0 ) {
Console.Write ( n % 10 );
n /= 10;
}
Console.WriteLine();
}
}
-
Code:
class Print
{
public static void printNumber(int n)
{
while (n != 0)
{
Console.Write(n % 10);
n /= 10;
}
Console.WriteLine();
}
}
-
Quote:
Originally posted by Unregistered
Code:
class Print
{
public static void printNumber(int n)
{
while (n != 0)
{
Console.Write(n % 10);
n /= 10;
}
Console.WriteLine();
}
}
that's dreadfully ugly. I just code like I usually do in C.
-
Prelude, that's about the same code you'd use if doing the same thing in Java. It makes things much easier for a Java guy (like me) when the syntax is so similar.
I'd structure the code like this...
Code:
using System;
class Print {
public static void printNumber(int n) {
while(n != 0) {
Console.Write(n % 10);
n /= 10;
}
Console.WriteLine();
}
}
class ProgramDriver {
public static void Main() {
Print.printNumber(45678);
}
}
-
>Prelude, that's about the same code you'd use if doing the same thing in Java
hmmm, I didn't realise Java imposed a particular style of code formating.
-
Not the format in particular, but the actual code itself. Just a few changes. Example, instead of "Console.Write(n % 10);" it would be "System.out.println(n % 10);". Most of the code from this certain app could be "ported" like that.